Validating Data

Validating Data

Anonymous
Not applicable
318 Views
5 Replies
Message 1 of 6

Validating Data

Anonymous
Not applicable
Matt,
I agree with not allowing invalid data to be entered in the first place.
Where exactly should I paste the code starting with "public..."? I copied
it to the keypress event of the text box but the public line becomes red and
when I try to run the app, it tells me I have a compile error.

Thanks...
Scott

PS- I pasted the function into the 'general' module

Matt Slay wrote in message
news:[email protected]...
> I just wanted to make sure you saw my reply to your earlier post.
>
> If you implement this KeyPress approach, the user's input will be limited
> to numeric characters ONLY, and there is NO WAY they can enter bad data to
> start with, then you do not have to worry about a message or about setting
> focus.
>
>
> Here is a function that I add to my programs, then make a call to it from
> the keypress event of the textbox:
>
> Public TextBox1.Keypress (KeyAscii as Integer)
> KeyAscii=Num_Check(KeyAscii)
> EndSub
>
>
>
> Function Num_Check(KeyAscii As Integer)
>
> '-- Only allows digits 0-9 and one decimal
> '-- (no negatives and no alpha characters)
>
> If KeyAscii <> vbKeyBack Then
>
> If KeyAscii = vbKeyReturn Then
> SendKeys "{TAB}"
> KeyAscii = 0
> ElseIf Chr(KeyAscii) = "." And InStr(ActiveControl, ".") = 0 Then
> KeyAscii = KeyAscii
> ElseIf Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
> KeyAscii = 0
> End If
>
> End If
>
> Num_Check = KeyAscii
>
> End Function
>
>
>
>
>
0 Likes
319 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Hi, Scott. I don't remember the name Matt used for his validation routine
but you need to call it from the KeyPress event of your TextBox control. If
you'll repost the code, I can tell you more.

Scott wrote in message
news:[email protected]...
> Matt,
> I agree with not allowing invalid data to be entered in the first place.
> Where exactly should I paste the code starting with "public..."? I copied
> it to the keypress event of the text box but the public line becomes red
and
> when I try to run the app, it tells me I have a compile error.
>
> Thanks...
> Scott
0 Likes
Message 3 of 6

Anonymous
Not applicable
Here's the code as originally posted by Matt Slay... Thanks, Frank...

> Here is a function that I add to my programs, then make a call to it from
> the keypress event of the textbox:
>
> Public TextBox1.Keypress (KeyAscii as Integer)
> KeyAscii=Num_Check(KeyAscii)
> EndSub
>
>
>
> Function Num_Check(KeyAscii As Integer)
>
> '-- Only allows digits 0-9 and one decimal
> '-- (no negatives and no alpha characters)
>
> If KeyAscii <> vbKeyBack Then
>
> If KeyAscii = vbKeyReturn Then
> SendKeys "{TAB}"
> KeyAscii = 0
> ElseIf Chr(KeyAscii) = "." And InStr(ActiveControl, ".") = 0 Then
> KeyAscii = KeyAscii
> ElseIf Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
> KeyAscii = 0
> End If
>
> End If
>
> Num_Check = KeyAscii
>
> End Function
>
>
>
>
>

Frank Oquendo wrote in message
news:[email protected]...
> Hi, Scott. I don't remember the name Matt used for his validation routine
> but you need to call it from the KeyPress event of your TextBox control.
If
> you'll repost the code, I can tell you more.
0 Likes
Message 4 of 6

Anonymous
Not applicable
Everything from FUnction down can go in your standard module. The foloowing
lin goes in your TextBox control's KeyPress event:

KeyAscii=Num_Check(KeyAscii)
0 Likes
Message 5 of 6

Anonymous
Not applicable
It turns out Matt's code breaks in VBA. The Num_Check function expects an
integer and VBA feeds it an MSForms.ReturnInteger, resulting in a type
mismatch error. There's also the matter of the call to ActiveControl. It
needs to be prefaced by the name of the UserForm. I've taken the liberty of
modifying Matt's code to work in VBA. The calling convention is a little
different than before. From the KeyPress event of a TextBox on a UserForm:

KeyAscii = Num_Check(Me, KeyAscii)

Here's the VBA version of Matt's Num_Check function:

Function Num_Check(frm As MSForms.UserForm, KeyAscii As
MSForms.ReturnInteger) As Integer

'-- Only allows digits 0-9 and one decimal
'-- (no negatives and no alpha characters)

If KeyAscii <> vbKeyBack Then

If KeyAscii = vbKeyReturn Then
SendKeys "{TAB}"
KeyAscii = 0
ElseIf Chr(KeyAscii) = "." And InStr(frm.ActiveControl, ".") = 0
Then
KeyAscii = KeyAscii
ElseIf Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
KeyAscii = 0
End If

End If

Num_Check = KeyAscii

End Function
0 Likes
Message 6 of 6

Anonymous
Not applicable
Thanks for everyone's help. It's grrreatly appreciated...

Frank Oquendo wrote in message
news:[email protected]...
> It turns out Matt's code breaks in VBA. The Num_Check function expects an
> integer and VBA feeds it an MSForms.ReturnInteger, resulting in a type
> mismatch error. There's also the matter of the call to ActiveControl. It
> needs to be prefaced by the name of the UserForm. I've taken the liberty
of
> modifying Matt's code to work in VBA. The calling convention is a little
> different than before. From the KeyPress event of a TextBox on a UserForm:
>
> KeyAscii = Num_Check(Me, KeyAscii)
>
> Here's the VBA version of Matt's Num_Check function:
>
> Function Num_Check(frm As MSForms.UserForm, KeyAscii As
> MSForms.ReturnInteger) As Integer
>
> '-- Only allows digits 0-9 and one decimal
> '-- (no negatives and no alpha characters)
>
> If KeyAscii <> vbKeyBack Then
>
> If KeyAscii = vbKeyReturn Then
> SendKeys "{TAB}"
> KeyAscii = 0
> ElseIf Chr(KeyAscii) = "." And InStr(frm.ActiveControl, ".") = 0
> Then
> KeyAscii = KeyAscii
> ElseIf Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
> KeyAscii = 0
> End If
>
> End If
>
> Num_Check = KeyAscii
>
> End Function
>
>
0 Likes