You are on page 1of 9

Chapter 9: Introduction to Working with Databases in Visual Basic

Its Your Turn Exercises Page 376


Questions 5 through 8 are designed for hands-on experience with the
material discussed in the text. Comments are added below when
appropriate.
Completing the remaining questions will enable you to create the Visual Basic
project discussed in the text.
1. Start a new project with a single form called frmMemberInfo. Add the controls
shown in Figure 9-4, including the data control to the form. (Recall that the
graphic of the video camera is found in the Graphics\Icons\Misc folder as
Camera.ico and that it goes in an image control.) Give the text box controls the
names shown in Table 9-1(txt book); name the data control as datMembers and
give it a caption of Members.

Figure 9-4

2. For the data control, the DatabaseName property should be


xx:\vintage00.mdb (U may obtained form moodle) and the RecordSource
property should be the Members table from this database. Refer below.

3. Set the DataSource property for each of the text boxes to the DatMembers
data control and set the DataField property for each text box to the appropriate
data field shown in Table 9-1(txtbk). Run your project and verify that the first
record of the database is displayed as shown in Figure 9-5. Try out each of the
VCR buttons on the data control to browse the database records.

FIG 9-5

4. Create a new Chapter9 folder on your data disk and save the form as
MemberInfo.frm and the project as Vintage9.vbp in this folder.
Note: VB files for these exercises are saved as Vintage9.vbp, MemberInfo.frm
and MemberInfo.frx.
Its Your Turn Exercises Page 380
Questions 5 through 8 are designed for hands-on experience with the
material discussed in the text. Comments are added below when
appropriate.
Completing the remaining questions will enable you to create the Visual Basic
project discussed in the text.
5. Add the navigational controls shown in Figure 9-6 along with an Exit button.
Use the names and other properties for the navigational buttons shown in Table
9-2(textbook) and cmdExit for the Exit button.

Fig 9-6
6. Add the code shown in VB Code Boxes 9-1, 9-2, and 9-3 for the
Form_Activate event procedure and for the cmdFirst and cmdLast navigational
buttons. Use the code shown in VB Code Box 9-4 for the imgNext button and the
code shown in VB Code Box 9-5 for the imgPrevious button. Finally, add the End
statement of the cmdExit button.

VB code 9-1, 9-2, 9-3,9-4 and 9-5


Option Explicit
Private Sub Form_Activate()
lblRecordNumber.Caption = Str(datMembers.Recordset.AbsolutePosition + 1)
End Sub
Private Sub cmdFirst_Click()
datMembers.Recordset.MoveFirst
lblRecordNumber.Caption = Str(datMembers.Recordset.AbsolutePosition + 1)
End Sub
Private Sub cmdLast_Click()
datMembers.Recordset.MoveLast
lblRecordNumber.Caption = Str(datMembers.Recordset.RecordCount)
End Sub
Private Sub imgNext_Click()
datMembers.Recordset.MoveNext
If Not (datMembers.Recordset.EOF) Then
lblRecordNumber.Caption = Str(datMembers.Recordset.AbsolutePosition + 1)
Else
MsgBox "Already at end of table", vbInformation
datMembers.Recordset.MoveLast
End If
End Sub
Private Sub imgPrevious_Click()
datMembers.Recordset.MovePrevious
If Not (datMembers.Recordset.BOF) Then
lblRecordNumber.Caption = Str(datMembers.Recordset.AbsolutePosition + 1)
Else
MsgBox "Already at beginning of table", vbInformation
datMembers.Recordset.MoveFirst
End If
End Sub
Private Sub cmdExit_Click()
End
End Sub

7. Run your project and try out each of the navigational buttons on the form to
browse the database records. Note that the record number changes to match the
current record. If you are sure the navigational buttons are working properly, set
the Visible property for the datMembers data control to False to make it invisible.
8. Save the form and project under their current names.
Note: VB files for these exercises are saved as Vintage9.vbp, MemberInfo.frm
and MemberInfo.frx.

Its Your Turn Exercises Page 385


Questions 6 through 14 are designed for hands-on experience with the
material discussed in the text. Comments are added below when
appropriate.
Completing the remaining questions will enable you to create the Visual Basic
project discussed in the text.
9. Add a menu bar option to the frmMemberInfo form with a caption of Member
Operations and a name of mnuMember. Add submenu options with the captions
and names shown in Table 9-4 (Textbook).

Menu bar
10. At the form level, declare blnAddingRecord as a Boolean variable and
varCurrentRecord as a String variable.

11. Add the code shown in VB Code Box 9-6 to the mnuMemberAdd event
procedure to enable the user to add records to the database.
VB Code 9-6
Private Sub mnuMemberAdd_Click()
varCurrentRecord = datMembers.Recordset.Bookmark
datMembers.Recordset.AddNew
blnAddingRecord = True
End Sub

12. Add the code shown in VB Code Box 9-7 to the mnuMemberSave event
procedure to enable the user to save changes to the database.
VB Code 9-7
Private Sub mnuMemberSave_Click()
datMembers.UpdateRecord

If blnAddingRecord Then
cmdLast_Click
blnAddingRecord = False
End If
End Sub

