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.
5 Answers
Actually you are much better off using the Vartype function.
Private Function IsDouble(ByVal value As Variant) As Boolean
IsDouble = (VarType(value) = vbDouble)
End Function
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
-
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).– ThureinApr 8, 2009 at 16:39
-
Be careful what CDbl will accept as input though. I think it accepts trailing nonsense characters. Apr 8, 2009 at 16:48
-
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. Apr 8, 2009 at 16:51
-
-
You can put parenthesis around it or call the CBool function if you really want to. But the above works. Apr 8, 2009 at 17:25
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."
There is no inbuilt IsDouble function available in VB6 instead you can use inbuilt IsNumeric Function. Otherwise you can use following User Defined Function to achieve the same:
Function IsDouble(ByVal value As Variant) As Boolean
Dim convertedValue As Double
On Error Goto EH
convertedValue = CDbl(value)
IsDouble = True
Exit Function
EH:
IsDouble = False
End Function
There's an IsNumeric()
function that should work well enough. If that is inadequate for your needs can you also explain in what ways?
-
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).– ThureinApr 8, 2009 at 16:37
-
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. Apr 8, 2009 at 16:54
-
That's not correct, it will only accept values that fit into a Double. (IEEE standard 8 byte floating point number)– MarkJApr 8, 2009 at 17:23
-
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?– ThureinApr 8, 2009 at 18:00
-
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"– MarkJApr 8, 2009 at 19:50