Bound List

Option Explicit
Private Utility As New clsUtility
Private mblnValidationFailed As Boolean

Private Sub datProducts_Validate(Action As Integer, Save As Integer)
Dim strMsg As String
Dim enumMsgResult As VbMsgBoxResult

If Save = True Or Action = vbDataActionUpdate _
Or mblnValidationFailed Or Action = vbDataActionAddNew Then
‘ One or more bound controls has changed or a previous validation failed,
‘ so verify that all fields have legal entries. If a field has an incorrect
‘ value, appenda string explaining the error to strMsg and set the focus
‘ to that field to facilitate correcting the error. We explain all
‘ errors encountered in a single message box.
strMsg = “”
If txtProductName.Text = “” Then
Utility.AddToMsg strMsg, “You must enter a Product name.”
End If

If strMsg “” Then
‘ We have something in the variable strMsg, which means that an error
‘ has occurred. Display the message. The focus is in the last
‘ text box where an error was found
enumMsgResult = MsgBox(strMsg, vbExclamation + vbOKCancel + _

If enumMsgResult = vbCancel Then
‘Restore the data to previous values using the data control
mblnValidationFailed = False
‘ Cancel the Validate event
Action = vbDataActionCancel
‘ Deny form Unload until fields are corrected
mblnValidationFailed = True
End If
mblnValidationFailed = False
End If
End If

End Sub
Private Sub Form_Unload(Cancel As Integer)

‘ Don’t allow the unload until the data is validate or the
‘ update is cancelled
If mblnValidationFailed Then Cancel = True

End Sub
Private Sub mnuDataAdd_Click()

‘ Reset all controls to the default for a new record
‘ and make space for the record in the recordset copy
‘ buffer.

‘Enable the save menu choice
mnuDataSave.Enabled = True

‘ Set the focus to the first control on the form
End Sub

Private Sub mnuDataDelete_Click()
Dim strMsg As String

‘Verify the deletion.
strMsg = “Are you sure you want to delete ” _
& IIf(txtProductName.Text “”, txtProductName.Text, _
“this record”) & “?”
If MsgBox(strMsg, vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then

‘ We really want to delete

‘ Make a valid record the current record and update the display.

‘ If we deleted the last record, move to the new last record
‘ because the current record pointer is not defined after
‘ deleting the last record, even though EOF is defined.
If datProducts.Recordset.EOF Then datProducts.Recordset.MoveLast
End If
End Sub

Private Sub mnuDataSave_Click()

‘ Invoke the update method to copy control contents to
‘ recordset fields and update the underlying table
If datProducts.Recordset.EditMode dbEditAdd Then

‘ If we added move to the new record
End If

End Sub

Private Sub mnuEditUndo_Click()

‘ Undo all pending changes from form by copy recordset values
‘ to form controls

If datProducts.Recordset.EditMode = dbEditAdd Then

‘ Disable the menu save and cancel the update
mnuDataSave.Enabled = False
End If

End Sub

Private Sub mnuFileExit_Click()

Unload Me

End Sub

One response to “Bound List

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s