Compare multiple values in ctl.Tag using Like function

coasterman

Registered User.
Local time
Today, 12:14
Joined
Oct 1, 2012
Messages
59
Is it possible to use Like when comparing the value in a ctl.Tag

I am trying to adapt the following validation routine so that it can be put behind a number of different command buttons to test for a different string in the tag property of the control. The code would be copied replacing the required string from the tag property, the example below is "*" but would of course be changed to some other string for subsequent cmd buttons.

Validation requirements vary on this form so for instance, and much simplified:

Scenario 1

Txt1 ,Txt2,Txt3,Txt4 need data

Scenario 2

Txt2, Txt3 need data

I was thinking if I could add two or more different values in the tag property and use Like I could validate only the controls needed from each cmd button.


If I change '=' to 'Like' in the following routine it finds an empty field (with no tag property) on another page of my Tab ctrl.

Code:
Private Sub cmdValpgGeneral_Click()
    Dim msg As String, Style As Integer, Title As String
    Dim nl As String, ctl As Control


    nl = vbNewLine & vbNewLine

    For Each ctl In Me.Controls
      If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
        If ctl.Tag = "*" And Trim(ctl & "") = "" Then
         msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
               "You can't save this record until this data is provided!" & nl & _
               "Enter the data and try again . . . "
          Style = vbCritical + vbOKOnly
          Title = "Required Data..."
          MsgBox msg, Style, Title
          ctl.SetFocus
          Cancel = True
          Exit For
        End If
      End If
    Next
End Sub

Is this a simple fix or am I getting into something rather more complex? I guess I need some way of having the code ignore ctrl's with no tag property value entered and still have the functionality of testing for Like?

Thanks for looking
 
Last edited:
I think I may have thought of an easier solution so thought I would share it.

If Mid(ctl.Tag, 3, 1) = "£" And Trim(ctl & "") = "" Then

By using the Mid function I can test for a character match at the position stated in the Start arg, in the case above "£" at position 3 in the tag property.

I might also have in my 2nd cmd button:

If Mid(ctl.Tag, 2, 1) = "*" And Trim(ctl & "") = "" Then

To test for "*" at position 2

I suppose there may be more elegant ways of doing this but it works for me here.
 

Users who are viewing this thread

Back
Top Bottom