Question Locked

TextBox Casting and Validation

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,

I have got 4 texboxes which should handle Double type numbers. At the access database in the back-end, I created them as Text data type. The program needs to multiply those 4 values to get a desired number. My 2 questions are:

1. I am trying to do the multiplication on the change handle of every text box, but I get an error which states that "Cast from string"" to type 'Double' is not valid". I made sure that the initial value of the text box is not empty but I am not successful.

2. What is the best way to force the user to enter the numbers in a fraction format i.e. 0.XXXX. Because if a whole number is entered it would mess up the whole calculation.

Appreciate your help on this because I am new to VB.NET

Awaiting replies

3 replies

by ahmedali_2020

Before Multiplication, you have to convert from string to double...
to do that use the function CDbl([Object as Expression]) for example:
Dim string1 as String
Dim double1 as Double
string1 = TextBox.Text
double1 = CDbl(string1)
'you do that with the rest of 3 textboxes..
and then do multiplication
dim total as double
total = double1 * double2 * double...

Hope that will answer your question....
Ahmed

by ahassan81

Thanks Ahmed for your reply.
In fact, I had tried that before, but it didn't work. I still got the error which says that converting from string "" to double is not valid.

For some reason, the text box initial value will be empty no matter how I try to make it 0. Be it through the text property or in the database.

Anyways, I found out how to do it. I did it using the VAL() function and it worked fine.

Thanks again.
Abdullah

This discussion is closed to new replies.