help with code please

mkelly

Registered User.
Local time
Today, 16:22
Joined
Apr 10, 2002
Messages
213
I enetered this code so that if you go to close the form and "proofreaders name" is null I want it to not close and set the focus back on the "proofreaders name" however all I get is an error message on the set focus line.. any suggestions?

Private Sub Close_form_LostFocus()
If IsNull(Me.Proofreaders_name) Then
MsgBox "You have not entered your name, cannot procede"
'DoCmd.GoToControl "Proofreaders name"
SetFocus(Me.Proofreaders_name) = True
End If

End Sub
 
It is:
Code:
Private Sub Close_form_LostFocus() 
    If IsNull(Me.Proofreaders_name) Then 
        MsgBox "You have not entered your name, cannot procede" 
        Proofreaders_name.SetFocus 
    End If 
End Sub
 
thanks for the help. however when I click the close button I still get the message box and when that is closed the form closes and the record has a start time with no workers name.
 
On which event of which object have you put the code?
If it is the X in the upper right corner, use the Unload event.
If it is a command button you created, I think it is the Exit event.
In these, you put that code:
Code:
If IsNull(Me.Proofreaders_name) Then 
    MsgBox "You have not entered your name, cannot procede" 
    Proofreaders_name.SetFocus 
    Cancel = True
End If
 
I put it on lost focus but I think it might be on focus otherwaise the command button exit takes over
 
Sorry Pat I didn't try your code or saw it when I wrote my reply. My reply was to Newman.
 
MKelly,
I may have confused you since I made a correction on the SetFocus in my first post, but didn't looked at the event used.
But in my second post I came back on it and told you that there was a mistake in it. I didn't find the real answer, but I was corrected by Pat. You should listen to Pat's advice. She is very good with Access.
 
Pat I tried your code maybe I am putting it in the wrong area can you help me please.


Preventing the form close is a little more work.
1.Create a form level variable named
Dim gPendingError As Boolean

I put this under the option explicit statement
---------------------------------------------------------------------
2. Initialize the variable in the Current event of the form
gPendingError = False

this went here,
Private Sub Form_Current()
gpendingerror = False

End Sub
------------------------------------------------------------
3. Set the error flag when an error is discovered


code:--------------------------------------------------------------------------------If IsNull(Me.Proofreaders_name) Then
MsgBox "You have not entered your name, cannot procede"
Me.Proofreaders_name.SetFocus
gPendingError = True
Cancel = True
Else
gPendingError = False
End If--------------------------------------------------------------------------------
I put the above code in "on error"
-------------------------------------------------------------------
4. Check the pending error flag in the form's unload event.

code:--------------------------------------------------------------------------------If gPendingError = True Then
Cancel = True
Msgbox "You must fix error before closing form", vbOKOnly
End If
And i put this in the unload event.

But the form still closes and record saves without a name?
please tell me what I am doing wrong????
 

Attachments

Pat thank you, thank you, thank you you are a lifesaver. One more question if you don't mind. I used this code on multiple forms for the name entry. but I cannot get it to work on a date field??? could this be because it is linked to a query and not a table? any help appriciated.

thanks again!!
 
once again help is needed

Can I use this code in the before update for two different fields on the form? If so how would I write that? Help is greatly appriciated.
 
Pat I do have it in beforeupdate on the form..

Thanks again for all your help you truly are a master at this program.
Thanks
 

Users who are viewing this thread

Back
Top Bottom