Keep Focus

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
 
F

fredg

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
 
K

Klatuu

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top