How to Respond to Auto-commit Feature of VB Data Control
ID: Q129929
|
The information in this article applies to:
-
Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0
-
Microsoft Visual Basic Standard and Professional Editions for Windows, version 3.0
SUMMARY
When you perform any of the following actions on a Visual Basic Data
Control, any changes to controls bound to the Data Control are
automatically committed to the Database:
- Move off of the current record with any of the Move methods (MoveFirst,
MovePrevious, MoveNext, MoveLast, or Move).
- Move off of the current record using the VCR-like buttons of the Data
Control.
- Add a new record with the AddNew method.
- Update a record that is being edited or added with the Update method.
Note that this is not the UpdateRecord method.
- Delete the current record with the Delete method.
- Move off the current record with any of the Find methods (FindFirst,
FindNext, FindPrevious, FindLast, or Seek). This only applies to the
Table-type RecordSet object.
- Move off the current record by setting the Bookmark property.
- Close the RecordSet with the Close method.
- Unload the form that the data control is on.
This article shows how to use the Action parameter of the Data Control's
Validate event to respond to this automatic commit.
MORE INFORMATION
The Action argument passes to the Sub procedure the action taking place on
the Data Control. If you use a Select Case statement on the Action
parameter, you can choose the events to which you want to respond.
The Validate event occurs before the next record becomes active. The action
that causes the Validate to be fired can be cancelled by setting the Action
parameter to vbDataActionCancel. The saving of the data can be cancelled by
setting the Save parameter in the Validate event to False. And you can
choose not to save bound control values individually by changing the
DataChanged property (which all bindable controls have) to False for the
individual controls.
If you want to roll back all the changes of an editing session, you can
place BeginTrans and Commitrans (or Rollback) statements around your Data
Control code. However, the following example code gives you better control.
Step-by-Step Example
- Start a new project in Visual Basic. Form1 is created by default.
- Add a Data Control (Data1) and two Text Boxes (Text1 and Text2) to
Form1.
- Set the following properties of the controls:
Control Property Value
------------------------------------------
Data1 DatabaseName Biblio.mdb
Data1 RecordSource PUBLISHERS
Text1 DataSource Data1
Text1 DataField Name
Text2 DataSource Data1
Text2 DataField City
- Add the following code to the Data1_Validate event:
Sub Data1_Validate (Action As Integer, Save As Integer)
Dim RC As Integer
Debug.Print "Action "; Action; " generated the Validate event."
If Save Then
Select Case Action
Case vbDataActionFind, _
vbDataActionDelete, _
vbDataActionAddNew, _
vbDataActionCancel, _
vbDataActionClose
'No action
Case vbDataActionMoveNext, _
vbDataActionMovePrevious, _
vbDataActionMoveFirst, _
vbDataActionMoveLast, _
vbDataActionUnload
RC = MsgBox("Save changes to current record?", vbYesNo + _
vbQuestion)
If RC = vbNo Then
Save = False
Debug.Print "The data will NOT be saved."
Else
If Not IsValid() Then
' Write your own IsValid Function to return T/F value
' and do any additional validation that you want.
Action = vbDataActionCancel
End If
End If
Case Else
' This case should never be executed unless a new
' (unhandled) value is added to the Action parameter.
End Select
End If
End Sub
Function IsValid() As Boolean
IsValid = True
' Write your own validation routine here.
End Function
- Start the program by choosing Start from the Run menu or by pressing the
F5 key.
- Make changes to the Text boxes.
- Move off the current record by using the Data control buttons.
The current behavior is that you are prompted to save or not save the data.
You can easily modify this to do any internal validation checking that
you need as long as within the Validation event you do not use any of the
methods described at the beginning of this article to generate another
change in the current record.
Additional query words:
4.00 vb4win vb4all
Keywords :
Version :
Platform :
Issue type :
|