Need some help please!....
Previous developer wrote the below code which now does not work as we have upgraded to XP and now have MS Access 2003. Basically the routine outputs one record at a time to a text file with fixed field lengths, while also formatting the table fields. I also have to incorporate two carriage return lines in between each record in the text file.
Bit of a novice at VBA and would appreciate and help to rewrite to function in MS Access 2003.
Private Sub Form_Load()
On Error Resume Next
'DoCmd.Hourglass True
Dim sFileName As String
Dim db As Database
Dim RS As Recordset
Dim fs As Scripting.FileSystemObject
Dim f As Scripting.TextStream
Dim newcnt As Integer
Set db = CurrentDb
cnt = 0
sqlstr = "SELECT id, sContact, sContactNo, sRCN, " & _
"sProdFolder , sAccount, sStmtEnd, sStmtNo, " & _
"dCreated, dSent, dComplete, dFailed, " & _
"sPostal1, sPostal2, sAddr1, sAddr2, " & _
"sSuburb, sState, sPostCode, sCustFax, " & _
"sFaxName, iFee, sWaiverInd, sStmtStart, " & _
"sReasonCode, sDebitAccount FROM dbo_dOnDemand WHERE dSent is Null ORDER BY sProdFolder;"
Set fs = New FileSystemObject
sFileName = "TJPORD" & Format(Date, "DDMMYYYY") & ".txt"
Set f = fs.CreateTextFile("\\server\ftp\" & sFileName, True)
Set RS = db.OpenRecordset(sqlstr, dbOpenDynaset, dbSeeChanges)
' Write the header data
s = s & "A"
s = s & Format(Date, "DDMMYYYY")
s = s & AddSpace("EFORM", 8)
s = s & "TJP"
s = s & AddSpace("My File", 60)
s = s & Space(221)
f.WriteLine (s)
Do While Not RS.EOF
cnt = cnt + 1
s = "R"
s = s & "TJP" & AddSpace(RS("sProdFolder").Value, 3)
s = s & AddSpace(RS("sAccount").Value, 16)
Dim sEnd
sEnd = RS("sStmtEnd").Value
s = s & AddSpace(sEnd, 8)
s = s & AddSpace(RS("sStmtNo").Value, 4)
s = s & "P"
If RS("sAddr1").Value <> "" Then
s = s & "N"
Else
s = s & "Y"
End If
s = s & AddSpace(UCase(RS("sPostal1").Value), 40)
s = s & AddSpace(UCase(RS("sPostal2").Value), 40)
s = s & AddSpace(UCase(RS("sAddr1").Value), 40)
s = s & AddSpace(UCase(RS("sAddr2").Value), 40)
Dim myAdd
myAdd = UCase(RS("sSuburb").Value) & " " & UCase(RS("sState").Value) & " " & RS("sPostCode").Value
s = s & AddSpace(myAdd, 40)
s = s & AddSpace(RS("sContactNo").Value, 10)
s = s & AddSpace(Left(RS("sContact").Value, 17), 17) & AddSpace(RS("sDebitAccount").Value, 16)
Dim sStart
sStart = RS("sStmtStart").Value
s = s & AddSpace(sStart, 8)
s = s & AddSpace(RS("sWaiverInd").Value, 1)
If UCase(RS("sWaiverInd").Value) = "N" Then
s = s & Space(2)
Else
s = s & AddSpace(RS("sReasonCode").Value, 2)
End If
s = s & AddSpace(RS("sRCN").Value, 7)
s = s & Space(3)
RS.Edit
RS![dSent] = Now()
RS.Update
f.WriteLine s
RS.MoveNext
Loop
'Write the trailer data
s = "T"
s = s & Format(Date, "DDMMYYYY")
s = s & AddSpace("FORM", 8)
s = s & "CBA"
s = s & AddSpace("My File", 60)
s = s & AddSpace(cnt, 7) '& record count = 7
s = s & Space(214)
f.WriteLine s
f.Close
Set f = Nothing
Set fs = Nothing
DoCmd.Hourglass False
DoCmd.Quit acQuitSaveNone
End Sub
Function AddSpace(sField, sLen) As String
If Len(sField) >= sLen Then
AddSpace = Left(sField, sLen)
Else
If IsNull(Len(sField)) Then
AddSpace = Space(sLen)
Else
AddSpace = sField & Space(sLen - Len(sField))
End If
End If
End Function
Previous developer wrote the below code which now does not work as we have upgraded to XP and now have MS Access 2003. Basically the routine outputs one record at a time to a text file with fixed field lengths, while also formatting the table fields. I also have to incorporate two carriage return lines in between each record in the text file.
Bit of a novice at VBA and would appreciate and help to rewrite to function in MS Access 2003.
Private Sub Form_Load()
On Error Resume Next
'DoCmd.Hourglass True
Dim sFileName As String
Dim db As Database
Dim RS As Recordset
Dim fs As Scripting.FileSystemObject
Dim f As Scripting.TextStream
Dim newcnt As Integer
Set db = CurrentDb
cnt = 0
sqlstr = "SELECT id, sContact, sContactNo, sRCN, " & _
"sProdFolder , sAccount, sStmtEnd, sStmtNo, " & _
"dCreated, dSent, dComplete, dFailed, " & _
"sPostal1, sPostal2, sAddr1, sAddr2, " & _
"sSuburb, sState, sPostCode, sCustFax, " & _
"sFaxName, iFee, sWaiverInd, sStmtStart, " & _
"sReasonCode, sDebitAccount FROM dbo_dOnDemand WHERE dSent is Null ORDER BY sProdFolder;"
Set fs = New FileSystemObject
sFileName = "TJPORD" & Format(Date, "DDMMYYYY") & ".txt"
Set f = fs.CreateTextFile("\\server\ftp\" & sFileName, True)
Set RS = db.OpenRecordset(sqlstr, dbOpenDynaset, dbSeeChanges)
' Write the header data
s = s & "A"
s = s & Format(Date, "DDMMYYYY")
s = s & AddSpace("EFORM", 8)
s = s & "TJP"
s = s & AddSpace("My File", 60)
s = s & Space(221)
f.WriteLine (s)
Do While Not RS.EOF
cnt = cnt + 1
s = "R"
s = s & "TJP" & AddSpace(RS("sProdFolder").Value, 3)
s = s & AddSpace(RS("sAccount").Value, 16)
Dim sEnd
sEnd = RS("sStmtEnd").Value
s = s & AddSpace(sEnd, 8)
s = s & AddSpace(RS("sStmtNo").Value, 4)
s = s & "P"
If RS("sAddr1").Value <> "" Then
s = s & "N"
Else
s = s & "Y"
End If
s = s & AddSpace(UCase(RS("sPostal1").Value), 40)
s = s & AddSpace(UCase(RS("sPostal2").Value), 40)
s = s & AddSpace(UCase(RS("sAddr1").Value), 40)
s = s & AddSpace(UCase(RS("sAddr2").Value), 40)
Dim myAdd
myAdd = UCase(RS("sSuburb").Value) & " " & UCase(RS("sState").Value) & " " & RS("sPostCode").Value
s = s & AddSpace(myAdd, 40)
s = s & AddSpace(RS("sContactNo").Value, 10)
s = s & AddSpace(Left(RS("sContact").Value, 17), 17) & AddSpace(RS("sDebitAccount").Value, 16)
Dim sStart
sStart = RS("sStmtStart").Value
s = s & AddSpace(sStart, 8)
s = s & AddSpace(RS("sWaiverInd").Value, 1)
If UCase(RS("sWaiverInd").Value) = "N" Then
s = s & Space(2)
Else
s = s & AddSpace(RS("sReasonCode").Value, 2)
End If
s = s & AddSpace(RS("sRCN").Value, 7)
s = s & Space(3)
RS.Edit
RS![dSent] = Now()
RS.Update
f.WriteLine s
RS.MoveNext
Loop
'Write the trailer data
s = "T"
s = s & Format(Date, "DDMMYYYY")
s = s & AddSpace("FORM", 8)
s = s & "CBA"
s = s & AddSpace("My File", 60)
s = s & AddSpace(cnt, 7) '& record count = 7
s = s & Space(214)
f.WriteLine s
f.Close
Set f = Nothing
Set fs = Nothing
DoCmd.Hourglass False
DoCmd.Quit acQuitSaveNone
End Sub
Function AddSpace(sField, sLen) As String
If Len(sField) >= sLen Then
AddSpace = Left(sField, sLen)
Else
If IsNull(Len(sField)) Then
AddSpace = Space(sLen)
Else
AddSpace = sField & Space(sLen - Len(sField))
End If
End If
End Function