Keep Focus

  • Thread starter Thread starter Dan @BCBS
  • Start date Start date
D

Dan @BCBS

The code below alerts the user that some fields are blank.
It works until the last line!!!

How can I make it keep the form open an go to the first blank field, then
the second etc..(I'll be adding more)???

Private Sub Form_Close()

If (IsNull(Me.FirstName)) Or (IsNull(Me.LastName)) Then
If MsgBox("Do you want to Edit Member Information now?", vbQuestion &
vbYesNo, "Question") = vbYes Then
(Me.???) ???.SetFocus
 
The code below alerts the user that some fields are blank.
It works until the last line!!!

How can I make it keep the form open an go to the first blank field, then
the second etc..(I'll be adding more)???

Private Sub Form_Close()

If (IsNull(Me.FirstName)) Or (IsNull(Me.LastName)) Then
If MsgBox("Do you want to Edit Member Information now?", vbQuestion &
vbYesNo, "Question") = vbYes Then
(Me.???) ???.SetFocus

The Close event is too late in the Form closing series of events.
Use the Form's Unload event. You can Cancel the unloading and set
focus where ever you wish.

Private Sub Form_Unload(Cancel as Integer)
If IsNull(Me.FirstName) Or IsNull(Me.LastName) Then
If MsgBox("Do you want to Edit Member Information now?",
vbQuestion & vbYesNo, "Question") = vbYes Then
Cancel = True
Me.[FirstName].SetFocus
End If
End If
End Sub
 
First, you need to move the code to the Form UnLoad event. The close event
is too late and can't be canceled. The form will just close.

Private Sub Form_UnLoad(Cancel As Integer)
Dim strCtl As String
Dim blnCancel As Boolean

If IsNull(Me.FirstName) Then
blnCancel = True
strCtl = "FirstName"
End If

If IsNull(Me.LastName) Then
blnCancel = True
strCtl = "LastName"
End If

If blnCancel Then
If MsgBox("Do you want to Edit Member Information now?", vbQuestion &
vbYesNo, "Question") = vbYes Then
Me.Controls(strCtl).SetFocus
Cancel = True
End If
End If
End Sub
 
Back
Top