Consulting

Results 1 to 5 of 5

Thread: forms-comboboxes & textboxes

  1. #1

    forms-comboboxes & textboxes

    hi

    i would like to restrict the entry into a combobox and a textbox and also for the cursor to move to the next text or combobox

    for example

    in textbox1 i would like the entry to be restricted to a number between 10000 and 99999 then once the five digits have been entered for the cursor to move to combobox1 and for this entry to be restricted to an entry from the dropdown

    thanks

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This should take care of the textbox validation.
    Private Sub TextBox1_Change()
    Static abort As Boolean
    If abort Then abort = False: Exit Sub
    With Me
        If Val(.TextBox1.Text) <> .TextBox1.Value Then
            abort = True
            .TextBox1.Text = Format(Val(.TextBox1.Text), "0")
        End If
        If Len(.TextBox1.Text) >= 5 Then Me.ComboBox1.SetFocus
    End With
    End Sub
    The rest can be done by setting the .MatchRequired property to True.

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Is this the same method used when say entering in a serial or product number for new software where it boots you across to the next textbox?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    In userforms, there are two ways to go from control to control using the keyboard

    The technique in the code uses the .SetFocus method(?) to set the focus to a particular control.

    The other, more common technique, uses the Tab Order to control what control gets the focus after the user presses Enter or Tab.

    Since the request was to move the focus after the (fifth) press of a number key, the normal Tab Order technique wouldn't work, requiring the .SetFocus technique of focus control.

  5. #5
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You could also:
    1. Set the Textbox's MaxLength property to 5
    2. Set its Autotab property to True
    3. Use the BeforeUpdate event to validate:

    [vba]Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Not (Val(TextBox1) >= 10000 And Val(TextBox1) <= 99999) Then
    MsgBox "Value must be between 10000 and 99999"
    Cancel = True
    End If
    End Sub[/vba]
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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