Option Group Validation Crisis! PLease Help!

D

datahead22

what I'm trying to do is get the user to select one of for check boxes
in an option group[contact_at] if they don't select one it should
cancel the sub and they get a message box telling them to make a
selection
then based on the users selection I need to check if a coresponding
text box is null if it is they must stop and populate the text field
before they can write the information to the tables.

What may make this more complicated is it's coming in the middle of a
bunch of other validation code.

I'm not sure this makes sense but it might when you look at the code
This is my latest attempt:
'This first section works fine
If IsNull([sources_count_residence1]) Then
MsgBox "Source's County of Residence is a Required Field"
Me![sources_count_residence1].SetFocus
Exit Sub
End If
'This is where the trouble begins
If IsNull(Me![contact_at]) Then
MsgBox "Please Select a Location to Contact Source At!"
Me![contact_at].SetFocus
Exit Sub
Select Case Me![contact_at]
Case 1
If IsNull([sources_home_phone1]) Then
MsgBox "Source's Home Phone is a Required Field"
Me![sources_home_phone1].SetFocus
End If
Exit Sub
Case 2
If IsNull([sources_cell_phone1]) Then
MsgBox "Source's Cell Phone is a Required Field"
Me![sources_cell_phone1].SetFocus
End If
Exit Sub
Case 3
If IsNull([sources_work_phone1]) Then
MsgBox "Source's Work Phone is a Required Field"
Me![sources_work_phone1].SetFocus
End If
Exit Sub
Case 4
If IsNull([sources_email_address1]) Then
MsgBox "Source's Email Address is a Required Field"
Me![sources_email_address1].SetFocus
End If
Exit Sub
End Select
'After this section I need to go back to...
If IsNull([sources_contact_method1]) Then
MsgBox "Source's Preferred Contact Method is a Required Field"
Me![sources_contact_method1].SetFocus
Exit Sub
End If
If IsNull([concern_desc1]) Then
MsgBox "Concern Description is a Required Field"
Me![concern_desc1].SetFocus
Exit Sub
End If

Sorry I know that's a lot of code but sure would appreciate the help
 
D

Duncan Bachen

datahead22 said:
'This is where the trouble begins

You don't actually say what the trouble is and what behavior you are seeing.
If IsNull(Me![contact_at]) Then

This assumes that you haven't set a default value for the option group.
If you have, it will never be null. Another option would be to *set* a
default value to something that is not one of your selectable choices
and then check against it in your Case statement.

Have you set a breakpoint on this line and determined if it's ever
actually Null or not?

Also, with your naming convention, I can't tell if you are referencing
objects or fields. Consider using prefixes such as optContactAt and
txtHomePhone1. That way, when you reference [sources_home_phone1] you
know that you are referring to the underlying field and not the text box
which is displaying it. That's important in debugging.
Sorry I know that's a lot of code but sure would appreciate the help

Where and how are you calling this validation code? You could have a
problem with setting the focus to the specific control based on that.

I think a little more information on what's not working will help.
 
D

datahead22

Thanks Duncan, I was having trouble getting it to run the select case
statement.
I'll remember the next time I post code to put in the prefixes for text
and option groups so you guys know what I'm referring to.
I had decided to use the case statement because I couldn't figure out
how to make the if statements work that I was using previously.
I would have had the answer at my finger tips if I was still writing
SQL all the time.
I'm just starting to get the syntax for VB figured out.
Some times I get so caught up in the VB that I forget what I know about
syntax from writing SQL.
I just changed the if statement to read like this and now it works
fine:


If IsNull(Me![contact_at]) Then
MsgBox "Please Select a Location to Contact Source At!"
Me![contact_at].SetFocus
Exit Sub
End If
If Me![contact_at] = 1 And IsNull([sources_home_phone1]) Then
MsgBox "Source's Home Phone is a Required Field"
Me![sources_home_phone1].SetFocus
Exit Sub

Basically I forgot to use 'AND' and I'm felling pretty dumb now but at
least I figure it out

Thank You anyway for getting back to me, I've learned so much from you
all out here glad this Group exists! Wish I knew about years ago!!
 
D

Duncan Bachen

datahead22 said:
I'll remember the next time I post code to put in the prefixes for text
and option groups so you guys know what I'm referring to.

It's not just for us, it's actual *for you*. We can make some
assumptions, but you'll find that you'll be able to find and debug your
own errors much better in the future if you use proper naming conventions.

It will also save you time if you have to go back and look at your code
later, or, if someone else inherits your code.

Start good coding habits, and they'll stick with you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top