inputmask alternative?

WalterInOz

Registered User.
Local time
Today, 17:36
Joined
Apr 11, 2006
Messages
93
I have an unbound textfield (txtGoTo) on my form in which users are only allowed to enter 4 digits, not more, not less. This is controlled by the input mask for that field and works just fine. However, there are a few things that I would like to improve to make it more user-friendly.

When I open the form I'd like the cursor to be in the txtGoTo field with the 4 placeholders blacked-out (selected) so that you can immediately enter the 4 digit number, press enter and off you go to the next form displaying the correct record.

At the moment I have me.txtGoTo.setfocus in the "on open" event but that doesn't do the trick. Also I don't see how you can code for the focus to shift automatically from the txtGoTo field to the button btnGo once the 4 digits have been entered.

I'm thinking along these lines:

If me.txtGoTo.value = 4 then
me.btnGo.setfocus
end if

But that doesn't work. What would?
 
Code:
Private Sub txtGoTo_Change()
    If Len(Me.txtGoTo.Text) = 4 Then Me.cmdButton.SetFocus
End Sub

Private Sub txtGoTo_KeyPress(KeyAscii As Integer)
    Call RestrictKey(KeyAscii, Me.txtGoTo)
End Sub

Public Function RestrictKey(KeyAscii, TextBox As TextBox) As Boolean
    Select Case KeyAscii
        Case 47 To 58:
            If Len(TextBox.Text) = 4 Then
                KeyAscii = 0
                Exit Function
            End If
        Case 8
        Case Else: KeyAscii = 0
    End Select
End Function

Here I just wrote it for you, no input mask involved
 
Bloody brilliant Mate! You're a champion. I don't understand the VBA you've used but it works. I'm beginning to understand the more simple coding that is sort of intuitive but this is way beyond me. I'd better start reading up on it.

The only thing thats not going at the moment is that the cursor isn't placed in the txtGoTo field when I open the form. Do you happen to know how to fix that as well?
 
1. In design view, right-click the form to bring up a pop-up menu.
2. Select "Tab Order" from that menu.
3. Drag your textbox to the top of the list.

(set the focus on form load should do the same thing though)
--------------------------------------------

In the above code:
47-58 = Keys 0 through 9
8 = the backspace key
0 = no keypress
 
Last edited:

Users who are viewing this thread

Back
Top Bottom