Dim WithEvents adoPrimaryRS As Recordset
Dim mbChangedByCode As Boolean
Dim mvBookMark As Variant
Dim mbEditFlag As Boolean
Dim mbAddNewFlag As Boolean
Dim mbDataChanged As Boolean
(Note 1) A connection object is created identified as the variable db.
The connection object db is defined as a Microsoft.Jet.OLEDB.3.51
Data Provider. Next, an ADO recordset is open
using an sql that defines data to go into the
grid. And Last, the grids DataSource is set
to the ADO recordset.
Private Sub Form_Load()
Dim db As Connection
Set db = New Connection
db.CursorLocation = adUseClient
db.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=c:\path\aDB.mdb;"
Set adoPrimaryRS = New Recordset
adoPrimaryRS.Open "select Field1,Field2, FieldN from aTable", db, adOpenStatic, adLockOptimistic
Set grdDataGrid.DataSource = adoPrimaryRS 'Binding a Grid to a Recordset
mbDataChanged = False
End Sub
(Note 2) Capture the keystrokes from the keybroad and map to the command key events.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If mbEditFlag Or mbAddNewFlag Then Exit Sub
Select Case KeyCode
Case vbKeyEscape
cmdClose_Click
Case vbKeyEnd
cmdLast_Click
Case vbKeyHome
cmdFirst_Click
Case vbKeyUp, vbKeyPageUp
If Shift = vbCtrlMask Then
cmdFirst_Click
Else
cmdPrevious_Click
End If
Case vbKeyDown, vbKeyPageDown
If Shift = vbCtrlMask Then
cmdLast_Click
Else
cmdNext_Click
End If
End Select
End Sub
Private Sub adoPrimaryRS_MoveComplete(ByVal adReason As ADODB.EventReasonEnum, _
ByVal pError As ADODB.Error,_
adStatus As ADODB.EventStatusEnum, _
ByVal pRecordset As ADODB.Recordset)
'This will display the current record position for this recordset
lblStatus.Caption = "Record: " & CStr(adoPrimaryRS.AbsolutePosition)
End Sub
Private Sub adoPrimaryRS_WillChangeRecord(ByVal adReason As ADODB.EventReasonEnum, _
ByVal cRecords As Long,_
adStatus As ADODB.EventStatusEnum,_
ByVal pRecordset As ADODB.Recordset)
'This is where you put validation code
'This event gets called when the following actions occur
Dim bCancel As Boolean
Select Case adReason
Case adRsnAddNew
Case adRsnClose
Case adRsnDelete
Case adRsnFirstChange
Case adRsnMove
Case adRsnRequery
Case adRsnResynch
Case adRsnUndoAddNew
Case adRsnUndoDelete
Case adRsnUndoUpdate
Case adRsnUpdate
End Select
If bCancel Then adStatus = adStatusCancel
End Sub
(Note 3) The MoveLast method of the ADO Recordset
moves the cursor to the End of File and positions
the Grid Row on the last record.
The AddNew methods
forces the grid into the empty row and allows the user
to enter in data for the new row.
Private Sub cmdAdd_Click()
On Error GoTo AddErr
adoPrimaryRS.MoveLast
adoPrimaryRS.AddNew
grdDataGrid.SetFocus
Exit Sub
AddErr:
MsgBox Err.Description
End Sub
(Note 4) The Delete removes the current record.
The delete record remains current. You can't modify
or edit the delete record. Therefore, the movenext
method is required to move past the deleted record.
If the current record is EOF then move to the
Last record of the recordset Keeping the
cursor on a editable record.
Private Sub cmdDelete_Click()
On Error GoTo DeleteErr
With adoPrimaryRS
.Delete
.MoveNext
If .EOF Then .MoveLast
End With
Exit Sub
DeleteErr:
MsgBox Err.Description
End Sub
(Note 5) Setting the DataSource to Nothing
resets the recordset. Requery updates the
recordset by re-executing the query.
Last, bind the grid's Data Source to the recordset.
Private Sub cmdRefresh_Click()
'This is only needed for multi user apps
On Error GoTo RefreshErr
Set grdDataGrid.DataSource = Nothing
adoPrimaryRS.Requery
Set grdDataGrid.DataSource = adoPrimaryRS
Exit Sub
RefreshErr:
MsgBox Err.Description
End Sub
(Note 6) Make the Add Command Button invisible and
the Update Button Visible. The Grid will detect
changes and pass the changes to the recordset.
Private Sub cmdEdit_Click()
On Error GoTo EditErr
lblStatus.Caption = "Edit record"
mbEditFlag = True
SetButtons False
Exit Sub
EditErr:
MsgBox Err.Description
End Sub
Private Sub cmdCancel_Click()
On Error Resume Next
SetButtons True
mbEditFlag = False
mbAddNewFlag = False
adoPrimaryRS.CancelUpdate
If mvBookMark > 0 Then
adoPrimaryRS.Bookmark = mvBookMark
Else
adoPrimaryRS.MoveFirst
End If
mbDataChanged = False
End Sub
(Note 7) Make the Add Command Button visible and
the Update Button invisible. Submit all changes
to the recordset in batch to the database.
Private Sub cmdUpdate_Click()
On Error GoTo UpdateErr
adoPrimaryRS.UpdateBatch adAffectAll
If mbAddNewFlag Then
adoPrimaryRS.MoveLast 'move to the new record
End If
mbEditFlag = False
mbAddNewFlag = False
SetButtons True
mbDataChanged = False
Exit Sub
UpdateErr:
MsgBox Err.Description
End Sub
Private Sub cmdFirst_Click()
On Error GoTo GoFirstError
adoPrimaryRS.MoveFirst
mbDataChanged = False
Exit Sub
GoFirstError:
MsgBox Err.Description
End Sub
Private Sub cmdLast_Click()
On Error GoTo GoLastError
adoPrimaryRS.MoveLast
mbDataChanged = False
Exit Sub
GoLastError:
MsgBox Err.Description
End Sub
Private Sub cmdNext_Click()
On Error GoTo GoNextError
If Not adoPrimaryRS.EOF Then adoPrimaryRS.MoveNext
If adoPrimaryRS.EOF And adoPrimaryRS.RecordCount > 0 Then
Beep
'moved off the end so go back
adoPrimaryRS.MoveLast
End If
'show the current record
mbDataChanged = False
Exit Sub
GoNextError:
MsgBox Err.Description
End Sub
Private Sub cmdPrevious_Click()
On Error GoTo GoPrevError
If Not adoPrimaryRS.BOF Then adoPrimaryRS.MovePrevious
If adoPrimaryRS.BOF And adoPrimaryRS.RecordCount > 0 Then
Beep
'moved off the end so go back
adoPrimaryRS.MoveFirst
End If
'show the current record
mbDataChanged = False
Exit Sub
GoPrevError:
MsgBox Err.Description
End Sub
Private Sub SetButtons(bVal As Boolean)
cmdAdd.Visible = bVal
cmdEdit.Visible = bVal
cmdUpdate.Visible = Not bVal
cmdCancel.Visible = Not bVal
cmdDelete.Visible = bVal
cmdClose.Visible = bVal
cmdRefresh.Visible = bVal
cmdNext.Enabled = bVal
cmdFirst.Enabled = bVal
cmdLast.Enabled = bVal
cmdPrevious.Enabled = bVal
End Sub