Link to home
Start Free TrialLog in
Avatar of supastar69
supastar69

asked on

textbox length

Hi, i am trying to simulate a user entering a credit card pin number (in a textbox), this textbox is in form1, what i need to do is once 4 numbers have been entered into the textbox, i want to switch to form2 ! so in other words i may require something like - 'if text1.text = Len...........=4 ?'.......then open form2....etc ANY IDEAS ??

Also relating to this - if the length of numbers entered is less then 4 - i will display a mesage in a label (to say please enter 4 numbers).....how woul this possibly be done ?
ASKER CERTIFIED SOLUTION
Avatar of learning_t0_pr0gram
learning_t0_pr0gram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of learning_t0_pr0gram
learning_t0_pr0gram

or instead of msgbox, for a label, put

Label1.caption = "Please enter 4 numbers"
Air code... :-)

private sub Text1_Change
     SendKeys "{Home}+{End}"
     If Len(Text1) = 4 Then
          Form2.Show
          Form2.SetFocus
          Unload Me
     End If

End Sub
for ictis's code, switch the:

"{Home}+{End}"

to

"{Home}" & "{End}"
Or...

private sub Text1_Change
     SendKeys "{Home}+{End}"
     If Len(Text1) = 4 Then
          Form2.Show
          Form2.SetFocus
          Unload Me
     Else
          Form1.Caption = "Please Enter 4 Digit PIN"
     End If

End Sub
save yourself some coding and make your textboxes an array ..
create 1 textbox , set its index to 0 , and then copy it for the other three textboxes....

Private Sub Form_Load()
    For i = 0 To Text1.Count - 1
        Text1(i).MaxLength = 4
    Next
End Sub


Private Sub Text1_Change(Index As Integer)
    If Len(Text1(Index).Text) = 4 Then
        If Index <> 3 Then
            Text1(Index + 1).SetFocus
        End If
    End If
End Sub

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If
End Sub
Why exactly do you want a form for each set of four digit numbers????
Good question mccainz2. I don't think you need the forms supastar.
I think you want something like this:
____-____-____-____ where ____ is a textbox and - is a label with caption = "-". If the user entered 4 digits, the form will focus the next textbox. So you'll have:

<Textbox(index 0)><label1><textbox (index 1)><label2><textbox (index 2)><label3><textbox (index 3)>
And you'll have a button (ButtonOK) with caption = "OK"
All Textbox MaxLength properties set to 4 (or 3, or 2...)

Private Sub Form_Load()
'Disable the OK-Button when this form is loaded
  ButtonOK.Enabled = False
End Sub

Private Sub Textbox_GotFocus(Index as Integer)
'Select the text if this textbox gets focussed, so user can re-enter numbers more easily, because text is selected
  Textbox(Index).SelStart = 0
  Textbox(Index).SelLength = Len(Textbox(Index).Text)
End Sub

Private Sub Textbox_KeyPress(Index as Integer, KeyAscii As Integer)
'Check if the input is a number or backspace. 48 means "0", 57 means "9". 8 means Backspace has been pressed
  If (KeyAscii < 48 or KeyAscii > 57) And Not KeyAscii = 8 then KeyAscii = 0
End Sub

Private Sub Textbox_Change(Index as integer)
  'Test if this textbox is filled already and switch to the next textbox
  If Len(Textbox(Index))=Textbox(Index).MaxLength Then _
    If Index < Textbox.Ubound Then Textbox(index+1).SetFocus
  TestOK
End Sub

Private Sub TestOK
'Tests if all fields have been filled in correctly. If so, user can press OK.
  Dim blnOK as boolean
  Dim i as integer
 
  blnOK = True
  i = Textbox.LBound
  While i <= Textbox.Ubound and blnOK
    blnOK = blnOK AND Len(Textbox(i)) = Textbox(i).MaxLength
    i = i + 1
  Wend
  ButtonOK.Enabled = blnOK
  if ButtonOK.Enabled then ButtonOK.SetFocus
End Sub

So now you'll have ONE form where a user can enter a credit card number. If one section is filled, the focus will be on the next section. If all sections have been filled, the focus will be on the OK-button. This button will be disabled while there's no complete input.

I hope this points you in the right direction. Look the way my code doesn't add too much business logic. The length you want in each textbox is set by the MaxLength property. You can add as many textboxes you want, as long as they're in the same control array. My code doesn't bother the user with messageboxes and stuff.

Good luck.
Btw, if you read and understand the code and events above, you then know how to add labels and place text in them in relation to the input of a textbox.
One more thing: You can copy and paste this code simply into a new VB Project. All you have to do is to place a number of textboxes on the form and put them into a control array. Give them all the name "Textbox". Also, add a button with the name "ButtonOK"... Add labels if you want too. Go ahaid and see. Code has been tested and found working.
Avatar of supastar69

ASKER

ok Thanks everyone - but ill accept the 1st answer (i only ended up needing it nice and simple)

Thanks everyone else !! :)
The situation you now have is indeed nice and simple... in code that is.
I do think however, that it is not nice and simple for a user. I, as a user, wouldn't find it nice and simple if I had to enter a credit card number in several different forms. I believe that - when developing applications - you always have to see it from a user's point of view.

Anyway, I'm glad you have it got to work now.

Cheers.