Académique Documents
Professionnel Documents
Culture Documents
STEPS:
1. Open a new Visual Basic project.
2. Put a data control (an intrinsic control, located in the VB toolbox) on the form and set
the properties as follows:
Property
(Name)
Caption
Connect
DatabaseName
DefaultType
RecordSource
Value
datAuthors
Use the arrows to view the data
Access (default)
..\biblio.mdb
UseJet (default)
Authors (choose from list)
Notes: When you use the Data Control in a project, the properties that must be
set areDatabaseName and RecordSource, in that order. DatabaseName is the
name of the database you want to use, and the RecordSource is the name of
the table in that database that you want to use.
3. On your form, create a text box for each field in the Authors table, with labels. (If you
were to open the database in Access, you would see that the three fields of the
Authors table are Au_ID, Author, and Year Born.) Set the properties of the three
textboxes as follows:
Name
txtAuthID
txtAuthor
txtYearBorn
DataSource
datAuthors
datAuthors
datAuthors
DataField
Au_ID
Author
Year Born
configured), and the DataField is the name of the particular field in the database
that should be displayed in the control (this field will be in the table that was
chosen for the RecordSource of the data control).
At this point, your form should resemble the screen-shot below:
4. Save and run the project. Use the arrows on the data control to scroll through the
data.
5. On any record, change the data in the author name or year born field. Move ahead,
then move back to the record you changed. Note that your changes remain in
effect. The data control automatically updates a record when you move off of the
record.
Note that this exercise demonstrated that you can create a simple but functional application that allows
the user to browse through the rows of a database table (or result set) and to update rows in that table
without writing any code.
EXERCISE 2
Using Navigation Buttons with a Data Control
In the previous exercise, you saw that by clicking specific buttons of the data control,
you could move to the first, previous, next, or last record. What is happening is that the
data control is automatically invoking specific methods of the recordset object: namely
the MoveFirst, MovePrevious, MoveNext, and MoveLast methods. You can also
invoke these methods through code, which is what this exercise demonstrates.
STEPS:
1.
Copy the files from Exercise #1 into a new folder and open the VBP file in the
new folder.
2. Set the Visible property of the data control (datAuthors) to False.
3. Make four command buttons with the following properties:
Name
cmdMoveNext
cmdMoveLast
cmdMovePrevious
cmdMoveFirst
Caption
Next Record
Last Record
Previous Record
First Record
4. Put the following four lines of code in the appropriate Click events for the buttons:
Event
cmdMoveNext_Click
cmdMoveLast_Click
cmdMovePrevious_Click
cmdMoveFirst_Click
Code
datAuthors.Recordset.MoveNext
datAuthors.Recordset.MoveLast
datAuthors.Recordset.MovePrevious
datAuthors.Recordset.MoveFirst
Note: EOF (End Of File) is a Boolean property of the recordset object that becomes true
when an attempt is made to move forward past the last record in a recordset.
BOF (Beginning Of File) is a Boolean property of the recordset object that becomes true
when an attempt is made to move backward past the first record in a recordset.
STEPS:
1. Copy the files from Exercise #1 into a new folder and open the VBP file in the new
folder.
2.
Click once on the data control and make sure that the following properties are
set:
Note that this exercise demonstrated that you can create a simple but functional application that not only
allows the user to browse through the rows of a database table and to update rows in that table, but also
allows the user to add a new record to that table without writing any code.
EXERCISE 4
Using the EOF and BOF Properties with Navigation Buttons
STEPS:
1. Copy the files from Exercise #2 into a new folder and open the VBP file in the new
folder.
2. When the user clicks on the MoveNext button, and there is no next record, your code
should stay on the same record (the last one).
Put the following code in the cmdMoveNext_Click() event:
datAuthors.Recordset.MoveNext
If datAuthors.Recordset.EOF = True Then
datAuthors.Recordset.MoveLast
End If
FYI: Instead of Recordset.MoveLast, you could use MoveFirst to let the user
loop around to the first record.
3. Put similar code in the cmdMovePrevious_Click() event. In this case, you will be
checking forRecordset.BOF = True.
4. Save and run the project and test it thoroughly.
EXERCISE 5
Adding, Updating and Deleting Records
In previous exercises, you saw that with the data control, changes to a record are
automatically updated when the user moves off of that record this is
the Update method of the recordset object of the data control at work. You also saw
that, by setting the EOFAction of the data control to "2 AddNew", the data control
will invoke the AddNew method of the recordset object, which causes all the bound
controls to be cleared so that the user can enter data. In addition to being invoked
automatically through the data control, the Update and AddNew methods can also be
invoked through code. The recordset object also has a Delete method, which can only
be invoked through code it cannot be accessed automatically through the data
control.
This exercise shows you how to invoke the Update, AddNew, and Delete methods of the
recordset object through code.
STEPS:
1. Copy the Exercise #4 files into a new folder and open the VBP file.
2. Add three more buttons to the form and set their properties as follows:
Name
cmdNewRecord
cmdSaveRecord
cmdDeleteRecord
Caption
New Record
Save Record
Delete Record
Enabled
True
False
True
3.
When the user clicks on New Record, your program should enable the Save
Data button and disable the others. Put the following code in
the cmdNewRecord_Click() event:
datAuthors.Recordset.AddNew
cmdSaveRecord.Enabled = True
cmdMoveFirst.Enabled = False
cmdMoveLast.Enabled = False
cmdMovePrevious.Enabled = False
cmdMoveNext.Enabled = False
cmdDeleteRecord.Enabled = False
cmdNewRecord.Enabled = False
4. Save and run the program to make sure the buttons are enabled and disabled
correctly.
5. When the user clicks on the Save button to save the data that was entered, the
Update method should be called and the buttons should be redisplayed. Place
the following code in thecmdSaveRecord_Click() event:
datAuthors.Recordset.Update
cmdSaveRecord.Enabled = False
cmdMoveFirst.Enabled = True
cmdMoveLast.Enabled = True
cmdMovePrevious.Enabled = True
cmdMoveNext.Enabled = True
cmdDeleteRecord.Enabled = True
cmdNewRecord.Enabled = True
6.
Something to watch out for with the Delete method is that when a record is
deleted, the recordset is no longer pointing to a valid record, but the data from
the deleted record still remains in the controls. If the user attempted to update the
record at that point, a run-time error would occur. To prevent this from happening,
you should move the user to a valid record immediately following the delete.
Another issue is that if you attempt to delete a record that has a related record in
another table, the Jet (Access) database engine will not allow the delete, and a
run-time error will occur. If you don't trap the error, the program will crash.
Finally, it is good programming practice to ask the user to confirm any destructive
action.
Place the following code, which addresses the above-mentioned issues, in
thecmdDelete_Click() event:
On Error GoTo Delete_Error
If MsgBox("Are you sure you want to delete this record?", _
vbQuestion + vbYesNo + vbDefaultButton2, _
"Confirm") = vbNo Then
Exit Sub
End If
'delete the current record
datAuthors.Recordset.Delete
'move to a valid record
cmdMoveNext_Click
Exit Sub
Delete_Error:
This exercise will add the following functionality to the form you created in Exercise 5:
Validating the user's input when they update an existing record or add a new
record to the database.
An Undo feature which will enable the user to cancel changes that they make to a
record.
This exercise introduces the Validate event of the data control, as well as the
CancelUpdate and UpdateControls methods of the data control, the Bookmark and
LastModified properties of the recordset object, and the DataChanged property of
bound controls.
The Validate event of the data control occurs prior to a record Move and prior to an
Update, Delete, Unload, Close, or setting of a Bookmark. This means that any code
contained in the Validate event procedure will be executed prior to the execution of a
statement containing one of these methods. For example, if somewhere in your
program you code the statement datAuthors.Recordset.Update, when VB encounters
this statement, any code in the Validate event will be executed first.
The Validate event takes in two VB-provided arguments: Action and Save. (For
example, the Validate event procedure header for this exercise will look like this:
Private Sub datAuthors_Validate(Action As Integer, Save As Integer).
The Action argument tells you which particular action (MoveFirst, Update, etc.) caused
the Validate event to fire. The value of Action can be tested by comparing it to a literal
Value
0
1
2
3
4
5
6
7
8
9
10
11
Description
Cancel the operation when the Sub exits
MoveFirst method
MovePrevious method
MoveNext method
MoveLast method
AddNew method
Update operation (not UpdateRecord)
Delete method
Find method
The Bookmark property has been set
The Close method
The form is being unloaded
The Save argument is a Boolean value indicating whether or not bound data has
changed. You can set Save to False to prevent VB from saving changes to a record.
DataChanged is a run-time only Boolean property available with data-bound controls
(such as the textboxes you have been using), typically used in the Validate event. You
would typically use DataChanged as a first test to see if a particular field needs further
validation (if the user did not change the data in a field, why bother doing further
validation on that field?) The logic structure would look something like the following:
If txtMyField.DataChanged Then
and hasn't yet invoked the Update method, CancelUpdate cancels any changes made
after Edit or AddNew was invoked.
The UpdateControls method gets the current record from a Data control's Recordset
object and displays the appropriate data in controls bound to a Data control. This
method is typically used to restore the contents of bound controls to their original
values, as when a user makes changes to data and then decides to cancel the
changes. This method does not cause the Validate event to fire. Similarly,
the UpdateRecord method (not used in this exercise) saves the current contents of
bound controls to the database during the Validate event without triggering the Validate
event again.
Bookmark is a property of the Recordset object that contains a binary string identifying
the current record. If you assign the Bookmark value to a variable and then move to
another record, you can make the earlier record current again by setting the Bookmark
property to that string variable.
LastModified is a property of the Recordset object that returns a bookmark indicating
the most recently added or changed record.
STEPS:
1.
2.
Caption
Enabled
cmdUndo
Undo
True
cmdCancelNew
True
3.
Place the following statements in the general declarations section of your form:
Code the event procedure for the new cmdUndo button, which consists of only
one statement:
datAuthors.UpdateControls
5. Modify the cmdNewRecord_Click() event procedure to look like the following (new statements are
shown in bold):
datAuthors.Recordset.AddNew
If blnValidationError Then Exit Sub
cmdSaveRecord.Enabled = True
cmdCancelNew.Enabled = True
cmdMoveFirst.Enabled = False
cmdMoveLast.Enabled = False
cmdMovePrevious.Enabled = False
cmdMoveNext.Enabled = False
cmdDeleteRecord.Enabled = False
cmdNewRecord.Enabled = False
cmdUndo.Enabled = False
blnAddingNewRecord = True
Explanation
When the user initiates the process to add a record, the code invokes the AddNew method, which
will cause the Validate event to fire, which will set the blnValidationError flag. This will catch the
case where the user has modified the current record (and has made errors) and then clicks the
"New Record" button. In that case the error is flagged, the AddNew is canceled, and the user
must correct the problem with the current record.
If everything's OK, we enable the "Cancel New" button so that they can cancel the process, and
disable the "Undo" button, because that is only applicable when the user is changing, not adding
a record. We also set the blnAddingNewRecord flag to true, which will be tested in the Validate
event (shown in a few steps below).
6. Modify the cmdSaveRecord_Click() event procedure to look like the following (new statements are
shown in bold):
datAuthors.Recordset.Update
If blnValidationError Then Exit Sub
' make the new record the current record
datAuthors.Recordset.Bookmark _
= datAuthors.Recordset.LastModified
cmdSaveRecord.Enabled = False
cmdCancelNew.Enabled = False
cmdMoveFirst.Enabled = True
cmdMoveLast.Enabled = True
cmdMovePrevious.Enabled = True
cmdMoveNext.Enabled = True
cmdDeleteRecord.Enabled = True
cmdNewRecord.Enabled = True
cmdUndo.Enabled = True
blnAddingNewRecord = False
Explanation
When the user initiates a save for a newly added record, the Update method is invoked (with the
statementdatAuthors.Recordset.Update). This will cause the Validate event to fire, where we
will set theblnValidationError flag. When the Validate event terminates, control resumes with the
If statement, where that flag is tested. If there is an error, we want to exit the sub early, which will
let the user continue working on the current record to correct the problem. Otherwise, if the flag is
False, that means that all validation checks passed and we can continue on our merry way.
The statement
datAuthors.Recordset.Bookmark =
datAuthors.Recordset.LastModified
causes the newly added record to become the current record. This is necessary if you
want to see the data for the new record on the form after you add it, because the
AddNew method and subsequent Update method does not cause the newly
added record to become the "current" record. Therefore, without this statement,
the record that was current before you invoked the "New Record" operation
would be displayed, and you would have to go to the last record to see the newly
added record.
Since this event completes the operation of adding a new record, the cmdCancelNew
button is disabled, the cmdUndo button is enabled and the blnAddingNewRecord
flag is set to False.
7. Code the Validate event procedure for the datAuthors data control. Recall that this
event fires prior to a record Move and prior to an Update, Delete, Unload, Close,
or setting of a Bookmark. Since Validate is the default procedure for a data
control, you can get the procedure header by double-clicking on the data control.
The code is shown below:
Private Sub datAuthors_Validate(Action As Integer, Save As
Integer)
If Action = vbDataActionBookmark Then Exit Sub
cmdSaveRecord.Enabled = False
cmdCancelNew.Enabled = False
cmdMoveFirst.Enabled = True
cmdMoveLast.Enabled = True
cmdMovePrevious.Enabled = True
cmdMoveNext.Enabled = True
cmdDeleteRecord.Enabled = True
cmdNewRecord.Enabled = True
cmdUndo.Enabled = True
blnAddingNewRecord = False
End Sub
9. Save, run and test the program. Make sure you understand what the code is doing.
EXERCISE 7
Using the Find Methods
The Recordset object has methods FindFirst, FindLast, FindNext, and FindPrevious.
You can use these to search for a particular record in the Recordset.
The syntax is
datControl.Recordset.FindFirst criteria
where criteria is a string item consisting of a field name, a relational (comparison)
operator, and a value. It is essentially the same as a SQL WHERE clause without the
word WHERE. The comparison operators that can be used
are =, >, <, >=, <=, <>, Like, Between, and In. The value on the right-hand side of the
comparison operator must conform to the following rules:
string values must be enclosed in single quotes
numeric values are not enclosed in quotes
date values must be enclosed in #'s (pound signs)
If the criteria is expressed in a literal string, that string must be enclosed in double quotes. Typically, you
must use VB's string-handling functions (especially the "&" for concatenation) to get the desired results.
Examples:
datBooks.Recordset.FindFirst "ISBN = '123-456-789-0' "
datMembers.Recordset.FindNext "Amount > 100"
In this exercise, the user selects both the field name and the relational operator from combo boxes, then
enters the search value in a textbox. The criteria for the Find methods are thus formed by statements
similar to the following:
Additional Notes:
If the name of the field in the database table has spaces in its name, you must put
square brackets around the field name, as in the following example:
datBooks.Recordset.FindFirst "[Pay Rate] > 30000"
For string values, if there is the possibility that the search string will contain an apostrophe, an extra
measure should be taken to "double" the apostrophes in the string otherwise, the apostrophe
embedded in the string will be interpreted as the end of the string and a syntax error will most likely
result. The easiest way to provide this "insurance" against embedded apostrophes is to use the
Replace$ function on the string in question to replace any occurrences of a single apostrophe with
two apostrophes:
datProducts.Recordset.FindFirst _
"ProductName = '" & Replace$(strSearchText, "'", "''") & "'"
For example, if strSearchText contained "Chef Anton's Cajun Gumbo", the criteria in
the above statement would evaluate to
ProductName = 'Chef Anton''s Cajun Gumbo'
and the double apostrophe in "Anton''s" would be correctly interpreted by the SQL
parser as a single apostrophe.
In this particular example, if the Replace function was NOT used (i.e., you simply
coded
"ProductName = '" & strSearchText & "'"
for the criteria, the result would be
ProductName = 'Chef Anton's Cajun Gumbo'
which would result in an error: the SQL parser would interpret the criteria to be "Chef
Anton" with extraneous characters ("s Cajun Gumbo") at the end.
The Recordset has a NoMatch property. It is set to False to begin with. If you use a
Find method and a record is not found, then the NoMatch property is set to True. You
should use this property to determine whether or not a record was found. If a match is
found, NoMatch will be set to True, and the found record becomes the current record.
STEPS:
1.
Copy the files from Exercise 6 into a new folder. Follow steps 2 through 4 to get
your form to resemble the following:
2. Add four command buttons to the form and set their properties as follows:
Name
Caption
Enable
cmdFind
F&ind
True
cmdFindAgain
Find &Again
False
cmdCancelFind
Cance&l Find
False
cmdGo
&Go!
False
3. Add two combo boxes and set their properties as follows (Note: you can set the List
property of a listbox or combo box in the Properties box by typing in values and
pressing Ctrl+Enter - this action is analogous to using the AddItem method in
code).
Name
cboField
Style
2 - Dropdown List
Enabled
False
cboRelOp
2 - Dropdown List
False
List
Au_Id
Author
[Year Born]
=
>
<
>=
<=
<>
Like
4. Add a textbox called txtCriteria and set its Enabled property to False.
5. Place the following statements in the Form_Load event:
cboField.Text = cboField.List(0)
cboRelOp.Text = cboRelOp.List(0)
6. Code the Click events for the combo boxes (to provide smoother navigation for the user). The Enabled
tests are necessary because the Form_Load event assigns the first element of the their
respective List property arrays to their respective text properties, which automatically invokes the
Click event, which will normally perform a SetFocus. SetFocus cannot be invoked on a disabled
control; nor can it be invoked prior to completion of a Form Load.
strCriteria = _
cboField.Text & " " & cboRelOp.Text & _
" " & strQuote & txtCriteria & strQuote
With datAuthors.Recordset
If blnFirstFind Then
blnFirstFind = False
.FindFirst strCriteria
Else
.FindNext strCriteria
End If
If .NoMatch Then
MsgBox "Data not found"
.Bookmark = strBookmark
cmdFind_Click
cmdFindAgain.Enabled = False
Else
cmdFindAgain.Enabled = True
cboField.Enabled = False
cboRelOp.Enabled = False
txtCriteria.Enabled = False
cmdGo.Enabled = False
End If
End With
10. In the cmdFindAgain_Click event, place the following line (performs the same
actions as the cmdGo_Click event):
cmdGo_Click
11. Place the following code in the cmdCancelFind_Click() event (enables the
navigation and updating controls, disables the Find controls):
txtAuthor.Enabled = True
txtYearBorn.Enabled = True
cmdMoveFirst.Enabled = True
cmdMoveLast.Enabled = True
cmdMovePrevious.Enabled = True
cmdMoveNext.Enabled = True
cmdDeleteRecord.Enabled = True
cmdNewRecord.Enabled = True
cmdUndo.Enabled = True
cmdSaveRecord.Enabled = True
cmdCancelNew.Enabled = True
cmdFind.Enabled = True
cmdCancelFind.Enabled = False
cmdGo.Enabled = False
cboField.Enabled = False
cboRelOp.Enabled = False
txtCriteria.Enabled = False
12. Run the program and test the Find operations using different fields, relational operators, and
search strings.
EXERCISE 8
Using the MSFlexGrid Control
The MSFlexGrid can be used to display multiple records in a grid. It cannot be used to
edit data or add and delete records. Follow the steps in this exercise to create a
Categories and Products master/detail form similar to the screen-shot shown below:
STEPS:
1. Place two data controls on the form and set their properties as follows:
Name
datCategories
datProducts
DatabaseName
..\NWIND.MDB
..\NWIND.MDB
RecordSource
Categories
(leave blank)
Visible
True
False
Set the Caption property of datCategories to "Use Arrow Buttons to Navigate Records".
2. Place three textboxes and one OLE control on the form. Set the DataSource property for each of these
four controls todatCategories.
Set the DataField property for these controls to CategoryID, CategoryName, Description,
and Picturerespectively.
Set the Enabled property of the Category ID textbox to False. For the Description text box, set its
MultiLine property to True and set its ScrollBars property to 2 Vertical.
Add appropriate label controls to accompany the textboxes, and group these four controls into a
frame, as shown in the screen-shot.
3. If necessary, add the MSFlexGrid to your toolbox from the Components item of the Project menu. Place
an MSFlexGrid control on your form, and set its properties as follows:
Property
AllowUserResizing
DataSource
FixedCols
Value
1 flexResizeColumns
datProducts
0
4.
This will force the controls bound to datCategories to be populated sooner than they would otherwise, and
will also trigger the Reposition event of the datCategories control. (The Reposition event of a data
control occurs whenever a Move method is executed against that control.)
5.
datProducts.RecordSource _
= "SELECT * FROM Products " _
& "WHERE CategoryID = " & txtCatID
datProducts.Refresh
EXERCISE 9
Using the DBGrid Control
This exercise uses the DBGrid control, which goes a step further than the MSFlexGrid
in that allows the user to add, update, and delete records.
The DBGrid control does not install automatically when you install Visual Basic 6.0, but
it is available on the VB 6/VS 6 CD. To install the DBGrid control, perform the following
steps.
Property
1.
2.
Copy your files from Exercise 8 to a new folder and open the project.
Delete the MSFlexGrid control.
3.
Out of Project | Components, include the Microsoft Data Bound Grid Control.
4.
Place a DBGrid on your form in the position previously occupied by the flex grid.
5.
AllowAddNew
AllowDelete
AllowUpdate
DataSource
6.
True
True
True
datProducts
The code can remain as is. Save and run the program; experiment by using the
grid to add, update, and delete records. While this application is certainly not
bullet-proof, you'll find that built-in error-checking will prevent most "illegal"
actions attempted by the user (such as trying to change the primary key,
changing a foreign key to a non-existent value, or deleting a record with a related
record in another table).