kingesk
09-08-2000, 04:14 PM
'This program should GET a text file, then output only
'the records I want with different field lengths.
'It worked with only 1 record but when I used a file
'with more than one record only the 1st record was formatted
'and the rest were output but not in the correct format (It looked
'like some data was added to the first line that wasn't supposed to be there).
'Also starting in the second line there were fields that were not supposed to be there.
'I have attached a zipped file containg this program and a sample
'text file to input.
'Any sugestions would be greatly appreciated.
Eric King
'PS
'I also noticed some people were attaching their programs to their post
'that did not seem to be zipped. How are they putting both their frm (form file)
'and vbp(project file) in one attachment?
'module level variables
'file name strings
Dim mstrInputFileName As String
Dim mstrOutputFileName As String
'define the input record format
Private Type InputRecord
strType As String * 1
strID As String * 4
strBenefit As String * 4
strApp As String * 3
strLocation As String * 4
strSchedule As String * 2
strDate As String * 8
strSSN As String * 9
strFirst As String * 20
strLast As String * 20
strStreet1 As String * 30
strStreet2 As String * 30
strCity As String * 32
strState As String * 2
strZip As String * 9
strBirthDate As String * 8
strEmpDate As String * 8
strTermDate As String * 8
strRehireDate As String * 8
strSex As String * 1
strKey As String * 1
strStatus As String * 1
strEffDate As String * 8
strSalary As String * 11
strDedCode1 As String * 3
strAmount1 As String * 6
strDedCode2 As String * 3
strAmount2 As String * 6
strDedCode3 As String * 3
strAmount3 As String * 6
strDedCode4 As String * 3
strAmount4 As String * 6
strDedCode5 As String * 3
strAmount5 As String * 6
strDedCode6 As String * 3
strAmount6 As String * 6
strTotalAmount As String * 8
End Type
'define the output record format
Private Type OutputRecord
strFirst As String * 10
strLast As String * 10
strSex As String * 1
strKey As String * 1
strStatus As String * 2
strSSN As String * 9
strBirthDate As String * 8
strEmpDate As String * 8
strSalary As String * 8
strStreet1 As String * 25
strStreet2 As String * 25
strCity As String * 15
strState As String * 2
strZip As String * 9
strDedCode1 As String * 3
strAmount1 As String * 6
strDedCode2 As String * 3
strAmount2 As String * 6
strDedCode3 As String * 3
strAmount3 As String * 6
strDedCode4 As String * 3
strAmount4 As String * 6
strDedCode5 As String * 3
strAmount5 As String * 6
strDedCode6 As String * 3
strAmount6 As String * 6
strDedCode7 As String * 3
strAmount7 As String * 6
strDedCode8 As String * 3
strAmount8 As String * 6
strDedCode9 As String * 3
strAmount9 As String * 6
strTotalAmount As String * 8
strTermDate As String * 8
End Type
Option Explicit
Private Sub cmdExit_Click()
'Exit the application
End
End Sub
Private Sub cmdFormat_Click()
'This method will perform the reformatting of the input file
'and generate an output file of the selected name and path
'define our buffers
Dim udtInputRecord As InputRecord
Dim udtOutputRecord As OutputRecord
'local variables
Dim intNumRecords As Integer
Dim intIndex As Integer
On Error GoTo HandleErrors
'get the input file name
dlgCommon.DialogTitle = "Select Input File"
dlgCommon.InitDir = "a:""
dlgCommon.FileName = vbNullString
dlgCommon.Filter = "*.txt"
dlgCommon.ShowOpen
mstrInputFileName = dlgCommon.FileName
'get output file name
dlgCommon.DialogTitle = "Select Output File"
dlgCommon.InitDir = "a:""
dlgCommon.FileName = vbNullString
dlgCommon.Filter = "*.txt"
dlgCommon.ShowOpen
mstrOutputFileName = dlgCommon.FileName
'clear out the output file
Open mstrOutputFileName For Output As #2
Close #2
'open the input and output files
Open mstrInputFileName For Random As #1 Len = Len(udtInputRecord)
Open mstrOutputFileName For Random As #2 Len = Len(udtOutputRecord)
'perform reformatting function
intNumRecords = LOF(1) / Len(udtInputRecord)
For intIndex = 1 To intNumRecords
Get #1, intIndex, udtInputRecord
udtOutputRecord.strFirst = udtInputRecord.strFirst
udtOutputRecord.strLast = udtInputRecord.strLast
udtOutputRecord.strSex = udtInputRecord.strSex
udtOutputRecord.strKey = udtInputRecord.strKey
udtOutputRecord.strStatus = udtInputRecord.strStatus
udtOutputRecord.strSSN = udtInputRecord.strSSN
udtOutputRecord.strBirthDate = udtInputRecord.strBirthDate
udtOutputRecord.strEmpDate = udtInputRecord.strEmpDate
udtOutputRecord.strSalary = udtInputRecord.strSalary
udtOutputRecord.strStreet1 = udtInputRecord.strStreet1
udtOutputRecord.strStreet2 = udtInputRecord.strStreet2
udtOutputRecord.strCity = udtInputRecord.strCity
udtOutputRecord.strState = udtInputRecord.strState
udtOutputRecord.strZip = udtInputRecord.strZip
udtOutputRecord.strDedCode1 = udtInputRecord.strDedCode1
udtOutputRecord.strAmount1 = udtInputRecord.strAmount1
udtOutputRecord.strDedCode2 = udtInputRecord.strDedCode2
udtOutputRecord.strAmount2 = udtInputRecord.strAmount2
udtOutputRecord.strDedCode3 = udtInputRecord.strDedCode3
udtOutputRecord.strAmount3 = udtInputRecord.strAmount3
udtOutputRecord.strDedCode4 = udtInputRecord.strDedCode4
udtOutputRecord.strAmount4 = udtInputRecord.strAmount4
udtOutputRecord.strDedCode5 = udtInputRecord.strDedCode5
udtOutputRecord.strAmount5 = udtInputRecord.strAmount5
udtOutputRecord.strDedCode6 = udtInputRecord.strDedCode6
udtOutputRecord.strAmount6 = udtInputRecord.strAmount6
udtOutputRecord.strDedCode7 = " "
udtOutputRecord.strAmount7 = " "
udtOutputRecord.strDedCode8 = " "
udtOutputRecord.strAmount8 = " "
udtOutputRecord.strDedCode9 = " "
udtOutputRecord.strAmount9 = " "
udtOutputRecord.strTotalAmount = udtInputRecord.strTotalAmount
udtOutputRecord.strTermDate = udtInputRecord.strTermDate
'write output record to file
Put #2, intIndex, udtOutputRecord
Next
'close the files
Close #1
Close #2
Exit Sub
HandleErrors:
Dim intResponse As Integer
Select Case Err.Number
Case 53, 76, 75 'file or path not found
intResponse = MsgBox("Create a new file?", vbYesNo + vbQuestion, "File not found")
If intResponse = vbYes Then
Resume
Else
Call cmdExit_Click
End If
Case 71 'disk not ready
intResponse = MsgBox("Disk not ready. Retry?", vbRetryCancel + vbQuestion, "Disk Error")
If intResponse = vbRetry Then
Resume 'Try again
Else
Call cmdExit_Click 'Exit project
End If
Case 5
intResponse = MsgBox("Invalid call or procedure (error 5), continue?", vbYesNo + vbQuestion, "Error 5")
If intResponse = vbYes Then
Resume
Else
Call cmdExit_Click
End If
Case Else 'all other errors should cancel execution
Err.Raise Err
End Select
Resume
End Sub
Private Sub Form_Activate()
Left = (Screen.Width - Width) 2
Top = (Screen.Height - Height) 2
'Execute a virus scan
'Shell "C:ericmy-vbshellProject1.exe", vbMaximizedFocus
End Sub
'the records I want with different field lengths.
'It worked with only 1 record but when I used a file
'with more than one record only the 1st record was formatted
'and the rest were output but not in the correct format (It looked
'like some data was added to the first line that wasn't supposed to be there).
'Also starting in the second line there were fields that were not supposed to be there.
'I have attached a zipped file containg this program and a sample
'text file to input.
'Any sugestions would be greatly appreciated.
Eric King
'PS
'I also noticed some people were attaching their programs to their post
'that did not seem to be zipped. How are they putting both their frm (form file)
'and vbp(project file) in one attachment?
'module level variables
'file name strings
Dim mstrInputFileName As String
Dim mstrOutputFileName As String
'define the input record format
Private Type InputRecord
strType As String * 1
strID As String * 4
strBenefit As String * 4
strApp As String * 3
strLocation As String * 4
strSchedule As String * 2
strDate As String * 8
strSSN As String * 9
strFirst As String * 20
strLast As String * 20
strStreet1 As String * 30
strStreet2 As String * 30
strCity As String * 32
strState As String * 2
strZip As String * 9
strBirthDate As String * 8
strEmpDate As String * 8
strTermDate As String * 8
strRehireDate As String * 8
strSex As String * 1
strKey As String * 1
strStatus As String * 1
strEffDate As String * 8
strSalary As String * 11
strDedCode1 As String * 3
strAmount1 As String * 6
strDedCode2 As String * 3
strAmount2 As String * 6
strDedCode3 As String * 3
strAmount3 As String * 6
strDedCode4 As String * 3
strAmount4 As String * 6
strDedCode5 As String * 3
strAmount5 As String * 6
strDedCode6 As String * 3
strAmount6 As String * 6
strTotalAmount As String * 8
End Type
'define the output record format
Private Type OutputRecord
strFirst As String * 10
strLast As String * 10
strSex As String * 1
strKey As String * 1
strStatus As String * 2
strSSN As String * 9
strBirthDate As String * 8
strEmpDate As String * 8
strSalary As String * 8
strStreet1 As String * 25
strStreet2 As String * 25
strCity As String * 15
strState As String * 2
strZip As String * 9
strDedCode1 As String * 3
strAmount1 As String * 6
strDedCode2 As String * 3
strAmount2 As String * 6
strDedCode3 As String * 3
strAmount3 As String * 6
strDedCode4 As String * 3
strAmount4 As String * 6
strDedCode5 As String * 3
strAmount5 As String * 6
strDedCode6 As String * 3
strAmount6 As String * 6
strDedCode7 As String * 3
strAmount7 As String * 6
strDedCode8 As String * 3
strAmount8 As String * 6
strDedCode9 As String * 3
strAmount9 As String * 6
strTotalAmount As String * 8
strTermDate As String * 8
End Type
Option Explicit
Private Sub cmdExit_Click()
'Exit the application
End
End Sub
Private Sub cmdFormat_Click()
'This method will perform the reformatting of the input file
'and generate an output file of the selected name and path
'define our buffers
Dim udtInputRecord As InputRecord
Dim udtOutputRecord As OutputRecord
'local variables
Dim intNumRecords As Integer
Dim intIndex As Integer
On Error GoTo HandleErrors
'get the input file name
dlgCommon.DialogTitle = "Select Input File"
dlgCommon.InitDir = "a:""
dlgCommon.FileName = vbNullString
dlgCommon.Filter = "*.txt"
dlgCommon.ShowOpen
mstrInputFileName = dlgCommon.FileName
'get output file name
dlgCommon.DialogTitle = "Select Output File"
dlgCommon.InitDir = "a:""
dlgCommon.FileName = vbNullString
dlgCommon.Filter = "*.txt"
dlgCommon.ShowOpen
mstrOutputFileName = dlgCommon.FileName
'clear out the output file
Open mstrOutputFileName For Output As #2
Close #2
'open the input and output files
Open mstrInputFileName For Random As #1 Len = Len(udtInputRecord)
Open mstrOutputFileName For Random As #2 Len = Len(udtOutputRecord)
'perform reformatting function
intNumRecords = LOF(1) / Len(udtInputRecord)
For intIndex = 1 To intNumRecords
Get #1, intIndex, udtInputRecord
udtOutputRecord.strFirst = udtInputRecord.strFirst
udtOutputRecord.strLast = udtInputRecord.strLast
udtOutputRecord.strSex = udtInputRecord.strSex
udtOutputRecord.strKey = udtInputRecord.strKey
udtOutputRecord.strStatus = udtInputRecord.strStatus
udtOutputRecord.strSSN = udtInputRecord.strSSN
udtOutputRecord.strBirthDate = udtInputRecord.strBirthDate
udtOutputRecord.strEmpDate = udtInputRecord.strEmpDate
udtOutputRecord.strSalary = udtInputRecord.strSalary
udtOutputRecord.strStreet1 = udtInputRecord.strStreet1
udtOutputRecord.strStreet2 = udtInputRecord.strStreet2
udtOutputRecord.strCity = udtInputRecord.strCity
udtOutputRecord.strState = udtInputRecord.strState
udtOutputRecord.strZip = udtInputRecord.strZip
udtOutputRecord.strDedCode1 = udtInputRecord.strDedCode1
udtOutputRecord.strAmount1 = udtInputRecord.strAmount1
udtOutputRecord.strDedCode2 = udtInputRecord.strDedCode2
udtOutputRecord.strAmount2 = udtInputRecord.strAmount2
udtOutputRecord.strDedCode3 = udtInputRecord.strDedCode3
udtOutputRecord.strAmount3 = udtInputRecord.strAmount3
udtOutputRecord.strDedCode4 = udtInputRecord.strDedCode4
udtOutputRecord.strAmount4 = udtInputRecord.strAmount4
udtOutputRecord.strDedCode5 = udtInputRecord.strDedCode5
udtOutputRecord.strAmount5 = udtInputRecord.strAmount5
udtOutputRecord.strDedCode6 = udtInputRecord.strDedCode6
udtOutputRecord.strAmount6 = udtInputRecord.strAmount6
udtOutputRecord.strDedCode7 = " "
udtOutputRecord.strAmount7 = " "
udtOutputRecord.strDedCode8 = " "
udtOutputRecord.strAmount8 = " "
udtOutputRecord.strDedCode9 = " "
udtOutputRecord.strAmount9 = " "
udtOutputRecord.strTotalAmount = udtInputRecord.strTotalAmount
udtOutputRecord.strTermDate = udtInputRecord.strTermDate
'write output record to file
Put #2, intIndex, udtOutputRecord
Next
'close the files
Close #1
Close #2
Exit Sub
HandleErrors:
Dim intResponse As Integer
Select Case Err.Number
Case 53, 76, 75 'file or path not found
intResponse = MsgBox("Create a new file?", vbYesNo + vbQuestion, "File not found")
If intResponse = vbYes Then
Resume
Else
Call cmdExit_Click
End If
Case 71 'disk not ready
intResponse = MsgBox("Disk not ready. Retry?", vbRetryCancel + vbQuestion, "Disk Error")
If intResponse = vbRetry Then
Resume 'Try again
Else
Call cmdExit_Click 'Exit project
End If
Case 5
intResponse = MsgBox("Invalid call or procedure (error 5), continue?", vbYesNo + vbQuestion, "Error 5")
If intResponse = vbYes Then
Resume
Else
Call cmdExit_Click
End If
Case Else 'all other errors should cancel execution
Err.Raise Err
End Select
Resume
End Sub
Private Sub Form_Activate()
Left = (Screen.Width - Width) 2
Top = (Screen.Height - Height) 2
'Execute a virus scan
'Shell "C:ericmy-vbshellProject1.exe", vbMaximizedFocus
End Sub