VBA how do I get 2 textboxes to display different strings based on keypress?

yytsunamiyy

Well-known Member
Joined
Mar 17, 2008
Messages
963
Hi all,

after a lot of silent reading and marvelling at your wisdom as a guest I decided to register und molest you with my problem.. :smile:

I have a Userform whose purpose it is to capture payment information, in particular Creditcard information. To this end it has (amongst other elements) 2 textboxes - textboxCardNr1 and TextboxCardnr2.

the User enters the cardnr in Textboxcardnr1 as, e.g. 4321098765432109. Through the code below I transform this into the more readable number
4321-0987-6543-2109. I capture the keypress instance to only allow numbers to be entered in this field and to simultaniously update TextboxCardNr2 with the keystrokes.

On exiting TextboxCardNr1 the content of TextboxCardNr2 is copied to the clipboard to be available for pasting on a secure website in the approriate field.

My problem is, that when the user makes a mistake and changes the number in TextboxCardNr1, those changes are not automatically mirrored in TextboxCardNr2.

I want the easy to read form of the card - with the dashes, to be saved. The website the user is pasting the content of TextboxCardnr2 to does not accept the number with dashes.

Any help on how to ensure that correct creditcardnumber is available without dashes for pasting but shown with dashes for easier reading would be greatly appreciated.
Also, if anybody has code or any other ideas on how to check the validity of a card - I guess its some kind of checksum in the numbers - a tip would be highly appreciated.

The code for TextboxCardNr1 follows (~Nr2 does not have code as the user never touches it):


Code:
 Private Sub TextBoxCardNr1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim taste As String
Dim wert As String

taste = VBA.Chr(KeyAscii)
wert = Me.TextBoxCardNr1.Text

KeyAscii = 0
If taste = "0" Or taste = "1" Or taste = "2" Or taste = "3" Or taste = "4" Or taste = "5" Or taste = "6" Or taste = "7" Or taste = "8" Or taste = "9" Then[INDENT]     If Not Me.TextBoxCardNr2.Text Like "################" Then
        Me.TextBoxCardNr2.Text = Me.TextBoxCardNr2.Text & taste
    End If
[/INDENT][INDENT]     If wert = "" Or wert Like "#" Or wert Like "##" Or wert Like "###" Then[INDENT]         wert = wert & taste
[/INDENT]ElseIf wert Like "####" Then[INDENT]         wert = wert & "-" & taste
[/INDENT]ElseIf wert Like "####-" Or wert Like "####-#" Or wert Like "####-##" Or wert Like "####-###" Then[INDENT]         wert = wert & taste
[/INDENT]ElseIf wert Like "####-####" Then[INDENT]         wert = wert & "-" & taste
[/INDENT]ElseIf wert Like "####-####-" Or wert Like "####-####-#" Or wert Like "####-####-##" Or wert Like "####-####-###" Then[INDENT]         wert = wert & taste
[/INDENT]ElseIf wert Like "####-####-####" Then[INDENT]         wert = wert & "-" & taste
[/INDENT]ElseIf wert Like "####-####-####-" Or wert Like "####-####-####-#" Or wert Like "####-####-####-##" Or wert Like "####-####-####-###" Then[INDENT]         wert = wert & taste
[/INDENT]End If

    Me.TextBoxCardNr1.Text = wert
[/INDENT]Else[INDENT]Me.TextBoxCardNr1.Text = wert
[/INDENT]End If

End Sub

Private Sub TextBoxCardNr1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Me.ComboBoxCardType.Text = "VS" And TextBoxCardNr1.Text Like "4###-####-####-####" Then[INDENT]Me.TextBoxCardNr2.SelStart = 0
             Me.TextBoxCardNr2.SelLength = 50
TextBoxCardNr2.Copy
             Me.TextBoxCardSecCode.SetFocus
[/INDENT]ElseIf Me.ComboBoxCardType.Text = "MA" And TextBoxCardNr1.Text Like "5###-####-####-####" Then[INDENT]             Me.TextBoxCardNr2.SelStart = 0
             Me.TextBoxCardNr2.SelLength = 50
             TextBoxCardNr2.Copy
             Me.TextBoxCardSecCode.SetFocus
[/INDENT]ElseIf Me.ComboBoxCardType.Text = "AE" And TextBoxCardNr1.Text Like "3###-####-####-####" Then[INDENT]             Me.TextBoxCardNr2.SelStart = 0
             Me.TextBoxCardNr2.SelLength = 50
             TextBoxCardNr2.Copy
             Me.TextBoxCardSecCode.SetFocus
[/INDENT]ElseIf Me.ComboBoxCardType.Text = "DI" And TextBoxCardNr1.Text Like "3###-####-####-####" Then[INDENT]             Me.TextBoxCardNr2.SelStart = 0
             Me.TextBoxCardNr2.SelLength = 50
             TextBoxCardNr2.Copy
             Me.TextBoxCardSecCode.SetFocus
[/INDENT]ElseIf Not TextBoxCardNr1.Text = "" Then[INDENT]     MsgBox "Esta tarjeta no puede ser de tipo " & Me.ComboBoxCardType.Text & "." & vbCr & vbCr & _
            "Por favor examinar tipo y numero." & vbCr & vbCr & _
            "Numeros de tarjetas empiezan con" & vbCr & _
            "3 son de Diners Club o de AmEx," & vbCr & _
            "4 son de Visa," & vbCr & _
            "5 son de Mastercard.", vbOKOnly Or vbExclamation
     Me.TextBoxCardNr2.Text = ""
     Me.TextBoxCardNr1.Text = ""
     Me.ComboBoxCardType.SetFocus
[/INDENT]End If
            

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,226,527
Messages
6,191,574
Members
453,665
Latest member
WaterWorks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top