467,151 Members | 933 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,151 developers. It's quick & easy.

Validation (VBA newbie!)

Hi, im doing a module at uni in which I have to make userforms and use VBA, ive never done VBA until a few days ago, getting on ok with it, but I was wondering how to validate the information in a text box.
Simply I have a spinner button that sets a wage amount, obviously as I cant set a negative amount I need validation so the amount in the text box cant go lower than 0.
This is probably really simple, but I would appreciate any help given.
I am using visual basic 6.3 on XP.

Thanks in advance

Nick
Nov 14 '06 #1
  • viewed: 1177
Share:
3 Replies
LacrosseB0ss
100+
There are 2 things you can do.
1) Check if it's <0
Expand|Select|Wrap|Line Numbers
  1. valid = CInt(txtBox.text)
  2. if valid <> 0 then
  3.  
2) (more advanced) do not allow the "-" character to be entered. In the txtBox_KeyPress() sub routine, one of the values passed in is the key that was pressed. Check if it is the ASCII value of "-" (I don't have it memorized but I'm sure my buddy 'Google' can help you) and if ascii <> # you're good. If it is, display " " in the textbox instead (ascii 1 or 2 I think).

Hope this helps
- LB

PS do a Google search for 'Extended ASCII Chart'. You'll get millions of hits
Nov 14 '06 #2
1GB
There are 2 things you can do.
1) Check if it's <0
Expand|Select|Wrap|Line Numbers
  1. valid = CInt(txtBox.text)
  2. if valid <> 0 then
  3.  
2) (more advanced) do not allow the "-" character to be entered. In the txtBox_KeyPress() sub routine, one of the values passed in is the key that was pressed. Check if it is the ASCII value of "-" (I don't have it memorized but I'm sure my buddy 'Google' can help you) and if ascii <> # you're good. If it is, display " " in the textbox instead (ascii 1 or 2 I think).

Hope this helps
- LB


PS do a Google search for 'Extended ASCII Chart'. You'll get millions of hits
Unfortunately the first one does not work for negative numbers
You can either fail at a negative or convert to a positive.

'check for negative or 0
valid = CInt(TextBox.Text)
If valid <= 0 Then Exit Sub 'or something else

OR

'convert to positive and check for 0
valid = Abs(CInt(TextBox.Text))
If valid = 0 Then Exit Sub 'or something else
Nov 14 '06 #3
Thanks for the help both of you.
Greatly appreciated
No doubt I'll be posting another problem up soon!
Nov 14 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Paul Rubin | last post: by
21 posts views Thread by peteski22@gmail.com | last post: by
4 posts views Thread by Melissa | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.