Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Posts
    23

    Unanswered: Access 2007 Forms

    Hi All,

    I'm new to MS Access forms. I have a combobox with 3 fields A, B, C. and a textbox. When the option B is selected the length of the characters in textbox should not exceed 3. I'm trying to write VBA code for this as below

    Private Sub TEXTBOX_KeyPress(KeyAscii As Integer)
    If [TEXTBOX].length && [COMBOBOX] = "B" Then
    MsgBox "Error"
    End Sub

    But the "length" field is not available in TEXTBOX, Could any one of you please let me know on how to resolve this.

    Thanks you.

  2. #2
    Join Date
    May 2005
    Posts
    1,191
    Try something like:
    Code:
    If (Len(me.[TEXTBOX])) AND (me.[COMBOBOX] = "B") Then
    MsgBox "Error"
    End Sub
    Something to keep in mind though is when the key events fire. If I recall correctly, the keypress event happens when the user pushes the key down and holds it, but before the letter is appended to the end of the string. So I think you may need the keydown event, and you're also going to then want to cancel the keystroke, I think.
    Me.Geek = True

  3. #3
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    You can control the length of a field with the input mask if that's what you want? If I am interpreting what you are needing correctly then I think you need the following code in the AfterUpdate event for you combo box.

    Code:
    If [ComboBoxName] = "B" Then
       If Len(Me.[TextBoxName]) > 3 Then Me.[TextBoxName] = Left(Me.[TextBoxName],3)
       Me.[TextBoxName].InputMask = "AAA"
    'Else[If]
    '  ... 
    EndIf
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  4. #4
    Join Date
    Jun 2009
    Posts
    23
    It worked for me. Thanks a lot.

  5. #5
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    You're welcome
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

Posting Permissions

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