View Full Version : ADODB.Command object not created inside subroutine

12-22-2004, 01:11 PM

I am currently experiencing problems with trying to use a subroutine to create a generic database connection that i can call each time I require it.

My code is as follows:

sub getDBConnection(blnRecordset)

dbConnString = "Provider=SQLOLEDB;server=SERVER_ADDRESS;database=DATABASE;uid=USERID;password=PASSWORD;"
set objConn = Server.CreateObject("ADODB.Connection")
objConn.CommandTimeOut = 100
objConn.Open dbConnString

if blnRecordset then
set objRs = server.CreateObject("ADODB.Recordset")
set objCmd = Server.CreateObject("ADODB.Command")
objCmd.CommandTimeOut = 100
objCmd.ActiveConnection = objConn
response.write isobject(objCmd) & "<br>"
end if
end sub

call getDBConnection(false)
response.write isobject(objCmd)

Output is:


As shown by the output, the Command object is created in the sub but not available immediately after on the page itself. The code works flawlessly without the use of a sub - it is only when enclosed within the subroutine I am experiencing this problem.

If anyone has an idea why this is happening, I'd be very grateful for a response.

Many thanks for your time


12-22-2004, 01:34 PM
the scope of the objCmd is local within the sub. Change it to a function which returns the object. Then do something like:
objCmd = getDBConnection(false)
response.write isobject(objCmd)

12-22-2004, 03:01 PM
Perfect, thank you for your time and response.