- Local time
- Yesterday, 19:39
- Joined
- Feb 19, 2002
- Messages
- 45,446
I just added #2 --- that link is further down. It is much shorter than #1. I also added a copy of the database I used for the video. Enjoy
I created a database to help people to understand how and why to use the Form's BeforeUpdate event to validate data. @Uncle Gizmo and I made a video to talk about the example. It's about a half hour long. Please take a look if you have time and we'd love to hear your comments.
You should be able to click on play without having to create an account.
Bad Data is Bad for Business - Pat & Tony (screencast-o-matic.com)
I have a couple more examples to add to the database and when I'm finished, I'll post the actual database for you to play with. It is a great learning tool if you care to learn how Access works so you can use Form and Control events as the MS Access design team intended them to be used. This is a difficult topic for even experienced developers to get their heads around. The video is longer than I'd like but I'm new at this. Once you view this one, the others in the set can be much shorter. I will explain the Contro''s BeforeUpdate event and when you can use it and when you shouldn't use it for validation. and also why you should NOT use the On Exit in particular for validation. You can keep Access from leaving a control but unless you take Draconian measures and remove the user's update, you can't stop Access from saving the bad data without code in multiple events. If you limit yourself to coding your validation in the form's BeforeUpdate event, you only ever need to use ONE event to have complete control over whether or not a record gets saved.
Here is the code in the State form's BeforeUpdate event along with the two functions it calls so you can see what is running behind the scenes in the video.
I created a database to help people to understand how and why to use the Form's BeforeUpdate event to validate data. @Uncle Gizmo and I made a video to talk about the example. It's about a half hour long. Please take a look if you have time and we'd love to hear your comments.
You should be able to click on play without having to create an account.
Bad Data is Bad for Business - Pat & Tony (screencast-o-matic.com)
I have a couple more examples to add to the database and when I'm finished, I'll post the actual database for you to play with. It is a great learning tool if you care to learn how Access works so you can use Form and Control events as the MS Access design team intended them to be used. This is a difficult topic for even experienced developers to get their heads around. The video is longer than I'd like but I'm new at this. Once you view this one, the others in the set can be much shorter. I will explain the Contro''s BeforeUpdate event and when you can use it and when you shouldn't use it for validation. and also why you should NOT use the On Exit in particular for validation. You can keep Access from leaving a control but unless you take Draconian measures and remove the user's update, you can't stop Access from saving the bad data without code in multiple events. If you limit yourself to coding your validation in the form's BeforeUpdate event, you only ever need to use ONE event to have complete control over whether or not a record gets saved.
Here is the code in the State form's BeforeUpdate event along with the two functions it calls so you can see what is running behind the scenes in the video.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Call LogEvent(Me, "Form_BeforeUpdate")
On Error GoTo ErrProc
If EditRequired(Me) = "Error" Then ' check required fields
Cancel = True
Exit Sub
End If
If Me.Population & "" = "" Then
Else
If IsNumeric(Me.Population) Then
If Me.Population < 0 Then
MsgBox "Population must be a positive number", vbOKOnly
Cancel = True
Me.Population.SetFocus
Exit Sub
End If
Else
MsgBox "Population must be numeric.", vbOKOnly
Cancel = True
Me.Population.SetFocus
Exit Sub
End If
End If
Me.UpdateBy = Environ("UserName")
Me.UpdateDT = Now()
ExitProc:
Exit Sub
ErrProc:
Select Case Err.Number
Case Else
MsgBox Err.Number & " -- " & Err.Description
Resume ExitProc
End Select
End Sub
Public Function LogEvent(frm As Form, EventName As String)
Dim strSql As String
Select Case Forms!frmEventSamples!fraControlLogging
Case 2 ''Form events only
If Left(EventName, 5) = "Form_" Then
Else
Exit Function
End If
Case 3 ''Control events only
If Left(EventName, 5) <> "Form_" Then
Else
Exit Function
End If
Case 4 ''No Logging
Exit Function
Case Else '' Log all events with log code
End Select
strSql = "Insert Into tblEventLog ( FormName, EventName, FormType) Values(" & QUOTE & frm.Name & QUOTE & ", " & QUOTE & EventName & QUOTE
strSql = strSql & ", " & QUOTE & Left(frm.Name, 1) & QUOTE & ");"
DoCmd.RunMacro "mWarningsOff"
DoCmd.RunSQL strSql
DoCmd.RunMacro "mWarningsOn"
Forms!frmEventSamples!sfrmEventLog.Requery
End Function
Public Function EditRequired(frm As Form) As String
Dim ctl As Control
EditRequired = "Error"
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox, acOptionGroup
If InStr(ctl.Tag, "Required") Then ''' used to allow the Tag property to be used for multiple procedures
If ctl.Value & "" = "" Then
MsgBox ctl.Name & " -- is required."
ctl.SetFocus
Exit Function
End If
End If
End Select
Next ctl
Set ctl = Nothing
EditRequired = "Valid"
End Function
Last edited: