How to test the length of a field on data entry?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BobS
    New Member
    • Jul 2010
    • 10

    #1

    How to test the length of a field on data entry?

    This seems very simple but I can't find how to do it. I wrote a form that is in data entry mode when the data is entered. The primary key field should always be a length of 5, how do I check the length and give a error if less then 5?

    Thanks,
    Bob
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Im guessing you have the user type the key? If so, you can add code to the control's BeforeUpdate. Asumming your textbox is called: tb_Primary you do like this.

    Select your textbox, open the properties window, then the events tab. Find the BeforeUpdate, and click the 3 ... on the right hand side of the window, and select Event Procedure. You will now be taken to the VBA editor looking like this:
    Code:
    Private Sub tb_Primary_BeforeUpdate(Cancel As Integer)
    
    End Sub
    Modify it to look like this, and you should be good to go:
    Code:
    Private Sub tb_Primary_BeforeUpdate(Cancel As Integer)
      If len(Me.tb_Primary)<>5 then
        'Input error  
          Msgbox "Primary Key must be 5 charecters long, please correct"
        'Cancel the update of the field
          Cancel=true 
      End If
    End Sub

    Comment

    • BobS
      New Member
      • Jul 2010
      • 10

      #3
      Thank you for your answer. I actually tried something similar to this under BeforeUpdate and I also tried your code also. I agree that the code will test the field length. The problem seems to be this is not executed because the form is marked as a DATA Entry (yes). I moved the code into another form where DATA Entry was marked to NO and it worked fine. Any suggestions?

      Thanks,
      Bob

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        Did you use the beforeUpdate of the form or of the textbox?

        Comment

        • BobS
          New Member
          • Jul 2010
          • 10

          #5
          I was doing the text box. I changed to the before/update on the form and it worked. I guess you have to do this because I had set the data entry property of the form to 'yes'.

          Thank you for your time & help.

          Bob

          Comment

          Working...