📄 form1.frm
字号:
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0"; "TABCTL32.OCX"
Begin VB.Form Form1
Caption = "Transaction Processing Program"
ClientHeight = 2940
ClientLeft = 60
ClientTop = 345
ClientWidth = 6045
LinkTopic = "Form1"
ScaleHeight = 2940
ScaleWidth = 6045
StartUpPosition = 3 'Windows Default
Begin MSComDlg.CommonDialog dlgTextFile
Left = 120
Top = 2400
_ExtentX = 847
_ExtentY = 847
_Version = 393216
DialogTitle = "Text File Name"
FileName = "print.txt"
Filter = "Text Files (*.txt) | *.txt"
End
Begin VB.CommandButton cmdExit
Caption = "Exit"
Height = 375
Left = 3735
TabIndex = 3
Top = 2520
Width = 1215
End
Begin VB.CommandButton cmdCloseFile
Caption = "Close File"
Height = 375
Left = 2415
TabIndex = 2
Top = 2520
Width = 1215
End
Begin VB.CommandButton cmdOpenFile
Caption = "Open File"
Height = 375
Left = 1095
TabIndex = 1
Top = 2520
Width = 1215
End
Begin TabDlg.SSTab tabOperations
Height = 2415
Left = 0
TabIndex = 0
Top = 0
Width = 6045
_ExtentX = 10663
_ExtentY = 4260
_Version = 393216
Tabs = 4
Tab = 3
TabsPerRow = 4
TabHeight = 520
TabCaption(0) = "Create Text File"
TabPicture(0) = "Form1.frx":0000
Tab(0).ControlEnabled= 0 'False
Tab(0).Control(0)= "cmdTextFile"
Tab(0).ControlCount= 1
TabCaption(1) = "New Record"
TabPicture(1) = "Form1.frx":001C
Tab(1).ControlEnabled= 0 'False
Tab(1).Control(0)= "txtNewBalance"
Tab(1).Control(1)= "txtNewLastName"
Tab(1).Control(2)= "txtNewFirstName"
Tab(1).Control(3)= "txtNewAccount"
Tab(1).Control(4)= "cmdAddNew"
Tab(1).Control(5)= "lblNewBalance"
Tab(1).Control(6)= "lblNewLastName"
Tab(1).Control(7)= "lblNewFirstName"
Tab(1).Control(8)= "lblNewAccount"
Tab(1).ControlCount= 9
TabCaption(2) = "Update Record"
TabPicture(2) = "Form1.frx":0038
Tab(2).ControlEnabled= 0 'False
Tab(2).Control(0)= "cmdUpdate"
Tab(2).Control(1)= "txtUpdateAccount"
Tab(2).Control(2)= "txtUpdateFirstName"
Tab(2).Control(3)= "txtUpdateLastName"
Tab(2).Control(4)= "txtUpdateBalance"
Tab(2).Control(5)= "lblUpdateAccount"
Tab(2).Control(6)= "lblUpdateFirstName"
Tab(2).Control(7)= "lblUpdateLastName"
Tab(2).Control(8)= "lblUpdateBalance"
Tab(2).ControlCount= 9
TabCaption(3) = "Delete Record"
TabPicture(3) = "Form1.frx":0054
Tab(3).ControlEnabled= -1 'True
Tab(3).Control(0)= "lblDeleteAccount"
Tab(3).Control(0).Enabled= 0 'False
Tab(3).Control(1)= "txtDelete"
Tab(3).Control(1).Enabled= 0 'False
Tab(3).Control(2)= "cmdDelete"
Tab(3).Control(2).Enabled= 0 'False
Tab(3).ControlCount= 3
Begin VB.CommandButton cmdDelete
Caption = "Delete this account"
Height = 375
Left = 3000
TabIndex = 25
Top = 1170
Width = 2415
End
Begin VB.TextBox txtDelete
Height = 375
Left = 675
TabIndex = 24
Top = 1170
Width = 2295
End
Begin VB.CommandButton cmdUpdate
Caption = "Click here to update"
Height = 375
Left = -73005
TabIndex = 18
Top = 1920
Width = 3375
End
Begin VB.TextBox txtUpdateAccount
Enabled = 0 'False
Height = 285
Left = -73005
TabIndex = 17
Top = 480
Width = 3375
End
Begin VB.TextBox txtUpdateFirstName
Enabled = 0 'False
Height = 285
Left = -73005
TabIndex = 16
Top = 840
Width = 3375
End
Begin VB.TextBox txtUpdateLastName
Enabled = 0 'False
Height = 285
Left = -73005
TabIndex = 15
Top = 1200
Width = 3375
End
Begin VB.TextBox txtUpdateBalance
Enabled = 0 'False
Height = 285
Left = -73005
TabIndex = 14
Top = 1560
Width = 3375
End
Begin VB.CommandButton cmdTextFile
Caption = "Click here to specify text file name"
Height = 495
Left = -73425
TabIndex = 13
Top = 960
Width = 2895
End
Begin VB.TextBox txtNewBalance
Height = 285
Left = -73080
TabIndex = 8
Top = 1560
Width = 3375
End
Begin VB.TextBox txtNewLastName
Height = 285
Left = -73080
TabIndex = 7
Top = 1200
Width = 3375
End
Begin VB.TextBox txtNewFirstName
Height = 285
Left = -73080
TabIndex = 6
Top = 840
Width = 3375
End
Begin VB.TextBox txtNewAccount
Height = 285
Left = -73080
TabIndex = 5
Top = 480
Width = 3375
End
Begin VB.CommandButton cmdAddNew
Caption = "Add New Record"
Height = 375
Left = -73080
TabIndex = 4
Top = 1920
Width = 3375
End
Begin VB.Label lblDeleteAccount
Caption = "Enter account number to delete"
Height = 255
Left = 675
TabIndex = 23
Top = 930
Width = 2415
End
Begin VB.Label lblUpdateAccount
Alignment = 1 'Right Justify
Caption = "Account number:"
Height = 255
Left = -74325
TabIndex = 22
Top = 495
Width = 1215
End
Begin VB.Label lblUpdateFirstName
Alignment = 1 'Right Justify
Caption = "First name:"
Height = 255
Left = -74325
TabIndex = 21
Top = 855
Width = 1215
End
Begin VB.Label lblUpdateLastName
Alignment = 1 'Right Justify
Caption = "Last name:"
Height = 255
Left = -74325
TabIndex = 20
Top = 1215
Width = 1215
End
Begin VB.Label lblUpdateBalance
Alignment = 1 'Right Justify
Caption = "Balance:"
Height = 255
Left = -74325
TabIndex = 19
Top = 1575
Width = 1215
End
Begin VB.Label lblNewBalance
Alignment = 1 'Right Justify
Caption = "Balance:"
Height = 255
Left = -74400
TabIndex = 12
Top = 1575
Width = 1215
End
Begin VB.Label lblNewLastName
Alignment = 1 'Right Justify
Caption = "Last name:"
Height = 255
Left = -74400
TabIndex = 11
Top = 1215
Width = 1215
End
Begin VB.Label lblNewFirstName
Alignment = 1 'Right Justify
Caption = "First name:"
Height = 255
Left = -74400
TabIndex = 10
Top = 855
Width = 1215
End
Begin VB.Label lblNewAccount
Alignment = 1 'Right Justify
Caption = "Account number:"
Height = 255
Left = -74400
TabIndex = 9
Top = 495
Width = 1215
End
End
Begin MSComDlg.CommonDialog dlgOpen
Left = 5520
Top = 2400
_ExtentX = 847
_ExtentY = 847
_Version = 393216
DialogTitle = "Specify File to Read"
Filter = "Random-access files (*.rnd) | *.rnd"
InitDir = "c:\"
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Fig. 15.8
' 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -