Solved

textbox length

Posted on 2003-12-08
12
547 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 250 total points
Comment Utility
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
Comment Utility
or instead of msgbox, for a label, put

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

Expert Comment

by:ictis
Comment Utility
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
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
Comment Utility
for ictis's code, switch the:

"{Home}+{End}"

to

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

Expert Comment

by:ictis
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 5

Expert Comment

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

Expert Comment

by:IThema
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now