by Anonymous (267 Submissions)
Category: Internet/HTML
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 31st March 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Transaction processing program with random access files
Transaction processing program with random access files
Option Explicit
Private Type ClientRecord
accountNumber As Integer
lastName As String * 15
firstName As String * 15
balance As Currency
End Type
Dim mUdtClient As ClientRecord user defined type
Private Sub Form_Load()
tabOperations.Enabled = False
End Sub
Sub cmdOpenFile_Click()
Dim recordLength As Long
Dim filename As String
Determine number of bytes in a ClientRecord object
recordLength = LenB(mUdtClient)
dlgOpen.ShowOpen
filename = dlgOpen.filename
If dlgOpen.FileTitle <> "" Then
Open file for writing
Open filename For Random Access Read Write As #1 _
Len = recordLength
cmdOpenFile.Enabled = False Disable button
cmdCloseFile.Enabled = True
tabOperations.Enabled = True
Else
MsgBox ("You must specify a file name")
End If
End Sub
Create a text file representation of the random-access file
Private Sub cmdTextFile_Click()
Dim filename As String, balanceString As String
On Error Resume Next
dlgTextFile.ShowOpen
filename = dlgTextFile.filename
If dlgTextFile.FileTitle <> "" Then
Open file for writing
Open filename For Output Access Write As #2
Print #2, "Account";
Print #2, Tab(10); "First Name";
Print #2, Tab(28); "Last Name";
Print #2, Tab(46); Format("Balance", "@@@@@@@@@@")
Seek #1, 1 reposition to start of file
Get #1, , mUdtClient read first record
While Not EOF(1)
If mUdtClient.accountNumber <> 0 Then
Print #2, mUdtClient.accountNumber;
Print #2, Tab(10); mUdtClient.firstName;
Print #2, Tab(28); mUdtClient.lastName;
balanceString = _
Format(mUdtClient.balance, "0.00")
Print #2, Tab(46);
Print #2, Format(balanceString, "@@@@@@@@@@")
End If
Get #1, , mUdtClient 'read next record
Wend
Close #2
Else
MsgBox ("You must specify a file name")
End If
End Sub
Add a new record to the file
Private Sub cmdAddNew_Click()
If txtNewAccount.Text <> "" Then
Get #1, Val(txtNewAccount), mUdtClient 'read record
If mUdtClient.accountNumber = 0 Then
mUdtClient.accountNumber = Val(txtNewAccount)
mUdtClient.firstName = txtNewFirstName.Text
mUdtClient.lastName = txtNewLastName.Text
mUdtClient.balance = txtNewBalance.Text
Put #1, mUdtClient.accountNumber, mUdtClient
MsgBox ("Account " & mUdtClient.accountNumber & _
" has been added to the file")
Else
MsgBox ("Account already exists")
End If
Else
MsgBox ("You must enter an account number")
End If
End Sub
Update an existing record
Private Sub cmdUpdate_Click()
Dim account As Integer, transactionAmount As Double
On Error Resume Next
account = Val(InputBox("Enter account number"))
Get #1, account, mUdtClient 'read record
If mUdtClient.accountNumber <> 0 Then
txtUpdateAccount.Text = Str$(mUdtClient.accountNumber)
txtUpdateFirstName.Text = mUdtClient.firstName
txtUpdateLastName.Text = mUdtClient.lastName
txtUpdateBalance.Text = Str$(mUdtClient.balance)
transactionAmount = Val(InputBox( _
"Enter transaction amount. Positive for charge. " & _
"Negative for payment."))
mUdtClient.balance = _
mUdtClient.balance + transactionAmount
txtUpdateBalance.Text = Str$(mUdtClient.balance)
Put #1, mUdtClient.accountNumber, mUdtClient
Else
MsgBox ("Record " & account & " does not exist")
End If
End Sub
Delete the specified record
Private Sub cmdDelete_Click()
Dim blankClient As ClientRecord
On Error Resume Next
Get #1, Val(txtDelete.Text), mUdtClient 'read record
If mUdtClient.accountNumber <> 0 Then
Put #1, mUdtClient.accountNumber, blankClient
MsgBox ("Account # " & mUdtClient.accountNumber & _
" has been deleted")
Else
MsgBox ("Record does not exist")
End If
End Sub
Sub cmdCloseFile_Click()
Close #1
cmdOpenFile.Enabled = True
cmdCloseFile.Enabled = False
End Sub
Private Sub Form_Terminate()
Close
End Sub
Private Sub cmdExit_Click()
Close
End
End Sub