Msgbox help

Gazza2

Registered User.
Local time
Today, 02:57
Joined
Nov 25, 2004
Messages
184
Not sure if this is in the right forum but here goes.

I have a form for entering customer and job details (form1) which work fine.

What i cant figure out is a couple of simple message boxes.

The first field on the form is a "name" textbox. What i want to happen is if the name field is empty and the user presses enter or tab to get to the next field i want a message box to pop up saying that "name is required"(Msgbox1).
If the user enters a name that already exists in the customers table then i want another message box to pop up saying that the name already exists, and when the user clicks ok they are taken back to an empty name field.

I`ve trawled through all the posts i can find but none of them seem to be what i need (and im not clever enough to modify the code myself) or the ones that do seem to be what i want i cant get to work.

Ive tried putting code in the Afterupdate, Beforeupdate and the lost focus event of the name field.

Sorry for the long post and thanks in advance for your help.
 
You will want to put your code in the lost focus event of you control. Use the dlookup function to determine if the name already exist.
 
Thanks for the reply KeithG

Got one message to come up for the empty field using (with a little help from one of ruralguy posts)

If Len(Me.Name & "") > 0 Then
MsgBox "Message"
End If

but if i wanted it to setfocus on the name field after clicking ok how would i do it and where in the code would i put it.

Not to sure of the syntax for dlookup so any help will be much appriceated.

Thanks
 
Code:
If Len(Me.Name & "") > 0 Then
    MsgBox "Message"
    Me.Name.Setfocus
End If
 
That gives an invalid qualifier for the .Name part of the statement

The name of the field is Name
 
ok i can get a message to show butnot under the right circumstances.

Heres the code im using in the beforeupdate box to display a message for duplicates:
Private Sub Name_BeforeUpdate(Cancel As Integer)
If DLookup("Name", "table6") > 0 Then
MsgBox "message2"
End If

And Heres the code for the lostfocus event for an empty name field

If Len(Me.Name & "") > 0 Then
MsgBox "Message"
End If

problem is it displays the "message" whether i enter anything in the name box or not , same with the "message2".

Am i right in thinking that these two statements need to go together with an If..Then..Else statement.
 
You should not use reserved words as object names. Me.Name actually returns the name of the current form.
 
Changed the name and used some sample code from a reply post by mstef
(Error 2501 using DCount ) and swapped the names for my own and it works fine.

thanks for all the help.
 

Users who are viewing this thread

Back
Top Bottom