19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

A Case Study: Recording Checks <strong>and</strong> Deposits 235<br />

Private Sub cmdRecord_Click()<br />

Dim amt As String, amount As Single, itemNum As Integer<br />

‘Check to ensure all required fields are filled<br />

If AllDataGiven Then<br />

amt = txtAmount.Text ‘Amount of transaction as string<br />

amount = Val(amt) ‘Amount of transaction as number<br />

amt = FormatCurrency(amt) itemNum = Val(txtNum.Text)<br />

If transType = “Check” Then<br />

curBal = curBal - amount<br />

lastCkNum = itemNum<br />

Else ‘transType = “Deposit”<br />

curBal = curBal + amount<br />

lastDpNum = itemNum<br />

End If<br />

lblBalance.Caption = FormatCurrency(curBal)<br />

Open fileName For Append As #1<br />

Write #1, transType, txtToFrom.Text, curBal, lastCkNum, lastDpNum, amt, _<br />

txtMemo.Text, txtDate.Text<br />

Close #1<br />

Call InitializeFields<br />

txtToFrom.SetFocus<br />

End If<br />

End Sub<br />

Private Sub CreateDataFile()<br />

Dim startBal As Single, ckNum As integer<br />

‘The first time the program is run, create a data file<br />

Open fileName For Output As #1<br />

nameOnChk = InputBox(“Name to appear on checks:”)<br />

startBal = Val(InputBox(“Starting balance:”))<br />

ckNum = Val(InputBox(“Number of the first check:”))<br />

lastCkNum = ckNum - 1 ‘Number of “last” check written<br />

ckNum = Val(InputBox(“Number of the first deposit slip:”))<br />

lastDpNum = ckNum - 1 ‘Number of “last” deposit slip processed<br />

curBal = startBal ‘Set current balance<br />

‘First record in data file records name to appear on checks<br />

‘plus initial data for account<br />

Write #1, nameOnChk, startBal, lastCkNum, lastDpNum<br />

Close #1<br />

End Sub<br />

Private Sub Form_Load()<br />

Dim drive As String, today As String<br />

‘If no data file exists, create one. Otherwise, open the<br />

‘data file <strong>and</strong> get the user’s name, last used check <strong>and</strong><br />

‘deposit slip numbers, <strong>and</strong> current balance.<br />

‘In next line adjust drive as necessary drive = “A:”<br />

‘Drive (or path) for data file<br />

fileName = drive & “CHKBOOK.TXT” ‘Program uses one data file<br />

If Dir(fileName) = “” Then<br />

‘Data file does not exist, so create it <strong>and</strong> obtain initial data<br />

Call CreateDataFile<br />

Else<br />

Call ReadDataFile<br />

End If

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!