VBScript: how to set values from recordset to string

recordsetstringvbscript

This is probably a beginner question, but how do you set a recordset to a string variable?

Here is my code:

Function getOffice (strname, uname) 

strEmail = uname
WScript.Echo "email: " & strEmail 
Dim objRoot : Set objRoot = GetObject("LDAP://RootDSE")
Dim objDomain : Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))
Dim cn : Set cn = CreateObject("ADODB.Connection")
Dim cmd : Set cmd = CreateObject("ADODB.Command")
cn.Provider = "ADsDSOObject"
cn.Open "Active Directory Provider"
Set cmd.ActiveConnection = cn

cmd.CommandText = "SELECT physicalDeliveryOfficeName FROM '" & objDomain.ADsPath & "' WHERE mail='" & strEmail & "'"
cmd.Properties("Page Size") = 1
cmd.Properties("Timeout") = 300
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Dim objRS : Set objRS = cmd.Execute

  WScript.Echo objRS.Fields(0)

Set cmd = Nothing
Set cn = Nothing
Set objDomain = Nothing
Set objRoot = Nothing

Dim arStore 

Set getOffice = objRS.Fields(0)

Set objRS = Nothing

End function 

When I try to run the function, it throws an error "vbscript runtime error: Type mismatch"
I presume this means it can't set the string variable with a recordset value.

How do I fix this problem?


I just tried

if IsNull(objRS.Fields(0).Value) = TRUE then
getOFfice = "noAD"
else
getOFfice = objRS.Fields(0).VAlue
end if

And that throws a different error ADODB.Field: Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

Best Answer

Try This:


getOffice = objRS.getString

This will return the entire recordset as a tab delimited string.