Results 1 to 2 of 2

Thread: Is it possible to establish a connection to a databse within a sub function

  1. #1
    Guest

    Cool

    Hi there...... I'm starting to wonder if it is actually possible to establish a connection with my database from within a sub function. I've tried several ways but I don't seem able to get it to work. Here's what I've tried:

    SUB GetRecords()
    set DBObj = server.createobject("ADODB.connection")
    DBObj.open "TaskManager", adOpenKeyset
    sql = "SELECT M, J, FROM mytable WHERE M = '" &Choosen& "'"
    RS = DBObj.execute(sql)
    DBObj.close
    END SUB


    SUB Detect_Option()
    If Detected.options(Detected.SelectedIndex).value="October" THEN
    Choosen = "October"
    GetRecords()
    else
    Choosen = "November"
    GetRecords()
    End If
    END SUB



    The same connection is done initially at the top of my page with a specific Field name instead of the variable "&Choosen&" and there's absolutely no problem there. But if I try to reaccess the databse from within my page with a sub function I've notice that it just doesn't execute the connectivity statements. Please send your recomendation. Thanks.

  2. #2
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    It depends.

    If your sub function is in client side script, no.

    If your sub is in server side script, sure.

    Remember scope. If you declare it in the sub, you won't be able to use it outside of the sub. If you intend on using it on the entire page, you'll need to either create a function that returns a connection object or declare the connection object on the page outside of any subs/functions and then make the actual connection inside of your sub.

    BTW- If you are closing the connection to the database, you are closing the recordset you created from it as well.

    [Edited by monte96 on 10-03-2000 at 04:46 PM]
    oOOo--oOOo
    __/\/\onte96
    oOOo--oOOo
    Senior Programmer/Analyst
    MCP
    [email protected]
    [email protected]


    Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..

Posting Permissions

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



Click Here to Expand Forum to Full Width