These weren't my first problems, my first version had a class withing this class called Export. When things weren't working in ASP I got rid of it and threw all the properties into one fat ugly class.

Basically what this is supposed to do is take sql tables and output them to a fixed width file (much like one can do in access). At first I passed it Recordsets, but since my problem I've whittled it down to passing connection strings and SQL statements etc. The worst part is that I never saved my original version! Oh well you live you learn...
Confession: I don't code with comments

Here's the code to my class object, it is named FWF for fixed width file:
Option Explicit
'Declare private module-level variable to store value.
Private P_errors As Integer
Private p_OutputFilePath As String
Private p_Spacer As String
Private p_SQL As String
Private p_ArrayIndex As Single
Private p_ArrRecords() As String
Private p_Connection As String
Private p_ColumnNames As String
Private p_TableName As String
Private p_WidthAmounts As String
Private p_SpacerCharacters As String
Private p_ExportSpacer() As String
Private p_bSpacerRedimmed As Boolean
Private p_Width() As Integer
Private p_bWidthRedimmed As Boolean
Private p_FooterDateFormat As String
Private p_HeaderDateFormat As String
Private p_BodyDateFormat As String
Private p_TempSQL As String
Private p_TempConnection As String
Private p_tempSection As String
Public Property Get Errors() As String
Errors = P_errors
End Property
Public Property Let HeaderDateFormat(strHeaderDateFormat As String)
' Store value of argument in module-level variable.
p_HeaderDateFormat = strHeaderDateFormat
End Property
Public Property Get HeaderDateFormat() As String
HeaderDateFormat = p_HeaderDateFormat
End Property
Public Property Let BodyDateFormat(strBodyDateFormat As String)
' Store value of argument in module-level variable.
p_BodyDateFormat = strBodyDateFormat
End Property
Public Property Get BodyDateFormat() As String
BodyDateFormat = p_BodyDateFormat
End Property
Public Property Let FooterDateFormat(strFooterDateFormat As String)
' Store value of argument in module-level variable.
p_FooterDateFormat = strFooterDateFormat
End Property
Public Property Get FooterDateFormat() As String
FooterDateFormat = p_FooterDateFormat
End Property
Public Property Let ConnectionS(ConnectionString As String)
p_Connection = ConnectionString
End Property
Public Property Get ConnectionS() As String
ConnectionS = p_Connection
End Property
Public Property Let TableName(strTableName As String)
' Store value of argument in module-level variable.
p_TableName = strTableName
End Property
Public Property Get TableName() As String
TableName = p_TableName
End Property
Public Property Let ColumnNames(strColumnName As String)
' Store value of argument in module-level variable.
p_ColumnNames = strColumnName
End Property
Public Property Get ColumnNames() As String
ColumnNames = p_ColumnNames
End Property
Public Property Let WidthAmounts(strWidthAmounts As String)
' Store value of argument in module-level variable.
p_WidthAmounts = strWidthAmounts
End Property
Public Property Get WidthAmounts() As String
WidthAmounts = p_WidthAmounts
End Property
Public Property Let SpacerCharacters(strSpacerCharacters As String)
' Store value of argument in module-level variable.
p_SpacerCharacters = strSpacerCharacters
End Property
Public Property Get SpacerCharacters() As String
SpacerCharacters = p_SpacerCharacters
End Property
Public Property Get Spacer(Index As Integer) As String
Spacer = p_ExportSpacer(Index)
End Property
Public Property Let Spacer(Index As Integer, strSpacer As String)
If p_bSpacerRedimmed = False Then 'p_Spacer has no index
ReDim Preserve p_ExportSpacer(Index)
p_bSpacerRedimmed = True
Else 'p_Spacer can be Redimmed
If UBound(p_ExportSpacer) < Index Then
ReDim Preserve p_ExportSpacer(Index)
End If
End If
p_ExportSpacer(Index) = strSpacer
End Property
Public Property Get Width(Index As Integer) As Integer
Width = p_Width(Index)
End Property
Public Property Let Width(Index As Integer, intWidth As Integer)
If p_bWidthRedimmed = False Then 'p_Width() has no index
ReDim Preserve p_Width(Index)
p_bWidthRedimmed = True
Else 'p_Width() can be Redimmed
If UBound(p_Width) < Index Then
ReDim Preserve p_Width(Index)
End If
End If
p_Width(Index) = intWidth
End Property
Public Property Let OutputFilePath(strOutputFilePath As String)
' Store value of argument in module-level variable.
p_OutputFilePath = strOutputFilePath
End Property
Public Property Get OutputFilePath() As String
OutputFilePath = p_OutputFilePath
End Property
Public Property Let TempSQL(strSQL As String)
' Store value of argument in module-level variable.
p_TempSQL = strSQL
End Property
Public Property Get TempSQL() As String
TempSection = p_tempSection
End Property
Public Property Get TempSection() As String
TempSection = p_tempSection
End Property
Public Property Let TempSection(Section As String)
' Store value of argument in module-level variable.
p_tempSection = Section
End Property
Public Property Get TempConnection() As String
TempConnection = p_TempConnection
End Property
Public Property Let TempConnection(TempConnection As String)
' Store value of argument in module-level variable.
p_TempConnection = TempConnection
End Property
Public Sub AddSource()
On Error GoTo ErrorLabel
Dim Recordset As New ADODB.Recordset
Recordset.Open p_TempSQL, p_TempConnection, adOpenDynamic, adLockOptimistic
Dim Section As String
Section = p_tempSection
Call DiscoverWidthsAndSpacers(Recordset, Me)
p_ArrayIndex = WriteDataToArray(Recordset, Me, Section, p_ArrRecords, p_ArrayIndex)
Call ArrayToText(p_ArrRecords, p_OutputFilePath)
Recordset.Close
Set Recordset = Nothing
Exit Sub
ErrorLabel:
P_errors = P_errors + 1
End Sub
'Public Property Get ExportSpecs() As ExportSpecs
' Set ExportSpecs = p_ExportSpecs
'ExportSpecTable.DataSource = p_ExportSpecTable.DataSource
'End Property
'Public Property Let ExportSpecs(strExportSpecTable As ExportSpecs)
' Store value of argument in module-level variable.
' p_ExportSpecs.Connection = strExportSpecTable.Connection
'End Property
Public Property Get LinesWritten() As Integer
LinesWritten = UBound(p_ArrRecords())
End Property
Here is my Code Module:
Option Explicit
Public Function CompleteField(Data As String, Width As Integer, Spacer As String, DateFormat As String, Optional bLeft As Boolean) As String
Dim k As Integer
Dim intSpaces As Integer
Dim strNewString As String
If IsDate(Data) Then
Data = Format(Data, DateFormat)
If IsDate(Data) Then MsgBox Data
End If
Data = Left(Data, Width)
intSpaces = Width - Len(Data)
For k = 1 To intSpaces
strNewString = strNewString & Spacer
Next
If bLeft = True Then
CompleteField = Data & strNewString
Else
CompleteField = strNewString & Data
End If
End Function
Public Sub DiscoverWidthsAndSpacers(objRS As Recordset, ExportSpecifications As FWF)
Dim Columns As Integer
Dim k As Integer
Dim mySQL As String
Dim RSColumn As String
Dim myConnectionString As String
Dim ObjRSExport As ADODB.Recordset
Dim myConnection As New ADODB.Connection
Dim myTableName As String, myWidthColumn As String
Dim mySpacerColumn As String, myColumnNamesColumn As String
myTableName = ExportSpecifications.TableName
myWidthColumn = ExportSpecifications.WidthAmounts
mySpacerColumn = ExportSpecifications.SpacerCharacters
myColumnNamesColumn = ExportSpecifications.ColumnNames
myConnectionString = ExportSpecifications.ConnectionS
myConnection.Open myConnectionString
Columns = objRS.Fields.Count
For k = 0 To (Columns - 1)
RSColumn = objRS.Fields(k).Name
mySQL = "Select " & myWidthColumn & "," & mySpacerColumn
mySQL = mySQL & " From " & myTableName
mySQL = mySQL & " where " & myColumnNamesColumn & " = '" & RSColumn & "'"
Set ObjRSExport = myConnection.Execute(mySQL)
ExportSpecifications.Width(k) = ObjRSExport.Fields(0)
ExportSpecifications.Spacer(k) = ObjRSExport.Fields(1)
Next
End Sub
Public Function WriteDataToArray(objRS As ADODB.Recordset, Export As FWF, ReportSection As String, arr() As String, ByVal ArrayIndex As Single) As Single
Dim strThisLine As String
Dim Handle As Integer
Dim RecordCounter As Integer
Dim k As Integer, Columns As Integer
Dim strDateFormat As String
Columns = objRS.Fields.Count
objRS.MoveFirst
Select Case ReportSection
Case "Header"
strDateFormat = Export.HeaderDateFormat
Case "Body"
strDateFormat = Export.BodyDateFormat
Case "Footer"
strDateFormat = Export.FooterDateFormat
End Select
Do Until objRS.EOF
strThisLine = ""
For k = 0 To (Columns - 1)
strThisLine = strThisLine & CompleteField(objRS.Fields(k), Export.Width(k), Export.Spacer(k), strDateFormat)
Next
ReDim Preserve arr(ArrayIndex)
arr(ArrayIndex) = strThisLine
objRS.MoveNext
ArrayIndex = ArrayIndex + 1
Loop
WriteDataToArray = ArrayIndex
End Function
Public Sub ArrayToText(arr() As String, OutputPath As String)
Dim s As String
Dim Handle As Integer, k As Integer
Handle = FreeFile
Open OutputPath For Output As #Handle
For k = LBound(arr()) To UBound(arr())
s = arr(k)
Print #Handle, s
Next
Close (Handle)
End Sub
Here is my ASP Code:
Public Function WriteText(strFilePath)
Dim MyReport, MyRecords, ConnectionString, mySQLHeader, mySQLBody
Dim mySQLFooter, ObjRSHeader, ObjRSBody, ObjRSFooter, myConnection
Set myConnection = Server.CreateObject("Adodb.Connection"
Set MyReport = Server.CreateObject("DBtoText.Fwf"
ConnectionString = "Provider=SQLOLEDB; Data Source=SERVERNAME;Initial Catalog=DataBase;User ID=Uid;password=pword;"
MyReport.OutputFilePath = strFilePath
myConnection.Open ConnectionString
With MyReport
.BodyDateFormat = "mmddyy"
.HeaderDateFormat = "yyyymmdd"
.ConnectionS = ConnectionString
.TableName = "ColumnSpecs"
.ColumnNames = "ColumnName"
.SpacerCharacters = "Spacer"
.WidthAmounts = "ColumnWidth"
.TempConnection = ConnectionString
.TempSection = "SuperHeader"
.TempSQL = "Select * from view_SuperHeader"
.AddSource
.TempConnection = ConnectionString
.TempSection = "Header"
.TempSQL = "Select * from view_bofaheader"
.AddSource
.TempSection = "Body"
.TempSQL = "Select * from view_BofABody"
.AddSource
.TempSection = "Footer"
.TempSQL = "Select * from view_bofatrailer"
.AddSource
End With
If MyReport.Errors >0 then
Response.Write "<font color=red>There were " & MyReport.Errors & " errors during process.</font>"
End if
myConnection.Execute("delete from importboachecks"
WriteText = MyReport.LinesWritten -2 'to discount the headers
Set MyReport = Nothing
'Response.Write "<br>Lines Written to file: " & MyRecords
'Response.Write "<br>Output:" & strFilePath
End Function