Access 2003 VBA help with Combo Box on Form

spike250

Registered User.
Local time
Today, 01:04
Joined
Aug 6, 2009
Messages
70
Hi All,

I have two combo boxes on my form called; (along with other text boxes)
cboTestG
cboResultsC

What I am trying to do is to prevent users from leaving both combo boxes blank. something like the below but i'm not sure how to put this into code?

If cboTestG is blank then cboResultsC must be completed,
If cboResultsC is blank then cboTestG must be completed,
If cboTestG and cboResultsC are both blank then a message box to pop up advising that they need to enter either a test or a result.

Any help would be appreciated.

Regards

Spike
:confused::confused::confused:
 
Lookup the IsNull() function and the IF ...ELSE ... END IF statement.
 
I have managed to write the following code,
but I cannot get it to work correctly?

Code:
Private Sub cboTestG_Exit(Cancel As Integer)
If Me.cboTestG = "" Then
Me.cboResultsC = True
Else
MsgBox "Please Enter A Result", vbOKOnly, Mandatory
If Me.cboTestG = True Then
Me.cboResultsC = ""
Else
MsgBox "Please Enter A Test", vbOKOnly, Mandatory
End If
End If
End Sub

When the cboTestG looses focus it brings up both messages before I have chance to type in the ResultsC, I might be putting it in the wrong place?

Any help?

Spike
:confused::confused::confused:
 
Couple of things Spike.

1. You want that in the Befor_Update event of the form, not the control.
2. Structure your if else statements by nesting them properly:
Code:
IF IsNull(Me![MyControl1]) Or IsNull(Me![Control2]) Then
    Cancel = False
    ..... Any other code you want to add.
End If

Also your message box is missing something. Remember the title is a string.:)
 
Thanks for the reply,

Not sure if I made the proplem clear enough to begin with.

There are three combo box in line and the user tabs down through them,
When they get to the third this is where I need the code;

If cboTestG & cboResultsC are both blank to bring up message box saying "You must either enter a Test or a Result!"

I need to make either of these fields mandatory;
So if cboTestG has data selected then cboResultsC can remain blank
Or
if cboTestG is blank then cboResults must contain data

Is this possible, if so any help would be great as I haven't had much practice with
Lookup the IsNull() function and the IF ...ELSE ... END IF statement.

Thanks

Spike
:confused::confused::confused:
 
You're on the right track Spike.

1. Use the aircode I gave in my last post as a guide and change the OR to AND.
2. Remove that "Cancel = False" line.
3. Once you've done that, put your msgbox line where "... Any other code you want to add" is. Enclose the TITLE argument (the 3rd argument) of the msgbox in quotes.
4. You may want to put it in the ENTER event, but it seems you prefer the EXIT event of the control. That's up to you when you want it to fire :)
 

Users who are viewing this thread

Back
Top Bottom