Re: VBScript INT Type Mismatch



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


.



Relevant Pages

  • Re: On ADSI and LDAP
    ... the problem is how can I retrieve the value for myuser using the ... would be more efficient to use ADO to query AD for the attributes values. ... For more on using ADO, ... Dim adoCommand, adoConnection, strBase, strFilter, strAttributes ...
    (microsoft.public.scripting.vbscript)
  • Re: Internet class differences...
    ... The database and web interface to the server is produced by a different ... Initially a WebClient class was used. ... This worked fine for submitting the query. ... retrieve the results but, instead, is sent to a login page from which it ...
    (microsoft.public.dotnet.general)
  • Re: Column Order Problem
    ... I understand the reasons for not using the BLOB data types but as I ... >> I'm working on required extending an existing query to retrieve some ... The query is written correctly and returns the ...
    (microsoft.public.data.ado)
  • Re: Returning only a subset of groups in AD
    ... Output.WriteLine "There are no members in this group." ... I assume that ADSICommand is an ADO command object, ... value assigned to the CommandText property, which is the ADO query. ... ' Comma delimited list of attribute values to retrieve. ...
    (microsoft.public.scripting.vbscript)
  • Re: Record source at runtime
    ... Clause, but is in the data requested, so it will return the PKs ... retrieve indexes for Lastname and Firstname. ... had I written the query: ...
    (microsoft.public.access.formscoding)