Option Compare Database
Option Explicit
Dim strControlName As String
Private Sub Form_Current()
''''''''''''''''''''''''''
'*
'* arnelgp
'*
'* NOTE:
'*
'* to be able for this to work, you must add an Autonumber field
'* on the table and on this form.
'*
'* Replace 'ID' on the proceeding expression with the name of Autonumber field.
'*
''''''''''''''''''''''''''
Const PKField As String = "ID"
Static TheEnabledControl As String
Static lngID As Long
Static bolHandled As Boolean
Dim lngCurrentID As Long
lngCurrentID = Nz(Me.ID, 0)
If TheEnabledControl <> "" Then
With Me.RecordsetClone
'* find the previous record
.FindFirst PKField & "=" & lngID
If Not .NoMatch Then
'* check if the control we are validating
'* has any value
If .Fields(TheEnabledControl).Value & "" = "" Then
'* there is no value...
'* go back to this record
Me.Bookmark = .Bookmark
'* the validation msg will fire 2X
'* so we have a flag that it has been
'* handled, so it will not show the
'* messagebox again.
If Not bolHandled Then
MsgBox "Cannot go to next/previous record until '" & TheEnabledControl & "' is filled up"
bolHandled = True
Else
bolHandled = False
End If
Else
'* there is value on the control
If lngCurrentID <> 0 Then
'* go to next/previous record
.FindFirst "ID=" & lngCurrentID
Me.Bookmark = .Bookmark
lngID = Nz(Me(PKField).Value, 0)
End If
End If
End If
End With
Else
'* when the form's initial opening, it will go
'* here
lngID = Nz(Me(PKField).Value, 0)
''''''''''''''''''''''''''''''''''''''''''
'* NOTE:
'*
'* Replace 'F3' with the name of the Enabled control (field)
'* you want to validate. in this example i am using F3 field.
'*
''''''''''''''''''''''''''''''''''''''''''
TheEnabledControl = "F3"
End If
End Sub