tags:

views:

554

answers:

4
+1  Q: 

IsDouble() in VB6?

Is there any IsDouble() function in VB6? If I'm not mistaken, Double data type in VB6 is equivalent to float in SQL Server 2000. Your advice would be much appreciated.

A: 

There's an IsNumeric() function that should work well enough. If that is inadequate for your needs can you also explain in what ways?

Joel Coehoorn
Let me reveal the background, I need to check a string value which comes from a text file in order to insert into a field (SQL float datatype) of a database table (SQL Server 2000).
Kthurein
Okay. The only problem with IsNumeric then is that it would allow an overflow: a number that is too big to fit in a float type.
Joel Coehoorn
That's not correct, it will only accept values that fit into a Double. (IEEE standard 8 byte floating point number)
MarkJ
MarkJ ... Do you mean that whichever value that satisfies IsNumeric() will not always overflow SQL 2K float data type? Am I right to say that?
Kthurein
I think so - I'm not that familiar with SQL server. "VB6 Double variables are stored as IEEE 64-bit (8-byte) floating-point numbers ranging in value from -1.79769313486232E308 to -4.94065645841247E-324 for negative values and from 4.94065645841247E-324 to 1.79769313486232E308 for positive values"
MarkJ
+1  A: 
Function IsDouble(ByVal varValue As Variant) As Boolean

   Dim dblTest As Double

   On Error Resume Next

   dblTest = CDbl(varValue)

   IsDouble = Err.Number = 0

End Function
Will Rickards
Thanks for your codes. Let me reveal the background, I need to check a string value which comes from a text file in order to insert into a field (SQL float datatype) of a database table (SQL Server 2000).
Kthurein
Be careful what CDbl will accept as input though. I think it accepts trailing nonsense characters.
Will Rickards
Nevermind, I was thinking of the Val function. But if you have international users you might want to test this against those too. I think CDBl uses the regional settings to do the conversion.
Will Rickards
I don't think VB handles boolean expressions like that.
Joel Coehoorn
You can put parenthesis around it or call the CBool function if you really want to. But the above works.
Will Rickards
Hmm... I know I have old code where I wanted to build a bool that way and wasn't able to. So I dug back (waaay back!) and found an example, and it turns out the reason was 'company policy' at my then work place. Old habits die hard, I guess.
Joel Coehoorn
+1  A: 

IsNumeric() is fine, it will only accept values that fit into Doubles. It is regionally aware, i.e. for French regional settings the decimal separator is a comma.

I just tried this in the Immediate window.

Debug.Print IsNumeric("4e308") 
False

The definition of a VB6 Double in the manual is "VB6 Double variables are stored as IEEE 64-bit (8-byte) floating-point numbers ranging in value from -1.79769313486232E308 to -4.94065645841247E-324 for negative values and from 4.94065645841247E-324 to 1.79769313486232E308 for positive values".

I think that's the same as a SQL Server float, going by the online docs. "Floating precision number data with the following valid values: -1.79E + 308 through -2.23E - 308, 0 and 2.23E + 308 through 1.79E + 308."

MarkJ
+3  A: 

Actually your much better off using the Vartype function.

Private Function IsDouble(ByVal value As Variant) As Boolean
    IsDouble = (VarType(value) = vbDouble)
End Function

Kind Regards Noel

Bigtoe