13. Add the code shown in VB Code Box 9-9 to the mnuMemberCancel event
procedure to enable the user to cancel changes to the database.
VB Code 9-9
Private Sub mnuMemberCancel_Click()
datMembers.UpdateControls
If blnAddingRecord Then
datMembers.Recordset.Bookmark = varCurrentRecord
blnAddingRecord = False
End If
End Sub

14. Add the code shown in VB Code Box 9-10 to the mnuMemberDelete event
procedure to enable the user to delete a record from the database.
Vb code 9-10
Private Sub mnuMemberDelete_Click()
Const strdelete As String = "Are you sure that you want to delete this record?"
Dim intresponse As Integer
intresponse = MsgBox(strdelete, vbYesNoCancel + vbCritical + vbDefaultButton2, "Delete
Record")
If intresponse = vbYes Then
datMembers.Recordset.Delete
datMembers.Recordset.MoveNext
If datMembers.Recordset.EOF Then
cmdLast_Click
End If
End If
End Sub

15. Run your project and add the new member discussed in the text (John Lister,
with phone number of 770-555-2579, residing at 292 Ashford Way in Bogart, GA
30622). Use the Save Member option to save this new record.
16. Edit the record for Janice Mullins to change her phone number to be 706555-0778 instead of 706-555-0777. Move to another record and then move back
to see that the change has been saved.
17. Save your form and project under the same name.
Note: VB files for these exercises are saved as Vintage9.vbp, MemberInfo.frm
and MemberInfo.frx.

Its Your Turn Exercises Page 390


Questions 6 through 12 are designed for hands-on experience with the
material discussed in the text. Comments are added below when
appropriate.
Completing the remaining questions will enable you to create the Visual Basic
project discussed in the text.
18. Add a list box and command button to the frmMemberInfo form in the lower
right-hand area of the form as shown in Figure 9-8. Name the list box lstLateFees
and the command button cmdFind. Add a caption of Find Late Fees to the
command button.

Figure 9-8
19. Add the code shown in VB Code Box 9-11 to the cmdFind event procedure.
Run your project and click the cmdFind command button. You should see the
same output as shown in Figure 9-8.

VB Code 9-11
Private Sub cmdFind_Click()
Dim strQueryString As String
Dim strLateFees As String
Dim varBookMark As Variant
strLateFees = InputBox("What is the late fees cutoff?")
varBookMark = datMembers.Recordset.Bookmark
strQueryString = "Late_Fees > " & strLateFees
lstLateFees.Clear
datMembers.Recordset.FindFirst strQueryString
Do Until datMembers.Recordset.NoMatch
lstLateFees.AddItem datMembers.Recordset("Name") & _
" " & Format(datMembers.Recordset("Late_Fees"), _
"currency")
datMembers.Recordset.FindNext strQueryString
Loop
datMembers.Recordset.Bookmark = varBookMark
End Sub

20. Add a mnuFind menu bar option with a caption of Find to the form. Add three
submenu options with the properties shown in Table 9-5.

Menu Find
21. Copy and paste the code from the cmdFind event procedure into the
mnuFindLateFees and mnuFindPhoneNumber event procedures you just
created. Modify the code for the mnuFindLateFees event procedure to match that
shown in VB Code Box 9-12. Finally, add the instruction:
lstLateFees.Clear
to the mnuFindClear event procedure.
VB Code 9-12:
Private Sub mnuFindClear_Click()
lstLateFees.Clear
End Sub
Private Sub mnuFindLateFees_Click()
Dim strQueryString As String
Dim strLateFees As String
Dim varBookMark As Variant
strLateFees = InputBox("What is the late fees cutoff?")
varBookMark = datMembers.Recordset.Bookmark
strQueryString = "Late_Fees > " & strLateFees
lstLateFees.Clear
datMembers.Recordset.FindFirst strQueryString

Do Until datMembers.Recordset.NoMatch
lstLateFees.AddItem datMembers.Recordset("Name") & _
" " & Format(datMembers.Recordset("Late_Fees"), _
"currency")
datMembers.Recordset.FindNext strQueryString
Loop
datMembers.Recordset.Bookmark = varBookMark
End Sub
Private Sub mnuFindPhoneNumber_Click()
Const strApos As String = "'"
Dim strQueryString As String
Dim strPhoneNum As String
strPhoneNum = InputBox("What is the Phone Number?")
strQueryString = "Phone_Number = " & strApos & strPhoneNum & strApos
lstLateFees.Clear
datMembers.Recordset.FindFirst strQueryString
If datMembers.Recordset.NoMatch Then
MsgBox "No match for this phone number", vbExclamation
End If
lblRecordNumber.Caption = Str(datMembers.Recordset.AbsolutePosition + 1)
End Sub

10. Run your procedure and test this submenu option by entering a telephone
number of 770-555-1294. Lonnie Stams name, late fees ($2.12), and other
information will be displayed in the text boxes. Now enter a telephone number of
706-555-1294. A message that there is no match for this phone number should
be displayed. Finally, enter another phone number for a member who is in the
database, and verify that the correct name and late fees are displayed.
11. Save your project and form under the current names.
Note: VB files for these exercises are saved as Vintage9.vbp, MemberInfo.frm
and MemberInfo.frx.

You might also like