Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type mismatch on conversion functions

Status
Not open for further replies.

EdB

Programmer
Jan 9, 2001
7
US
Hi,

This seems to be a simple piece of code but I keep getting a type mismatch error on CInt. Have tried CSng, CDbl as well but get a similar mismatch error.

Confirmed, using TypeName, that intTemp is Integer and strTemp is string.

Scenario is that I have a table of up to five entries on an input form that the user can fill out. The total of these entries must equal 30. The field names on the user form are RegPct0 thru RegPct4.

Can anyone see what I'm missing?
Thanks in advance.

Code:

dim intCounter
dim intTotPct
dim strTemp
dim intTemp

intCounter = 0
intTotPct = 0
intTemp = 0
For intCounter = 0 To 4
strPctField = "RegPct" & intCounter
If Request.Form.Item(strPctField) <> &quot;&quot; Then
strTemp = Request.Form.Item(strPctField)
intTemp = cint(strTemp)
End If
intTotPct = intTotPct + intTemp
Next
 
For intCounter = 0 To 4
strPctField = &quot;RegPct&quot; & intCounter
If Request.Form(strPctField) <> &quot;&quot; Then
if isnumeric(Request.Form(strPctField)) then
intTemp = cint(Request.Form(strPctField))
end if
End If
intTotPct = intTotPct + intTemp
next

if intTotPct <> 30 then
' do whatcha gotta do
end if
I got rid of the item bit, i think you are meant to use index position rather than name whne using item, you dont need to do that as you already know the name so just use request.form

I also added an isnumeric check for safety since someone could add &quot;abcd&quot; as one of the values
 
Thank you. Working perfectly now.
 
Save yourself some &quot;pain&quot; caused by bad input. Never trust user input. Suppose the user enters 32768, which exceeds tha max for an integer...BOOM
On Error Resume Next
intTemp = cint(strTemp)
lngErr = Err.Number
On error goto 0
if LngErr <> 0 then
...bad input
else
...
End if


Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
good point john, i missed that. Its usually something I go round telling people myself, just goes to show how easy it is miss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top