...

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



martin_narg
12-22-2004, 12:11 PM
All,

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")
else
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:


true
false


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

Martin

Roelf
12-22-2004, 12: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)

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum