Data type checking 
Author Message
 Data type checking

I have a some code.

"If UserForm1.txtAmt3 = "" Then

Else
    curAmt3 = UserForm1.txtAmt3
    curTotal = curTotal + curAmt3
End If"

Basically I want this code to do nothing if there is no entry into the
txtAmt3 box, if there is a value there I want that value to be assigned to
curAmt3 and then add that value to the curTotal accumulator.

The code works fine, but I would like to check to make sure that the user
entered a numeric value rather than some aimless text.  Any thoughts on how
to do that?



Sun, 01 Jan 2006 06:30:14 GMT  
 Data type checking
Hi, DWTSG,

You're forcing VBA to make an implicit (unwritten, behind-the-scenes)
translation from a text value to a number. If you make that translation
explicit, you also solve the problem of garbage in the text box and save
yourself some code at the same time. Get rid of the If...Then...Else...End
If structure. The only code you need is:

    curAmt3 = Val(txtAmt3.Value)
    curTotal = curTotal + curAmt3

Since the Val function stops evaluating the text at the first character that
isn't a digit or a period, it will return 0 if the box is empty or if the
first character is garbage. (Spaces are thrown away.)

--
Regards,
Jay Freedman
Microsoft Word MVP     FAQ: http://www.mvps.org/word

Quote:

> I have a some code.

> "If UserForm1.txtAmt3 = "" Then

> Else
>     curAmt3 = UserForm1.txtAmt3
>     curTotal = curTotal + curAmt3
> End If"

> Basically I want this code to do nothing if there is no entry into the
> txtAmt3 box, if there is a value there I want that value to be
> assigned to curAmt3 and then add that value to the curTotal
> accumulator.

> The code works fine, but I would like to check to make sure that the
> user entered a numeric value rather than some aimless text.  Any
> thoughts on how to do that?



Sun, 01 Jan 2006 10:16:29 GMT  
 Data type checking
Hi DWTSG,

Use If Isnumeric(txtAmt3) Then etc.

If this code is in the userform, there is no need to use UserFrom1.txtAmt3
and you could just use

curTotal = curTotal + txtAmt3

so your code can be simplified to

If Isnumeric(txtAmt3) Then curTotal = curTotal + txtAmt3

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP

Quote:
> I have a some code.

> "If UserForm1.txtAmt3 = "" Then

> Else
>     curAmt3 = UserForm1.txtAmt3
>     curTotal = curTotal + curAmt3
> End If"

> Basically I want this code to do nothing if there is no entry into the
> txtAmt3 box, if there is a value there I want that value to be assigned to
> curAmt3 and then add that value to the curTotal accumulator.

> The code works fine, but I would like to check to make sure that the user
> entered a numeric value rather than some aimless text.  Any thoughts on
how
> to do that?



Sun, 01 Jan 2006 10:24:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. check the data type

2. Checking data types

3. How to check for correct data type input into text box or other controls

4. Get error: Disallowed implicit conversion from data type varchar to data type money

5. Data type conversion error when saving numeric data types

6. Q: variant data type with user defined data type

7. C data type vs. VB Data type

8. ADO data types and vbasic data types mapping

9. Data Type Problem accessing Btrieve (MBF) data type

10. Data Type Problem accessing Btrieve (MBF) data type

11. Data Type Problem accessing Btrieve (MBF) data type

12. VB Data Types - C Data Types

 

 
Powered by phpBB® Forum Software