Database Design:
The first step is to design a database using Data Manager (or Access). My database is
a single table (named MYSTUFF). Its specifications are:
This database is saved as file HomeInv.mdb. Create a data link to your database. The
link is saved as HomeInv.udl.
Report Design:
The second step is to use the Data Environment and Data Report designers to setup
how you want the printed home inventory to appear. Use your discretion here. My
final report design is saved in denHomeInv and rptHomeInv. We will access this
report from our Visual Basic application. My Data Report design looks like this:
Project Design:
Form:
Properties:
Form frmHome:
BorderStyle = 1 - Fixed Single
Caption = Home Inventory
CommandButton cmdExit:
Caption = E&xit
ADO Data Control dtaHome:
Caption = Book Titles
ConnectionString = HomeInv.udl (in whatever folder you saved it in -
select, don’t type)
RecordSource = SELECT * FROM MyStuff
Visible = False
CommandButton cmdShow:
Caption = Show &Report
CommandButton cmdPrevious:
Caption = &Previous Item
CommandButton cmdNext:
Caption = &Next Item
CommandButton cmdDelete:
Caption = &Delete Item
CommandButton cmdAdd:
Caption = &Add Item
TextBox txtLocation:
DataField = Location
DataSource = dtaHome
FontName = MS Sans Serif
FontSize = 9.75
MaxLength = 40
TextBox txtValue:
DataField = New Value
DataSource = dtaHome
FontName = MS Sans Serif
FontSize = 9.75
TextBox txtDate:
DataField = Date Purchased
DataSource = dtaHome
FontName = MS Sans Serif
FontSize = 9.75
MaxLength = 20
TextBox txtSerial:
DataField = Serial Number
DataSource = dtaHome
FontName = MS Sans Serif
FontSize = 9.75
MaxLength = 20
TextBox txtItem:
DataField = Item
DataSource = dtaHome
FontName = MS Sans Serif
FontSize = 9.75
MaxLength = 40
Label Label5:
Caption = Location
FontName = Times New Roman
FontSize = 12
Label Label4:
Caption = New Value
FontName = Times New Roman
FontSize = 12
Label Label3:
Caption = Purchase Date
FontName = Times New Roman
FontSize = 12
Label Label2:
Caption = Serial Number
FontName = Times New Roman
FontSize = 12
Label Label1:
Caption = Item
FontName = Times New Roman
FontSize = 12
Code:
General Declarations:
Option Explicit
cmdAdd Click Event:
Private Sub cmdAdd_Click()
'Add new item to database
dtaHome.Recordset.AddNew
txtItem.SetFocus
End Sub
cmdDelete Click Event:
Private Sub cmdDelete_Click()
'Delete item from database
Dim Rvalue As Integer
Rvalue = MsgBox("Are you sure you want to delete this
item?", vbQuestion + vbYesNo, "Delete Item")
If Rvalue = vbNo Then Exit Sub
dtaHome.Recordset.Delete
dtaHome.Recordset.MoveNext
If dtaHome.Recordset.EOF Then
If dtaHome.Recordset.BOF Then
MsgBox "You must add an item.", vbOKOnly + vbInformation,
"Empty Database"
Call cmdAdd_Click
Else
dtaHome.Recordset.MoveFirst
End If
End If
txtItem.SetFocus
End Sub
cmdExit Click Event:
Private Sub cmdExit_Click()
End
End Sub
cmdNext Click Event:
Private Sub cmdNext_Click()
'Move to next item - if at end-of-file, backup one item
dtaHome.Recordset.MoveNext
If dtaHome.Recordset.EOF Then
dtaHome.Recordset.MovePrevious
txtItem.SetFocus
End Sub
cmdPrevious Click Event:
Private Sub cmdPrevious_Click()
'Move to previous item - if at beginning-of-file, go down
one item
dtaHome.Recordset.MovePrevious
If dtaHome.Recordset.BOF Then dtaHome.Recordset.MoveNext
txtItem.SetFocus
End Sub
cmdShow Click Event:
Private Sub cmdShow_Click()
rptHomeInv.Show
End Sub