Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

textbox length

Posted on 2003-12-08
12
Medium Priority
?
557 Views
Last Modified: 2010-05-01
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 ?
0
Comment
Question by:supastar69
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 4

Accepted Solution

by:
learning_t0_pr0gram earned 750 total points
ID: 9900281
if len(Text1.text) = 4 then
form2.show
else
msgbox "Please enter 4 numbers"
end if
0
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
ID: 9900286
or instead of msgbox, for a label, put

Label1.caption = "Please enter 4 numbers"
0
 
LVL 1

Expert Comment

by:ictis
ID: 9900307
Air code... :-)

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

End Sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:learning_t0_pr0gram
ID: 9900325
for ictis's code, switch the:

"{Home}+{End}"

to

"{Home}" & "{End}"
0
 
LVL 1

Expert Comment

by:ictis
ID: 9900327
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
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9900539
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
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9900593
Why exactly do you want a form for each set of four digit numbers????
0
 
LVL 5

Expert Comment

by:IThema
ID: 9902849
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.
0
 
LVL 5

Expert Comment

by:IThema
ID: 9902866
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.
0
 
LVL 5

Expert Comment

by:IThema
ID: 9902874
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.
0
 

Author Comment

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

Thanks everyone else !! :)
0
 
LVL 5

Expert Comment

by:IThema
ID: 9927697
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.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question