Solved

textbox length

Posted on 2003-12-08
12
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 4

Accepted Solution

by:
learning_t0_pr0gram earned 250 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

739 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