Full Version: setting message box with condition
UtterAccess Forums > Microsoft® Access > Access Forms
dariusme
Guys..
How do i make a message box to appear error message whenever some of the field in my form are not entered?I know i can set the field Required to yes but can i write in code instead?I tried this code but it does not seem to work
Private Sub Command79_Click()
If Forms!AddITR![Defect] = "Null" Then
MsgBox "Error"
End If
End Sub
where defect is the field inside my form name call additr
Larry Larsen
Hi
There are many post that have covered this subject,(do a search on "Validation") attached is a small demo from a fellow member here at UA (sorry I don't/can't remember their name) but worth a look at.
The action of the validation and control colour change can be found using functions in the modules object.
Also here is a : Link : to a post on the subject.
If you require any futher help please post back.
HTH's
thumbup.gif
Dom DXecutioner
try the following ..
code]'// if the field is in the current form you use the word "Me" to refer to the current form
If IsNull(Me.AddITR) Then
MsgBox "You left something required blank "
End If
dariusme
thanks guys..it works. But how do i set the cursor to retorn to the field after the message box appear?IT seems that it will goes to the next field
Larry Larsen
Hi
Try:
Me.Your_Control_Name.Setfocus
thumbup.gif
dariusme
Hi Larry
Odid that but it seems that when the message box comes out when i did not key in the data and i click on the ok at the message box, the cursor just go to the next field. This is what i wrote:
If IsNull(Me.additr) Then
MsgBox "Pls fill in MS number"
Me.additr.SetFocus
End If
Larry Larsen
Hi
Which event are you trying run the code behind..?
thumbup.gif
r_cubed
The cursor is STILL moving on to the next control because that is the 'process' that was being done (i.e. tabbing away from your field 'additr') which cause yo to run your special check, so Access firstly does your check code, but then has to continue what IT was doing, and so it 'moves/tabs' to the next control.

YOU have to 'force' the cursor to some OTHER field on the form YOURSELF , BEFORE then 'forcing' the cursor BACK TO where YOU want it to be.

OALWAYS have a command button on every form from where the user 'exits' MY forms, so I am able to have a consistent approach to this problem. My command button name is 'cmd_Exit', so the code that you need would THEN read:

If IsNull(Me.additr) Then
MsgBox "Pls fill in MS number"
ME.cmd_Exit.SETFOCUS
Me.additr.SetFocus
End If
Edited by: r_cubed on Sun Nov 7 15:28:18 EST 2004.
dariusme
wow...its works man....
but what i don't understand is why must force it to another field before we can orce it back to the one we want?
dariusme
if access need to complete the check before it can prpceed, wouldn't it work if we add in teh setfocus after teh end if?
r_cubed
>
It is the (internal) order of processing that Access itslef does.
When you 'exit' a field, Access will FIRSTLY 'move' the curso away from the field in question (thus positioning the cursor somewhere else, and this 'movement' TRIGGERS ANY/ALL of the processes that you may have defined in the OnExit, LostFocus, AfterUpdate events. It has to do these checks because YOU (the developer) makes your OWN choice as to what (if anything) you do within any/all of these events.
So Access will do THESE events, and then come back and 'coninue' to DO the ACTION that 'initiated' any/all of these (i.e. the movement (tab) away from the field in question.
For further explanation, I suggest : www.bill_gates@microsoft.com
FOR Access Help ----> search for 'Order of Events'
2nd question: It is NOT right to do it AFTER the EndIF, as this would cause the cusrsor to be placed BACK INTO the field IRRESPECTIVE of whether there was an error detected or NOT (which is NOT what you want). You only want it back there if an error occurs.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.