Doing some math

1. sug
Junior Member
Join Date
Dec 1969
Posts
25

## Doing some math

Making a VERY simple poll and I&#039;m stuck at the basic part. If question 1 has a value of 1, I want the next vote for that question to be 2, and so forth. I tried strsql=rs("question")+1 but that just gives me 11 instead of 2. Any help would be appreciated.

2. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## Use cint()

Example:<BR>strsql = cint(rs("question")) + 1

3. Senior Member
Join Date
Dec 1969
Posts
3,195

## Think about what you are doing

You are adding 1 to a string("1"). Guess what you are going to get for a question with value of 2. ; )

4. Senior Member
Join Date
Dec 1969
Posts
1,843

## Strings and Numbers

In VBS you can use the + sign when working with strings to concatenate (just like the & sign) Thats what&#039;s happening here. <BR><BR>Your number is being treated as a string, rather then a number. So, we have to convert it to a double-precision number:<BR><BR>StrSQL = CDBL(rs("Question"))+1<BR><BR>This will work.

5. Senior Member
Join Date
Dec 1969
Posts
1,843

## CINT is OK for *THIS* but...

is usually best to avoid given it&#039;s size limitations. Most likely his poll won&#039;t have 40,000 votes but if it does, CInt will break. <BR><BR>Using CDBL is better.

6. Senior Member
Join Date
Dec 1969
Posts
3,195

## Since you are making this

point.<BR><BR>In this case, CLNG() is actually better. ; )

7. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## No, there can be 1/2 votes.

Don&#039;t you remember the Florida election snafu?

8. Junior Member
Join Date
Dec 1969
Posts
10

## RE: Doing some math

I&#039;ve run into this problem in nearly every scripting language. The problem is that the intepreter has trouble knowing if you are working with strings or integers. I have found solutions that should (with slight changes in syntax) avoid confusion.<BR><BR>When the intepreter thinks you are working with a string when you want it to be an integer do something like<BR><BR>MyInteger = iInt + (sInt * 1)<BR><BR>What this does is forces the interpreter to change the value of sInt to a number regardless of the actual type.<BR><BR>When the intepreter thinks you are working with a integer when you want it to be an string do something like<BR><BR>MyString = sStr & mid(iStr, 1)<BR><BR>What this does is forces the interpreter to change the value of iStr to a string regardless of the actual type.<BR><BR><BR>Hope this is helpful to you.<BR>

9. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Why bother with all..

.. of that?<BR><BR>MyInteger = iInt + CDbl(sInt)<BR>Or CInt, CLng, etc., etc., etc.<BR><BR>MyString = sStr & iStr<BR>& ALWAYS concatenates.<BR><BR>MyString = sStr + CStr(iStr)<BR><BR>Learn about the casting functions and you won&#039;t leave the next programmer wondering what you were smoking.

10. Senior Member
Join Date
Dec 1969
Posts
3,195

## Agreed

Don&#039;t complicate things unnecessarily. ; )<BR><BR>

#### Posting Permissions

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