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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type Mismatch

Status
Not open for further replies.

parsman

Programmer
Jul 15, 2003
17
0
0
US
I'm sure this is a dumb question for you experienced VBers, but why do I get a runtime "Type Mismatch" error on this statement":

xPlan.CntyCd = IIf(IsNumeric(cmbCnty.Text), CInt(cmbCnty.Text), -1)

xPlan.CntyCd is defined as Integer
The ComboBox gives the user the option of selecting "ANY" or a county code number. Selecting the county code number works fine -- the "ANY" selection is what fails.

I've also tried the the following with the same results:

xPlan.CntyCd = IIf(cmbCnty.Text = "ANY", -1, CInt(cmbCnty.Text)
 
What value does CInt(cmbCnty.Text) return. If it is too large for an integer to handle (32,7??) that may be your problem.

Try declaring xPlan.CntyCd as a long.

Thanks and Good Luck!

zemp
 
If cmbCnty.Text is larger that "32,7??" then the CInt() function will also get that error. Try esamsalah's suggestion or CLng() as well.

Thanks and Good Luck!

zemp
 
Thanks, but the CInt is working fine and changing to CLng has no effect. When the County code number is selected (a number from 1 to 500 probably), the CInt or CLng converts the text to an integer. When the user selects "ANY" from the ComboBox, the statement is simply supposed to return the integer value of -1. That's where the error occurs.
 
The problem is that CInt(0 cannot evaluate "ANY". Try using the Val() function, as suggested earlier.

If that does not work you can always split it up into,

Code:
if... then
    ...
  else
   ...
  end if

>"CInt or CLng converts the text to an integer..." is not correct. CLng converts to a long. The result will be the same as with CInt is the expression is small enough for an integer, but it will be of type long.

Thanks and Good Luck!

zemp
 
[clip]
xPlan.CntyCd = IIf(IsNumeric(cmbCnty.Text), CInt(cmbCnty.Text), -1)
[/clip]

I don't agree with the previous posts of Zemp, but the last one seems to be the solution. Simple logic to solve the problem. Don't try to do anything extravagant. Trying to pass a string to a function that handles numbers will always cause problems.

Logic:

If the value of the choice is "ANY" then
the value of the choice is -1
run the assignment
Else
the value of the choice is the value of the choice
run the assignment
End if
 
There must be something odd about how the IIF function works. I still don't understand it, but the Val() function seems to solve the problem. Breaking out the condition into a standard IF...THEN...ELSE also works.

Thanks everyone!
 
>There must be something odd about how the IIF function works

Nothing odd.
The same thing will happen with:

If IsNumeric("") And CInt("") > 0 Then

End If

The whole line will be evaluated, even if the first part fails.

Seeings how the text is non-numeric, you cannot use CInt() on it, because CInt() requires a numeric or numeric equivilent value, which "" or " " is not one, in order to convert.

The Val() function doesn't require this.
But becareful. With decimal numbers, the Val() function only will work as expected if the decimal seperator is a Dot and the thousands seperator is a comma, otherwise only the integer portion of the number will be returned.
 
Thanks, CCLINT. The key to your answer is that despite the fact that it's a runtime error, both the true and false parts are evaluated. I didn't think it would work that way at runtime.
 
>I didn't think it would work that way at runtime.

'fraid so; VB doesn't do short-circuiting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top