Home All Groups Group Topic Archive Search About

Convert Variant String to Double

Author
6 Mar 2006 3:31 PM
remo0033
Hello,

What am I missing here? (Code pasted below)

1) Set objInstaller = CreateObject("WindowsInstaller.Installer")
2) Version = objInstaller.Version
3) Wscript.Echo typename(Version)
4) WScript.Echo IsNumeric(Version)
5) Version = Cdbl(Version)
6) Wscript.Echo typename(Version)

I'm writing a script to check if Windows Installer 3.1(or above) is
installed on our systems. The variable 'Version' holds the data I need
but it comes as a string. To verify that its a string, I added lines 3
& 4. Now I'm trying to change it to the double subtype on line 5 but I
get the error message "Microsoft VBScript runtime error: Type mismatch:
'Cdbl'". Can someone please assist? I need to change it to double so I
could perform an operation like:

If Version > 3.1 Then (Do something)

Author
6 Mar 2006 3:42 PM
Ken Halter
<remo0***@gmail.com> wrote in message
news:1141659071.842162.74870@u72g2000cwu.googlegroups.com...
> Hello,
>
> What am I missing here? (Code pasted below)

Since there's only 1 decimal point allowed, you're probably getting the
error because the version string contains 3 decimals...

'===========
Private Sub Command1_Click()
   Dim o As Object
   Set o = CreateObject("WindowsInstaller.Installer")
   Debug.Print o.version 'shows 3.1.4000.2435

   'convert to number
   Dim v As Variant
   Dim d As Double
   v = o.version
   v = Split(v, ".")
   d = Val(v(0) & "." & v(1))
   Debug.Print d 'shows 3.1

> If d > 3.1 Then (Do something)

End Sub
'===========

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Author
6 Mar 2006 3:57 PM
remo0033
WOW! Works like a charm. Thanks for your assistance.
Author
6 Mar 2006 5:20 PM
Rick Rothstein [MVP - Visual Basic]
>    'convert to number
>    Dim v As Variant
>    Dim d As Double
>    v = o.version
>    v = Split(v, ".")
>    d = Val(v(0) & "." & v(1))

Instead of the last two lines above, wouldn't this have worked as well?

     d = Val(v)

or, replacing all three lines and reducing it to a one-liner<g>, this

     d = Val(o.Version)

Rick



Show quoteHide quote
>    Debug.Print d 'shows 3.1
>
> > If d > 3.1 Then (Do something)
>
> End Sub
Author
6 Mar 2006 3:57 PM
Norm Cook
On my box, I get a string that looks like
3.1.4000.2435 which is why CDbl() fails.
I think you will have to do some string manipulation to
check the version, but I don't know enough about
scripting to tell you how to approach it.

<remo0***@gmail.com> wrote in message
Show quoteHide quote
news:1141659071.842162.74870@u72g2000cwu.googlegroups.com...
> Hello,
>
> What am I missing here? (Code pasted below)
>
> 1) Set objInstaller = CreateObject("WindowsInstaller.Installer")
> 2) Version = objInstaller.Version
> 3) Wscript.Echo typename(Version)
> 4) WScript.Echo IsNumeric(Version)
> 5) Version = Cdbl(Version)
> 6) Wscript.Echo typename(Version)
>
> I'm writing a script to check if Windows Installer 3.1(or above) is
> installed on our systems. The variable 'Version' holds the data I need
> but it comes as a string. To verify that its a string, I added lines 3
> & 4. Now I'm trying to change it to the double subtype on line 5 but I
> get the error message "Microsoft VBScript runtime error: Type mismatch:
> 'Cdbl'". Can someone please assist? I need to change it to double so I
> could perform an operation like:
>
> If Version > 3.1 Then (Do something)
>
Author
6 Mar 2006 5:58 PM
Larry Serflaten
<remo0***@gmail.com> wrote

> I'm writing a script to check if Windows Installer 3.1(or above) is
> installed on our systems. The variable 'Version' holds the data I need
> but it comes as a string. To verify that its a string, I added lines 3
> & 4. Now I'm trying to change it to the double subtype on line 5 but I
> get the error message "Microsoft VBScript runtime error: Type mismatch:
> 'Cdbl'". Can someone please assist? I need to change it to double so I
> could perform an operation like:
>
> If Version > 3.1 Then (Do something)

Why not just compare it as a string?

  If Version >= "3.1" Then (Whatever)

???
LFS