Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2005

    Unanswered: Data Validation Routine

    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?

    Sub ValidateFields()
        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)
              Exit Sub
              ok_to_continue = True
           End If
        End If
    End Sub

  2. #2
    Join Date
    Feb 2004
    Chicago, IL
    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
                    Do Something 
                End If
            End If
        Next varLabel
    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.

  3. #3
    Join Date
    Sep 2005
    Thanks for the reply. I've never used tags before. Can you tell me how they work?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts