Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts

    ADODB.Command object not created inside subroutine

    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:
    Code:
    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:
    Code:
    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
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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)
    I am the luckiest man in the world

  • #3
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Perfect, thank you for your time and response.
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •