How to limit input length in Access form textbox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    How to limit input length in Access form textbox?

    Hi all.

    Does a textbox on an Access form have an equivalent of the VB6 .MaxLength property? I need to limit the length of an input string. It's not bound to anything, just used in code, but I'd prefer to limit it in the control rather than validating afterward.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    Specifically no.
    But you might want to check out the 'InputMask Property'. It may be able to give the required functionality.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by NeoPa
      Specifically no.
      But you might want to check out the 'InputMask Property'. It may be able to give the required functionality.
      Nuts! I was hoping to avoid that.

      Thanks, anyway.

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Originally posted by Killer42
        Hi all.

        Does a textbox on an Access form have an equivalent of the VB6 .MaxLength property? I need to limit the length of an input string. It's not bound to anything, just used in code, but I'd prefer to limit it in the control rather than validating afterward.
        In code you could

        Code:
         Dim someStr As String 
         
          someStr = InputBox("Enter some string: ")
          If Len(someStr) > 100 Then
        	 someStr = Left(someStr,100)
        	 'Msg to user
          End If

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          Originally posted by mmccarthy
          In code you could

          Code:
           Dim someStr As String 
           
          someStr = InputBox("Enter some string: ")
          If Len(someStr) > 100 Then
          	 someStr = Left(someStr,100)
          	 'Msg to user
          End If
          Or if textbox, damn I should read these things better ...

          Code:
           Dim someStr As String 
           
          someStr = Me.textbox
          If Len(someStr) > 100 Then
          	 someStr = Left(someStr,100)
          	 Me.textbox = someStr
          	 'Msg to user
          End If

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Originally posted by mmccarthy
            Or if textbox, damn I should read these things better ...

            Code:
             Dim someStr As String 
             
            someStr = Me.textbox
            If Len(someStr) > 100 Then
            	 someStr = Left(someStr,100)
            	 Me.textbox = someStr
            	 'Msg to user
            End If
            I'm now wondering if you could get the realtime text value of the input and validate on KeyPress event. Preformance ???

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by mmccarthy
              I'm now wondering if you could get the realtime text value of the input and validate on KeyPress event. Preformance ???
              It's ok, I can handle the validation alright. I just didn't want to have to, because .MaxLength is ever so much simpler.

              Maybe I need to use the VB6 library ;)

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Originally posted by Killer42
                It's ok, I can handle the validation alright. I just didn't want to have to, because .MaxLength is ever so much simpler.

                Maybe I need to use the VB6 library ;)
                Worth a try as it should be on the list. Never tried it for something like this though.

                Mary

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by mmccarthy
                  Worth a try as it should be on the list. Never tried it for something like this though.
                  Hm... nah!

                  I've just spent a few minutes browsing the References list and don't really like what I saw. Think I'll just bite the bullet and put in some code to do validation (or real-time feedback) on the form.

                  Comment

                  • MMcCarthy
                    Recognized Expert MVP
                    • Aug 2006
                    • 14387

                    #10
                    Originally posted by Killer42
                    Hm... nah!

                    I've just spent a few minutes browsing the References list and don't really like what I saw. Think I'll just bite the bullet and put in some code to do validation (or real-time feedback) on the form.
                    Yeah!

                    It is a bit of a minefield.

                    When you get some time :)

                    Have a look on msdn regarding 'Automation' from Access.

                    Mary

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by mmccarthy
                      Yeah!
                      It is a bit of a minefield.
                      When you get some time :)
                      Have a look on msdn regarding 'Automation' from Access.
                      Will do.

                      In the meantime, I ended up going with something very simple. I just turn the field background red as long as the value is not within the required length. Although it doesn't account for the possibility of a black (zero) background, here's the routine I used.
                      Code:
                      Private Sub tx_Change()
                        Static HeldColour As Long
                        With tx
                          If Len(.Text) > 8 Then
                            If HeldColour = 0 Then
                              Beep
                              HeldColour = .BackColor
                              .BackColor = vbRed
                            End If
                          Else
                            If HeldColour <> 0 Then
                              .BackColor = HeldColour
                              HeldColour = 0
                            End If
                          End If
                        End With
                      End Sub

                      Comment

                      • MMcCarthy
                        Recognized Expert MVP
                        • Aug 2006
                        • 14387

                        #12
                        If you wanted even more control:

                        Code:
                         if Len(tx.Text) > 8 then 
                        Me.someothercontrol.SetFocus
                        Me.tx = Left(Me.tx, 8)
                        Msgbox("You are limited to 8 characters ....", vbOKOnly + vbWarning)
                        Me.tx.SetFocus
                        Else
                        >
                        >
                        >

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by mmccarthy
                          If you wanted even more control: ...
                          Yeah, I thought about chopping it off, but decided against it. The users know the limits, if they choose to ignore the warning then so be it.

                          One technique I've used in similar cases at times is to have a timer on the form. To highlight a field I do one of two things:
                          • set a value in the .Tag property (VB6, not sure about Access) and the timer watches for these flags and flashes the controls on and off.
                          • Highlight the control (typically by setting background colour), and also flag it in .Tag or whatever. The timer gradually fades the colour back to normal. This one looks quite good, actually. You get a beep, the field turns (typically) bright red, then over a few seconds it fades back to normal, but it doesn't interfere with your typing or anything.
                          (Note, in cases such as these the warning message might be displayed in a particular place on the form, rather than as a message box).

                          Damnit, I've got to stop spending so much time here - I keep getting carried away. :(

                          Comment

                          • MMcCarthy
                            Recognized Expert MVP
                            • Aug 2006
                            • 14387

                            #14
                            Never thought about using the control tags in this way. Sounds interesting.

                            Mary

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #15
                              Originally posted by mmccarthy
                              Never thought about using the control tags in this way. Sounds interesting.
                              I find them quite useful for this kind of thing.

                              One caveat, though.

                              I recommend that one always assume there may be other info in there. I try to
                              • Add my flag onto the end and make it distinctive, with something like "/FLASHFIELD", and
                              • Remove it without disturbing any other contents.

                              That way, you can make use of multiple routines that use the .Tag property, without any cross-interference.

                              Comment

                              Working...