A2K: problem with limitfieldsize code that restricts number of input characters

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

    A2K: problem with limitfieldsize code that restricts number of input characters

    I use this code from http://support.microsoft.com/kb/210047 to stop more
    than 50 characters being input into a textbox.

    ****code start****
    Sub LimitFieldSize (KeyAscii, MAXLENGTH)
    Dim C As Control
    Dim CLen As Integer

    Set C = Screen.ActiveCo ntrol

    ' Exit if a non-printable character is typed.
    If KeyAscii < 32 Then Exit Sub

    ' Exit if typing replaces a selection.
    If C.SelLength 0 Then Exit Sub

    ' Fetch length of current contents + 1 for the character typed.
    CLen = Len(C.Text & "") + 1

    ' Are there trailing spaces to contend with?
    If C.SelStart + 1 CLen Then CLen = C.SelStart + 1

    ' Is length of string greater than max?
    If CLen MAXLENGTH Then
    Beep
    KeyAscii = 0
    End If
    End Sub
    ****code end****


    The problem is that once the limit is reached the count stays at 50 even
    after the user deletes a few characters and then tries to enter something
    different. This means the caret aka cursor, just stays locked and no more
    input is allowed.

    I have been trying to rewrite the code without success - can anyone see what
    needs doing to make this code work in the above situation?

    thanks



  • Allen Browne

    #2
    Re: problem with limitfieldsize code that restricts number of input characters

    "Deano" <deano@mailinat or.comwrote in message
    news:611rnhF1sa 2jhU1@mid.indiv idual.net...
    >I use this code from http://support.microsoft.com/kb/210047 to stop more
    than 50 characters being input into a textbox. [snip]
    The problem is that once the limit is reached the count stays at 50 even
    after the user deletes a few characters ...
    Haven't fooled with that particular solution, but here's an alternative:
    Unbound text box: limiting entry length
    at:
    How to limit the number of characters that can be typed into a text box in a Microsoft Access database, even where the box is not bound to a field that limits its length.


    --
    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.

    Comment

    • Deano

      #3
      Re: problem with limitfieldsize code that restricts number of input characters


      "Allen Browne" <AllenBrowne@Se eSig.Invalidwro te in message
      news:47abbf00$0 $5577$5a62ac22@ per-qv1-newsreader-01.iinet.net.au ...
      "Deano" <deano@mailinat or.comwrote in message
      news:611rnhF1sa 2jhU1@mid.indiv idual.net...
      I use this code from http://support.microsoft.com/kb/210047 to stop more
      than 50 characters being input into a textbox. [snip]
      The problem is that once the limit is reached the count stays at 50 even
      after the user deletes a few characters ...
      >
      Haven't fooled with that particular solution, but here's an alternative:
      Unbound text box: limiting entry length
      at:
      http://allenbrowne.com/ser-34.html
      I might use that where I can, but I'm mostly using bound textboxes.


      Comment

      • Salad

        #4
        Re: A2K: problem with limitfieldsize code that restricts number ofinput characters

        Deano wrote:
        I use this code from http://support.microsoft.com/kb/210047 to stop more
        than 50 characters being input into a textbox.
        >
        ****code start****
        Sub LimitFieldSize (KeyAscii, MAXLENGTH)
        Dim C As Control
        Dim CLen As Integer
        >
        Set C = Screen.ActiveCo ntrol
        >
        ' Exit if a non-printable character is typed.
        If KeyAscii < 32 Then Exit Sub
        >
        ' Exit if typing replaces a selection.
        If C.SelLength 0 Then Exit Sub
        >
        ' Fetch length of current contents + 1 for the character typed.
        CLen = Len(C.Text & "") + 1
        >
        ' Are there trailing spaces to contend with?
        If C.SelStart + 1 CLen Then CLen = C.SelStart + 1
        >
        ' Is length of string greater than max?
        If CLen MAXLENGTH Then
        Beep
        KeyAscii = 0
        End If
        End Sub
        ****code end****
        >
        >
        The problem is that once the limit is reached the count stays at 50 even
        after the user deletes a few characters and then tries to enter something
        different. This means the caret aka cursor, just stays locked and no more
        input is allowed.
        >
        I have been trying to rewrite the code without success - can anyone see what
        needs doing to make this code work in the above situation?
        >
        thanks
        >
        What happens if you remove the
        KeyAscii = 0
        line? It seems you are setting it to null (guess I need a Ascii chart).
        What would happen if you set KeyAscii to a space (32).

        Lava

        Comment

        Working...