PDA

View Full Version : Solved: Prevent numeric or symbol and if function in userform



slamet Harto
05-12-2008, 03:02 AM
Dear all

Is there a way to prevent any numeric or symbol in a userform
I use the following code, but seem does not work at all

Private Sub TxtCMName_Change()
TxtCMName.Text = UCase(Left(TxtCMName.Text, 30))
'To prevent any numerical or symbol
If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ/!!@#$%^&*() ", Chr KeyAscii)) = 0 Then KeyAscii = 0
End Sub

Other case:
Kindly advise me, what is my mistake with if function in userform
If total of digit at the textbox "Txtno" is not 15 digit or "Txtno" is blank
then show message box

Here is the code that I'm using.
If TxtNo.Text = "" Then
MsgBox "Enter the Number! ", vbInformation, "Error message"
TxtCard1.SetFocus
Exit Sub

ElseIf TxtNo.Text <> 15 Then
MsgBox "Invalid Card Number! ", vbInformation, "Please check total digit "
TxtCard1.SetFocus
Exit Sub
End if


Thanks for your assistance.
Best rgds, Harto

Edited by Aussiebear: to group the sections of code within the vba Tags

Bob Phillips
05-12-2008, 03:11 AM
Pewrhaps this is what you want



Private Sub TxtCMName_Change()
TxtCMName.Text = UCase(Left(TxtCMName.Text, 30))
'To prevent any numerical or symbol
If TxtCMName.Text Like "*[ABCDEFGHIJKLMNOPQRSTUVWXYZ/!!@#$%^&*() ]*" Then KeyAscii = 0
End Sub

Aussiebear
05-12-2008, 04:10 AM
Hi Harto,

When posting sections of code, please use the vba tag function. It makes the code much easier to read.

slamet Harto
05-13-2008, 02:14 AM
Dear Xld

It doesn't work at all. What I want is the user have to input with character only instead of numeric and any symbol.

Your advise would be appreciate it.
Rgds, harto

Bob Phillips
05-13-2008, 02:23 AM
Use the proper event



Private Sub TxtCMName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Chr(KeyAscii) Like "*[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\/!!@#$%^&*() ]*" Then

KeyAscii = 0
End If
End Sub

slamet Harto
05-13-2008, 02:41 AM
Geting closer now. I'll fix it.

Thanks a bunch.

With my respect, harto