Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Validating Combo Boxes 1

Status
Not open for further replies.

JesseL

IS-IT--Management
Jul 29, 2002
59
US
Hello all! I have combo boxes on my form that get values from a lookup wizard. What is the correct code to use? I am assuming something like NotInList. Can someone help me if they have time and show me the correct way to check if nothing is entered in the combobox? I am using If then statements.

I thought the code would look something like this, but i am wrong:

If me![cboSwingID].value = false then
msgbox "Please choose a Swing Type"
me.cboSwingID.setfocus
Exit Sub

elseif me![cboUnitDescriptionID].value = false then
msgbox "Please Choose a Unit Description"
me.cboUnitDescriptionID.setfocus
exit sub

else

docmd.gotorecord , , acNewRec

end if
 
You're close with the check...

If me![cboSwingID].text = "" then
msgbox "Please choose a Swing Type"
me.cboSwingID.setfocus
Exit Sub

that's what I use.

Not sure if you were asking something else or not...but hope that helps.

Kevin
 
That first code i showed first worked in another database :(
Now its not!!??

This is what is happening:

I have a command button called cmdAddNewRec. If there are empty values in the combo boxes or text boxes, I want to check for empty values when the button is clicked. The text boxs check for the data, but the combo boxes are skipped, and it goes to a new record.

Godawgs.. when i change the code to what you have, I get the following error message "You can't reference a property or method for a control unless the control has the focus"
I have never seen this message before. Can someone please tell me whats wrong?

thanks
ksharp

 
Yes! that worked.. I put it before the If statement. Now, when i put all other controls before the if statement, i get the same error.. This is what i have before the If statement:

Me![txtMrdType].SetFocus
Me![cboSwingID].SetFocus
Me![cboUnitDescriptionID].SetFocus
Me![txtHardwareSet].SetFocus

Should each of these come after the exit sub like this?


me![txtMrdType].Setfocus

If IsNull(Me![txtMrdType]) Then
MsgBox "Please Enter a Mrd Type"""
Me![txtMrdType].SetFocus
Exit Sub

me![cboSwingID].Setfocus

and so on... ?

Thanks
ksharp
 
Each setfocus needs to come directly before you check the value of that control...basically setfocus is saying "Look at that combo box", so you need to tell it to do that before each combo box or other control is checked. Only one control can have the focus at a time too, so that's why you got the error again when you listed all the controls with the .setfocus. Hope that helps.
 
Sorry, im kinda Newb to vba. Can you give me an example where to put the .setfocus for each control? I placed all the .setfocus before each control is checked, and it is still not working. This must be really easy to fix. Can you do an example with the code i gave u at first. :)

thanks
ksharp
 
Yeah, no problem. I think I see your problem, you're gonna have to lose the elseif just because you can't check the focus otherwise...not a big deal though since you exit the sub if the first if is true anyway...try this:

me.cboswingid.setfocus
If me.cboSwingID.text = "" then
msgbox "Please choose a Swing Type"
me.cboSwingID.setfocus
Exit Sub
end if

me.cboUnitDescriptionID.setfocus
if me.cboUnitDescriptionID.text = "" then
msgbox "Please Choose a Unit Description"
me.cboUnitDescriptionID.setfocus
exit sub
end if

 
Before I put the code in, How will this tie in with the:

docmd.gotorecord , , acnew ?

at the end of the if then statement is what i mean.


 
I got it Godawgs! Thank you so much for your time and help. This is great practice for me!

Thanks!
ksharp
 
Just put that after all of the if statements are complete. You've put "exit sub" in every if statement...so basically any time that one of the combo boxes ends up being blank it will give you the little message box, and it will exit the sub, which means that it won't go through the rest of the code after that...so as long as you check them all and each one of them has text you can just put that last line in right after the final end if. Hope that makes sense.

Kevin
 
Erm.. lol, It is skipping the FIRST If statement now?

me.txtMrdType.setfocus

If IsNull(Me![txtMrdType]) Then
MsgBox "Please Enter a Mrd Type"
Me![txtMrdType].SetFocus
Exit Sub
End If

It skips this, and now checks all others :(
 
I'd do the same with the text boxes...check if me.txtMrdType.text=""...that's just me though, I don't like to use the IsNull and all that...I may get yelled at for that too, but oh well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top