I am trying to write a rather generic field validation routine for a form that I have and I need some advice. My form has a series of option buttons, followed by text boxes where users can enter values. I need to check to see if the option button is enabled, and if it is, and the text box is empty, pop up an error message stating the field name.
Here's what I have so far, but the fieldname is not being populated. Is this the best way to do something like this?
Dim fieldname As String
Msg = "You have enabled the " & fieldname & " button but the field is blank."
Title = "Data Validation Error"
Style = vbCritical + vbYesOnly
If DaysOfSupplyOption.Enabled = True Then
If Trim(DaysOfSupplyText.Value & "") = "" Then
fieldname = "Days of Supply Option"
MyAnswer = MsgBox(Msg, Style, Title)
ok_to_continue = True
What I would do is link your option button with a text box using the Tag field of the option button. Then loop through all of the option buttons and check the text box associated with the option button if it is enabled.
To loop through controls use the For Each loop:
For Each varOption In Me.Controls
If varOption.ControlType = acOptionButton Then
strValue = Me.Controls(varOption.Tag).Value
If strName & ""="" Then
varOption must be a variant type. This will loop through all of your controls on the form and only check when it comes across an Option Button. You will have to put in the check and error message in the Do Something part.