# Miscellaneous > SQL Scripts >  Funny results from SQL command used in VBScript

## dumoulip

Hi Everyone,

I am currently writing a VBScript that will help us update our asset tracking database as we deploy workstations. Here is the statement I am having trouble with:

objConnection.Execute "SELECT Department FROM tblEmployee WHERE USERID='"&Username&"'",Department

My connection to the database works and I have used this command in SQL Server's query analyzer and it works but when I run it from the VBScript, I invetiably get a Department Value of -1. Can anybody point me in the right direction? thanks!

----------


## DBottaro

Try doing this instead:

dim objCn, objRs, strDepartment
set objCn = CreateObject("ADODB.Connection")
set objRs = CreateObject("ADODB.Recordset")

'Open your connection...
objCn.Open "DSN=myDSN;"

objRs.Open "SELECT Department FROM tblEmployee WHERE USERID='" & UserNAme & "'", objCN

if NOT objRs.EOF
  strDepartment = objRs("Department")
else
  strDepartment = "*** NOT FOUND ***"
end if

objRs.Close
objCn.Close

set objRs = Nothing
set objCn = Nothing

----------

