Form Data Validation Driving me Nuts!

Montez659

Well-known Member
Joined
May 4, 2005
Messages
918
I cannot for the life of me figure out how the data validation property on a combobox control on a form works! Maybe it is the fact that it is a subform in datasheet view - who knows - but everytime that I try something for the validation rule, nothing happens. What I want is supposed to be simple:
Code:
 Is Not Null
I have tried this code too many different ways to document here, but someone please explain to me what the correct expression should be.

The field should not be blank and the user has a value list to choose from - Primary or Assistant. Limited to list and no edits to list. But when the form pops up (which actually contains the subform for a Junction table) they can choose a name from the first field and still leave the repRole field blank with no warning message.

Thanks for the help in advance!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I would do your checks in one place - the Form's Before Update event. Then you can use code like:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
 
If Len(Me.ComboNameHere & vbNullString) = 0 Then
   strMsg = "Field Name Here" & vbCrLf
End If
 
If Len(Me.SomeOtherControlNameHere & vbNullString) = 0 Then
   strMsg = strMsg & "Other Field Name Here" & vbCrLf
End If
 
If strMsg <> vbNullString Then
   MsgBox "You are missing data for these fields: " & vbCrLf & strMsg
   Cancel = True
End If
 
End Sub

And where it says "Field Name Here" and "Other Field Name Here" that is where you can put a friendly name which the user will recognize instead of the control or field name.
 
Upvote 0
How does that work in a subform in datasheet view? Do I need to loop through something to check each field Before Update or does the code already take care of that?

BTW, Bob, you have been a tremendous help to me today (and many times in the past). I just want to say that I appreciate the time that you put into sharing your wisdom here.
 
Upvote 0
Ok, nm that. I got it to work. Is Access form data validation (via the properties box) just a pain in the rear, or do I just need to read up on it some more?
 
Upvote 0
It would go in the subform's Before Update event and it would run if anything in the subform is trying to save. And as far as going through each control - you would need to have any of the controls in this code if you want to validate the entry. You can use validation at table level but that, and the control's validation rules are not all that flexible like this is. In the example I gave you, it would give the user ONE message which includes ALL of the places they messed up. Users like not having to get an error, and another, and another, and another. They like to know ONE TIME if they messed up and how to fix it.
 
Upvote 0
Yeah, I looked into that. However, the problem with that is a generic error message. I needed something to be specific. The code above that Bob posted took care of that specific message that I needed.

I appreciate your input. BTW, where exactly is that Required property? Is it at the table level or should it be a property of the control on the form itself? I found myself looking for it but couldn't find it.
 
Upvote 0
where exactly is that Required property?
It is a property of the field at the table level. You can put in your own message in the Validation Text property which will then be shown instead of the Access one, but again it is not as flexible as it is using the other method.
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top