Private Sub cmdStart_Click()
On Error GoTo Err_Handler
Me.MousePointer = vbHourglass
Dim fso As Scripting.FileSystemObject 'FileSystemObject
Dim ts As TextStream
Dim tsURL As String
Dim tempstr As String
Dim strName As String
Dim strNRIC As String
Dim strSocso As String
Dim missing As Integer
'To prevent from processing empty records
If MSFlexGrid1.Rows <= 1 Then
MsgBox " No records available ", vbInformation + vbOKOnly, "Start - Prepare Diskette"
Me.MousePointer = vbDefault
Exit Sub
End If
tsURL = Mid(Drive1.Drive, 1, 2) & "\brg8a.txt"
Set fso = New Scripting.FileSystemObject
If fso.FileExists(tsURL) = False Then fso.CreateTextFile tsURL
Set ts = fso.OpenTextFile(tsURL, ForWriting)
For i = 1 To MSFlexGrid1.Rows - 1
'****** Company Code ******
ts.Write MSFlexGrid1.TextMatrix(i, 0) 'Company Code
'****** NRIC Formatting ******
tempstr = MSFlexGrid1.TextMatrix(i, 1)
missing = 12 - Len(tempstr)
strNRIC = Space(missing) & tempstr
ts.Write strNRIC
'****** SocsoNo Formatting ******
tempstr = MSFlexGrid1.TextMatrix(i, 2)
missing = 9 - Len(tempstr)
strSocso = tempstr & Space(missing)
ts.Write strSocso
'****** Period (MonthYear) ******
ts.Write MSFlexGrid1.TextMatrix(i, 3)
'****** Name Formatting ******
tempstr = MSFlexGrid1.TextMatrix(i, 4)
missing = 45 - Len(tempstr)
strName = tempstr & Space(missing)
ts.Write strName
'****** Contribution Formatting ******
Dim ringgit As String
Dim cents As String
'Dim tempstr As String
Dim cont1 As String
Dim cont2 As String
tempstr = MSFlexGrid1.TextMatrix(i, 5)
ringgit = Mid(tempstr, 1, InStr(1, tempstr, ".", vbTextCompare) - 1)
cents = Mid(tempstr, InStr(1, tempstr, ".", vbTextCompare) + 1, 2)
cont1 = ringgit & cents
If Len(cont1) < 4 Then
If Len(cont1) = 1 Then
cont1 = "000" & cont1
ElseIf Len(cont1) = 2 Then
cont1 = "00" & cont1
ElseIf Len(cont1) = 3 Then
cont1 = "0" & cont1
End If
End If
tempstr = MSFlexGrid1.TextMatrix(i, 6)
ringgit = Mid(tempstr, 1, InStr(1, tempstr, ".", vbTextCompare) - 1)
cents = Mid(tempstr, InStr(1, tempstr, ".", vbTextCompare) + 1, 2)
cont2 = ringgit & cents
If Len(cont2) < 4 Then
If Len(cont2) = 1 Then
cont2 = "000" & cont2
ElseIf Len(cont2) = 2 Then
cont2 = "00" & cont2
ElseIf Len(cont2) = 3 Then
cont2 = "0" & cont2
End If
End If
ts.Write cont1 'Employee Contribution
ts.WriteLine cont2 'Company Contribution
Next
ts.Close
Set ts = Nothing
Set fso = Nothing
Me.MousePointer = vbDefault
MsgBox "Process completed.", vbInformation + vbOKOnly, "Process"
Exit Sub
Err_Handler:
If Err.Number = 0 Then
Resume Next
ElseIf Err.Number = 20 Then
Me.MousePointer = vbDefault
Exit Sub
'ElseIf Err.Number = 91 Then 'Object variable or With block variable not set (appears in Windows 98)
' Resume Next
ElseIf Err.Number = 430 Then 'Error on Windows 98 - Class does not support Automation or does not support expected interface
Resume Next
ElseIf Err.Number = 381 Then 'Runtime Error - Subscript Out of Range (This appears when running from Local hardrive)
MsgBox " " & Err.Description & " ", vbExclamation + vbOKOnly, "ERROR: " & Err.Number
Me.MousePointer = vbDefault
Exit Sub
ElseIf Err.Number = 70 Then 'Runtime Error - Permission Denied (This appears when running from Network Drive)
MsgBox " " & Err.Description & " ", vbExclamation + vbOKOnly, "ERROR: " & Err.Number
Me.MousePointer = vbDefault
Exit Sub
Else
MsgBox " " & Err.Description & " ", vbExclamation + vbOKOnly, "ERROR: " & Err.Number
Me.MousePointer = vbDefault
Exit Sub
End If
End Sub