By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,593 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,593 IT Pros & Developers. It's quick & easy.

Validation (VBA newbie!)

P: 5
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
Share this Question
Share on Google+
3 Replies


LacrosseB0ss
100+
P: 113
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

100+
P: 1,646
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

P: 5
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.