Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Beany
    New Member
    • Nov 2006
    • 173

    Validation

    Hi,

    i have a form that requires validation. i am using the following code:

    Private Sub Form_BeforeUpda te(Cancel As Integer)

    If IsNull(Me.numbe r) Then
    MsgBox "number is required", vbOKOnly
    Cancel = True
    Me.number.SetFo cus
    End If


    i have repeated the above code for other fields.........

    The problem i have is, if i click 'x' and before it allows me to exit, it brings up the validations, one by one (so annoying).

    Is there any way THAT it can show all the validations at once??? in one message????

    THANKS
  • Tanis
    New Member
    • Mar 2006
    • 143

    #2
    You could use a function. Then call this function from your text boxes.

    Public Function ValidateNumeric (strText As String) As Boolean
    ValidateNumeric = CBool(strText = "" Or strText = "-" Or strText = "-." Or strText = "." Or IsNumeric(strTe xt))
    MsgBox "Not Numeric"
    End Function

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by Beany
      Is there any way THAT it can show all the validations at once??? in one message????
      What about a routine you can call (much like that proposed by Tanis) which does something like...
      Code:
      Private Function FieldsAreOK(ByVal ShowMsgBox As Boolean) As Boolean
        ClearMessage
        If <Validation1> Then
          AddToMessage "Field1 must be numeric"
        End If
        If <Validation2> Then
          AddToMessage "Field2 must be numeric"
        End If
        If <Validation3> Then
          AddToMessage "Field3 doesn’t look right for some reason"
        End If
        ... and so on...
        If Msg <> ""
          If ShowMsgBox then
            MessageBox "The following errors should be addressed:" & VbNewLine & Msg
          End If
        Else
          FieldsAreOK = True
        End If
      And you'd define a form variable Msg As String, and a couple of subs as follows
      Code:
      Private Sub ClearMessage
        Msg = ""
        ' (You [I]could[/I] just do this in your code instead of calling the Sub)
      End Sub
      
      Private Sub AddToMessage (ByVal What As String)
        Msg = Msg & iif(Msg = "", "", VbNewLine) & What
      End Sub
      You could also get it to highlight the fields in some way (colour, border, font style, etc etc) but then you have the issue of changing them back.

      Comment

      Working...