Results 1 to 4 of 4

Thread: control array

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Location
    Arkansas
    Posts
    35

    control array

    Hello,
    I have a validate routine that works fine as is but I need to be able to let the user cancel if they wish, before completing the form. They cannot do so this way. I want to create my own procedure and attach it to the save command so the form is completed before it validates but I cannot seem to get it right.Could someone explain how to create a procedure using a control array
    Here is what I have for the validate. This just checks to see if the textboxes are empty.
    Public Sub txtFields_Validate(Index as Integer, KeepFocus As Boolean)
    Dim dmv As String
    If txtFields(Index) < 1 Or txtFields(Index) > 21 Then Exit Function
    If Len(txtFields(Index).Text) > 0 Then Exit Function
    dmv = ""
    Select Case Index
    Case 2

    dmv = "Issued to"

    Case 3
    dmv = "Contact"
    Case 4
    dmv = "Phone"
    Case 5
    dmv = "Permit Amount"
    Case 6
    dmv = "Check or Cash"
    Case 11
    dmv = "County Road"
    Case 12
    dmv = "Location"
    Case 13
    dmv = "Desc of work"
    Case 14
    dmv = "Authorized BY"
    Case 15
    dmv = "Issued BY"



    End Select

    If dmv <> "" Then
    KeepFocus = True
    txtFields(Index).BackColor = vbYellow
    txtFields(Index).ToolTipText = ("The " & dmv & " Field may not be left Blank.")
    End If
    End Sub

  2. #2
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: control array

    Alexus, I usually put a Cancel command button on the form to serve as an escape. After pressing that, and information has been typed in, I usually supply a warning message that allows them to continue, just in case they hit the Cancel button by mistake. If they entered nothing, the warning can be skipped.

    The Form Unload X button needs to be trapped also. Users frequently hit that X by mistake, and if they do not get a chance to save first, it's a bit frustrating.
    Doctor Ed

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: control array

    Good effort on the code tags. It's [highlight=vb], and the [/highlight] close tag goes at the end of the code.

    There's a few ways you can tighten up this code, btw. What I would recommend is that you put the dmv strings ("Contact", "Phone", etc...) in the tag properties of the textboxes, that way you don't have to waste lines of code typing them out. Also, the With...End With construct makes code less messy.
    Code:
    Public Sub txtFields_Validate(Index as Integer, KeepFocus As Boolean) 
        With txtFields(Index)
            If Len(.Text) = 0 And Len(.Tag) <> 0 Then
                KeepFocus = True
                .BackColor = vbYellow
                .ToolTipText = "The '" & .Tag & "' field may not be left blank."
            End If
        End With
    End Sub
    This makes it very easy for you to manage the logic. Simply put the field name in the .Tag property of any textbox that should have this logic. For the other textboxes, leaving the .Tag blank will bypass them in the validation logic.

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: control array

    As for your question, be sure to set the Cancel button's CausesValidation property to False if you go that route.

    If you wanted to not do any validation until they hit the OK button, (my personal preference,) then remove the validation procedures altogether and instead add something like this to the OK button's click event:
    Code:
    Private Sub cmdOK_Click()
        Dim i As Long
        
        For i = txtFields.LBound To txtFields.Ubound
            If Len(txtFields(i).Text) = 0 And Len(txtFields(i).Tag) <> 0 Then
                MsgBox "The '" & txtFields(i).Tag & "' field may not be left blank.", vbInformation, "Notice"
                txtFields(i).SetFocus
                Exit Sub
            End If
        Next
        ' Data is valid
    End Sub
    (This assumes you implement the .Tag property suggestion.)

Posting Permissions

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



Click Here to Expand Forum to Full Width