By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,611 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,611 IT Pros & Developers. It's quick & easy.

building required field logic in a form and corresponding error messages

P: 35

I am building a form and i would like it to have conditional error messages.

For instance, I am creating a Date field. If the date field is populated, I also need Field 2 populated and an error message created. However, if the date field is not populated, then Field 2 need not be populated.

is this possible?
Apr 28 '09 #1
Share this Question
Share on Google+
11 Replies

Expert 100+
P: 1,287
You could use a form with unbound controls and a Save or Submit button. On click, you can execute whatever validation code you want, then if everything is valid, insert a record. That's my preference for forms with lots of validation, anyway. Let me know if you decide to go this route and have problems.
Apr 28 '09 #2

P: 35
Hi Chip,

thanks for responding! Here's where i have problems. i'm not sure where to put the logic. the logic i'm hoping to build is something like this:


I'm hoping to design the form so that the capitalized and encased in {} is the outcome. does this make sense?
Apr 28 '09 #3

Expert 100+
P: 1,287
A quick and easy way is some code in the Form's BeforeUpdate event.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. On Error GoTo ErrorHandler
  3.     If (Not ( IsNull ( [Enrollment Date] ) ) ) And ( IsNull(count) ) Then
  4.         Cancel = True
  5.         MsgBox "Count is required if an Enrollment Date is entered."
  6.     End If
  7. ExitCode:
  8.     Exit Sub
  9. ErrorHandler:
  10.     MsgBox Err.number & " - " & Err.Description
  11. Resume ExitCode
  12. End Sub
Apr 28 '09 #4

P: 35
Great! what if i have multiple things that i need to check. do i need to fit the logic within one statement or can i create multiple statements. if i can create multiple statements, then will each error message come up seperately?

finally, what's the function of the ErrorHandler? is that for me to create my own error message? confused then about the msgbox "count is required". are those redundant? thanks so much!
Apr 28 '09 #5

Expert 100+
P: 1,287
You can check multiple things with multiple statements, and give an appropriate MsgBox for each. They will all be checked, and then at the end if Cancel was set to True by any of them, the update will cancel, and the record won't be saved.

The error handling code will only be called in case something in your code creates a run-time error, which would normally crash the program. Error handling allows you to display a nice message box and not crash, in case you caused a problem that wasn't caught when you compiled, but it has nothing to do with your validation. Sorry to confuse you with that. It's not really necessary for development, since you can debug, but if you are planning to release your application for others to use, it should be in every piece of code.
Apr 28 '09 #6

P: 35
thanks Chip, i'll give it a shot! your assistance is very much appreciated. thank you so much!

i'll let you know how it goes. Todd
Apr 28 '09 #7

P: 35
regarding form validation, will a seperate message box come up with each error type? thanks!
Apr 28 '09 #8

Expert 100+
P: 1,287
Just to be clear, let's not describe them as errors, because there are predefined errors that already exist and have another purpose. For each validation check you want to do, use the format:
Expand|Select|Wrap|Line Numbers
  1.   If (logic for checking that your rule was broken) Then
  2.      MsgBox "your message to the user"
  3.      Cancel = True
  4.   End If
And you can do a bunch of those one after another.
Apr 28 '09 #9

P: 35
brilliant! thank you again.

Apr 28 '09 #10

Expert 100+
P: 1,287
No problem, Todd. Good luck.
Apr 28 '09 #11

Expert Mod 15k+
P: 31,712
It's not for everyone, or even every situation (There is only one message for instance), but Access tables include Validation Rule & Validation Text properties. These can be used at a table level (which means they are not reliant on the operator using the intended form) and can specify how the data in the table can fit together.

To be honest, I doubt this will help you with your current problem Todd, but it may help some of those who are looking for similar answers and find this thread.
Apr 29 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.