Re: VBScript INT Type Mismatch
- From: "Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 10 May 2006 15:15:23 -0500
bxr222 wrote:
I am trying to pull a value out of a table and assign it to a variable and
am getting type mismatch errors. Here is how I am trying to retrieve the
value:
intLocation = Cint("Select Location_ID from Locations where
Location_Code='" & strGroupLocation & "'")
The script errors out on that exact line with:
Microsoft VBScript runtime error: Type mismatch: 'Cint'
If I run the query in Query Analyzer it returns a numeric value. So, I am
assuming the problem lies with VBScript. Thanks in advance for
suggestions.
The only way I know to run T-SQL queries in VBScript is with ADO (unless you
use the Run method of the wshShell object to run osql). You can assign the
query statement, as a string, to a Recordset object, for example. Use the
Open method to run it, then enumerate the results. You must assign a
connection string so that ADO knows which database on which server to use.
For example:
=================
Option Explicit
Dim strConnect, adoRecordset, strQuery, strGroupLocation, intLocation
' Specify strGroupLocation.
strGroupLocation = "Test"
' Specify connection string to database.
' The syntax depends on the DBMS. This example is SQL Server.
' This assumes Windows integrated authentication.
' For the default instance, omit "\MyInstance".
strConnect = "DRIVER=SQL Server;" _
& "Trusted_Connection=Yes;" _
& "DATABASE=MyDatabaseName;" _
& "SERVER=MyServer\MyInstance"
' Specify the query.
strQuery = "SELECT Location_ID " _
& "FROM Locations " _
& "WHERE Location_Code = '" & strGroupLocation & "'"
' Use ADO to run the query.
Set adoRecordset = CreateObject("ADODB.Recordset")
adoRecordset.ActiveConnection = strConnect
adoRecordset.Source = strQuery
adoRecordset.Open
' Retrieve the result. This assumes the resulting recordset
' has just one row. It will raise an error if there are no rows.
' It will only retrieve the first if there are more than one.
intLocation = CInt(adoRecordset.Fields("Location_ID").Value
adoRecordset.Close
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
.
- Prev by Date: Re: VBScript INT Type Mismatch
- Next by Date: Re: Powershell performance
- Previous by thread: Re: VBScript INT Type Mismatch
- Next by thread: Re: VBScript INT Type Mismatch
- Index(es):
Relevant Pages
|