# Thread: should be simple.. how to make a variable equa a lnumeric value typed into a textbox?

1. ## should be simple.. how to make a variable equa a lnumeric value typed into a textbox?

Not used VB6 for a while, and now I just can't remember how to do this...
I have a textbox on form1, I want to take a user inputted value (3 digits usually), and make a 'double' variable take it's value, using 'Val'. But I've fallen at the first hurdle... I've forgotten how to make a variable take a value inputted to a text box by a user.
What is the syntax for this?

The textbox appears as 'Textbox2' on the form while I'm writing code.

The sub is called 'Private Sub pictureHeight_Change()'

If IsNumeric(TextBox2.Text) Then picinput\$ = TextBox2.Text
PicHeight_Px = Val(picinput\$)

was my best effort

Thanks.

2. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

Do you need to save the value in picinput\$ for another purpose?
You can just do a Val on the TextBox text, although once you've verified it is numeric, then using CDbl is technically more efficient.
Code:
If IsNumeric(TextBox2.Text) Then
PicHeight_Px = Val(TextBox2.Text)
End If

3. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

Not only more efficient, CDbl() is locale-aware unlike the crusty old Val() function.

And there isn't much point to your temporary picinput\$ the way you are using it.

4. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

It seems I had to substitute pictureHeight.Text for TextBox2.Text...

If IsNumeric(pictureHeight.Text) Then
PicHeight_Px = Val(pictureHeight.Text)
End If

Only trouble is, as soon as I type just 1 digit into the textbox, it immediately starts running this sub.

How do I make sure it only does this when the user has finished typing in the new value into the textbox?

Thanks

5. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

Well, how do you know WHEN the user is 'done'? You said that 'usually' it is 3 digits, which means to me it could be 1, 2, 4 or more as well. One way to do that is when the user 'finishes', he can hit the enter key (you can figure that one out, I am sure). Or have a cmd button next to the textbox and the user would click it when 'done' entering whatever number of digits he wants.

6. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

Just a personal preference.

When I develop forms that require user input via textboxes, I generally create a separate procedure that validates all the textbox values are valid and filled in, as needed. In other words, unless absolutely necessary, I don't validate textbox input as the input occurs or when the textbox loses focus. This validation routine would be called when the user selects an action that uses the input. If validation fails, then the user is informed via a msgbox that required information is missing or invalid.

7. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

I had considered using an 'ok' or 'enter' button, not sure whether to or not yet. Validation will be something I'll add when I've got my head around the basic idea of adding the values. The textbox 'losing focus' seems like a good way of signalling the end of user input, and there's another button that has to be pressed immediately afterwards anyway, in most cases.

8. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

What do you mean by:

when I've got my head around the basic idea of adding the values.
Still need help with that...and what are you adding to what?

9. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

I meant user behaviour when adding values to this particular text box.

The fact that the bit of code I initially had trouble with gets called whenever any digit is changed in the text box turns out not to matter, so I left it as per Passel's example, and added a dummy 'ok' button next to it, which just makes the user feel more certain that the program has taken notice of them! A message box that pops up whenever a non numerical value is entered takes care of the odd typo.

10. ## Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

Note that if numerical input is the main reason for this thread, you may want to search the forum for similar solutions. There are several ways to ensure user can only enter numerical data in a textbox including during input, post-input, using masked edit control, and using APIs.

Key search terms: textbox numbers only

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured