Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    Barnsley, Yorkshire, England
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validation at Control level (Access 97)

    i need to force users to enter data into a particular text box, in a particular manner, that is bound to a number field in the underlying table.

    I am fine on checking if the data is of the right format using the Len function, but i am having problems when it is not. What i want to do is set the control value back to "" i.e. erase what was entered, and set the focus back to that control, (in effect, a loop until the user gets it right) but i have tried using the On Exit, BeforeUpdate and AfterUpdate events, but each time i either get told i can't set the focus back, or the cursor tabs to the next control.

    What am i doing wrong?

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Validation at Control level (Access 97)

    Try something like this in the Before Update event of your control:<pre>Private Sub TextBox1_BeforeUpdate(Cancel As Integer)
    If Len(Me!TextBox1) <> 5 Then
    Me!TextBox1.Undo
    Cancel = True
    End If
    End Sub</pre>

    Francois

  3. #3
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    Barnsley, Yorkshire, England
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Validation at Control level (Access 97)

    Thanks for your reply Francois. That works in terms of returning the cursor to the textbox, but does erase the entered value.

    Any ideas??

  4. #4
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    Barnsley, Yorkshire, England
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Validation at Control level (Access 97)

    Sorry, i meant does not erase the previous value, and more importantly, the user can still click into another control...

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Validation at Control level (Access 97)

    Ok,
    Try this in the on exit event of your control, but if the user is changing the text, he won't be able to see what was in it before. This is why I prefer the function in Before Update. Up to you.

    <pre>Private Sub TextBox1_Exit(Cancel As Integer)
    If Len(Me!TextBox1) <> 5 Then
    Me!TextBox1 = ""
    Cancel = True
    End If
    End Sub

    </pre>

    Francois

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Validation at Control level (Access 97)

    I generally build a function that tests any required controls and then sets the focus back to the first control that doesn't validate. I call that in the BeforeUpdate event of the Form.

    If you don't want the user to be able to leave the control (when you validate at the individual control), just Cancel the BeforeUpdate event but don't undo the value. You could pop up a messagebox explaining the problem but force them to stay there until they fix it. The only thing you won't be able to handle that way is if the user clicks the Esc button, because that will undo form changes and reset the control to its original empty state. Of course, then you could use the Form level event to validate any required fields. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  7. #7
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Validation at Control level (Access 97)

    Exactly what are you trying to check for? Between an input mask and validation rules, you probably wouldn't need any code at all.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

Posting Permissions

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