Validation Rule

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Williams

    Validation Rule

    I am validating two text boxes to make sure they contain data. I have use Is
    Not Null in the Validation Property of the control but it doesn't seem to
    work. The Help shows Validation with code. How do I write the rule to check
    the user has completed the control?
    TIA
    Tony Williams


  • Allen Browne

    #2
    Re: Validation Rule

    Simplest approach is to open your table in design view, select the field,
    and set the Required property (lower pane) to Yes.

    If you want to do this with code in your form, you must use the BeforeUpdate
    event of the *form*. That way your code runs before the record is written
    even if the user never visited the control itself.

    Example of checking multiple fields:

    Private Sub Form_BeforeUpda te(Cancel As Integer)
    Dim strMsg As String

    If IsNull(Me.[SomeControl]) Then
    Cancel = True
    strMsg = strMsg & "SomeContro l required." & vbCrLf
    End If

    If IsNull(Me.[AnotherControl]) Then
    strMsg = strMsg & "AnotherCon trol required." & vbCrLf
    End If

    'etc for other controls

    If Cancel Then
    strMsg = strMsg & "Correct the entry, or press <Esc> to undo."
    MsgBox strMsg, vbExclamation, "Invalid data"
    End If
    End Sub

    --
    Allen Browne - Microsoft MVP. Perth, Western Australia.
    Tips for Access users - http://allenbrowne.com/tips.html
    Reply to group, rather than allenbrowne at mvps dot org.

    "Tony Williams" <tw@tcp.invalid > wrote in message
    news:c5tsrg$88p $1@titan.btinte rnet.com...[color=blue]
    > I am validating two text boxes to make sure they contain data. I have use[/color]
    Is[color=blue]
    > Not Null in the Validation Property of the control but it doesn't seem to
    > work. The Help shows Validation with code. How do I write the rule to[/color]
    check[color=blue]
    > the user has completed the control?
    > TIA
    > Tony Williams
    >
    >[/color]


    Comment

    • Tony Williams

      #3
      Re: Validation Rule

      Thanks Allen
      Tony
      "Allen Browne" <AllenBrowne@Se eSig.Invalid> wrote in message
      news:40827ba1$0 $16606$5a62ac22 @freenews.iinet .net.au...[color=blue]
      > Simplest approach is to open your table in design view, select the field,
      > and set the Required property (lower pane) to Yes.
      >
      > If you want to do this with code in your form, you must use the[/color]
      BeforeUpdate[color=blue]
      > event of the *form*. That way your code runs before the record is written
      > even if the user never visited the control itself.
      >
      > Example of checking multiple fields:
      >
      > Private Sub Form_BeforeUpda te(Cancel As Integer)
      > Dim strMsg As String
      >
      > If IsNull(Me.[SomeControl]) Then
      > Cancel = True
      > strMsg = strMsg & "SomeContro l required." & vbCrLf
      > End If
      >
      > If IsNull(Me.[AnotherControl]) Then
      > strMsg = strMsg & "AnotherCon trol required." & vbCrLf
      > End If
      >
      > 'etc for other controls
      >
      > If Cancel Then
      > strMsg = strMsg & "Correct the entry, or press <Esc> to undo."
      > MsgBox strMsg, vbExclamation, "Invalid data"
      > End If
      > End Sub
      >
      > --
      > Allen Browne - Microsoft MVP. Perth, Western Australia.
      > Tips for Access users - http://allenbrowne.com/tips.html
      > Reply to group, rather than allenbrowne at mvps dot org.
      >
      > "Tony Williams" <tw@tcp.invalid > wrote in message
      > news:c5tsrg$88p $1@titan.btinte rnet.com...[color=green]
      > > I am validating two text boxes to make sure they contain data. I have[/color][/color]
      use[color=blue]
      > Is[color=green]
      > > Not Null in the Validation Property of the control but it doesn't seem[/color][/color]
      to[color=blue][color=green]
      > > work. The Help shows Validation with code. How do I write the rule to[/color]
      > check[color=green]
      > > the user has completed the control?
      > > TIA
      > > Tony Williams
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...