Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function to open/close DB

Status
Not open for further replies.

bobmmp

Programmer
Apr 22, 2002
43
US
Below I made two functions (VBScript) to open and close a database connection. Problem, I get a syntax error using these functions... I am trying to use the functions in an include file since it is used in multiple scripts...

When I am calling it, I am just using the function name

DBOpen... this gives me a syntax error pointing to Function.

when I actually place it in my code, I get the following error.

Microsoft VBScript runtime error '800a01a8'

Object required: ''

First time I ever tried using functions to open and shut a db.

Function DBOpen()
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
Conn.ConnectionString = "Data Source=d:\home\pathtodb\mydb.mdb"
Conn.Open

Set Rs = Server.CreateObject("ADODB.Recordset")
End Function

Function DBClose()
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Function

 
i haven't figured out how to do this yet either...however, i simulate it by putting the open db call in an include, and including that file in every page.
=========================================================
if (!succeed) try();
-jeff
 
If I get this figured out, I will make sure you a pass it along to you.
 
My method is same as jemminger.

I put all the functions(global and local) into a file named
sysFuncions.asp and then include it in my each .asp file.

I guess that you could use a class to solve this problem. I have read an article about class in vbscript, you can search it using
Anticipate your way.
 
I have essentially the same code in a Sub routine instead of a function. This sub is in an include file, so I only call the sub when required. If you're not using parameters or expecting a return why not use a sub?

The sub can contain the error handling too.

HTH
 
I have used the following code pretty successfully in the past for opening and closing an Access database connection on a web server.


<%
dbname = &quot;databasename.mdb&quot;
set conn = server.createobject(&quot;adodb.connection&quot;)
cnpath = &quot;DBQ=&quot; & server.mappath(dbname)
conn.Open &quot;DRIVER={Microsoft Access Driver (*.mdb)}; &quot; &cnpath

'Add SQL statements

set rsInv = server.createobject(&quot;adodb.recordset&quot;)
rsInv.open SQL, conn, 3, 3

if rsInv.eof then
rsInv.close
conn.close
response.redirect(&quot;search.asp?status=fail&quot;)
end if
%>

Hope this helps!
 
Thanks all, I might just return to using sub dbopen() and put the sub in an include file. I am using the same asp script to proccess about five db functions, therefore the include file would be used a lot.

I will take a look at using a class for vbscript.

Thanks All.

btw, the function call works fine as long as I keep both the close and open calls in one function.
 
bobmmp,

that makes sense, as the connection would fall out of scope as soon as the function ends.

perhaps try making the connection an application or session variable? never tried it - just an idea. could possibly get out of hand memory-wise if many session vars are created...
=========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top