-
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.
-
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]