View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jean-Pierre D via OfficeKB.com Jean-Pierre D via OfficeKB.com is offline
external usenet poster
 
Posts: 31
Default userinput is wrong (check in vba)

Hi Dave,

Thanks for your input.
Can you explain the Me.Label1.Caption = "" part of your code please ?
Thanks,
Pierre



Dave Peterson wrote:
I created a userform with 10 textboxes, 1 label and 2 commandbuttons.

This is the code that's behind the userform:

Option Explicit
Private Sub UserForm_Initialize()
Me.Label1.Caption = ""
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
Dim OkToContinue As Boolean

OkToContinue = CheckTextBoxes

If OkToContinue = False Then
Exit Sub
End If

MsgBox "do the real work here"

End Sub
Private Sub TextBox1_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(1, keyascii)
End Sub
Private Sub TextBox2_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(2, keyascii)
End Sub
Private Sub TextBox3_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(3, keyascii)
End Sub
Private Sub TextBox4_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(4, keyascii)
End Sub
Private Sub TextBox5_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(5, keyascii)
End Sub
Private Sub TextBox6_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(6, keyascii)
End Sub
Private Sub TextBox7_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(7, keyascii)
End Sub
Private Sub TextBox8_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(8, keyascii)
End Sub
Private Sub TextBox9_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(9, keyascii)
End Sub
Private Sub TextBox10_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Call CheckKeys(10, keyascii)
End Sub

Sub CheckKeys(TBNumber, keyascii)
Dim myTB As MSForms.TextBox
Dim OkChar As Boolean

Set myTB = Me.Controls("textbox" & TBNumber)

OkChar = True
Select Case keyascii
Case Asc("0") To Asc("9")
'ok
Case Is = Asc(",")
If Len(myTB.Value) - _
Len(Application.Substitute(myTB.Value, ",", "")) 0 Then
'too many decimals
OkChar = False
End If
Case Else
OkChar = False
End Select

If OkChar = False Then
keyascii = 0
Beep
End If
End Sub
Function CheckTextBoxes() As Boolean
Dim iCtr As Long
Dim myTB As MSForms.TextBox
Dim AllOk As Boolean

AllOk = True
For iCtr = 1 To 10
Set myTB = Me.Controls("Textbox" & iCtr)
If Len(Trim(myTB)) = 0 Then
myTB.SetFocus
AllOk = False
Exit For
End If
Next iCtr

If AllOk = True Then
Me.Label1.Caption = ""
Else
Me.Label1.Caption = "Please fill in all the textboxes!"
End If

CheckTextBoxes = AllOk

End Function

Hi,

[quoted text clipped - 13 lines]
Thanks,
Pierre




--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200509/1