|
-
Jan 19th, 2006, 01:10 PM
#1
Thread Starter
Addicted Member
Creating a Connection String
I was wondering if I could create a connection string with a function, so that I can call the function once, instead of creating connections within each function. Can anyone let me know if this is possible?
VB Code:
Private Function connectToDB(database as String) as ADODB.Connection
Dim connection as ADODB.connection
Set connection = New ADODB.connection
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & database
connection.Open
connectToDB = connection
End Function
Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.
-
Jan 19th, 2006, 01:23 PM
#2
Re: Creating a Connection String
If you'd try it, you might find that it works.
-tg
-
Jan 19th, 2006, 01:31 PM
#3
Thread Starter
Addicted Member
Re: Creating a Connection String
Well I keep getting a user defined datatype error. Is there something I need to add in the references or how do I go about defining a datatype?
-
Jan 19th, 2006, 01:33 PM
#4
Re: Creating a Connection String
On what line to do you get the error (as tg intimated, on the surface, it looks just fine)?
-
Jan 19th, 2006, 01:35 PM
#5
Thread Starter
Addicted Member
Re: Creating a Connection String
It's complaining that ADODB.Connection is not a return type, and is a user defined type
-
Jan 19th, 2006, 09:01 PM
#6
Re: Creating a Connection String
Why don't you just use 1 public connection object?
-
Jan 19th, 2006, 11:14 PM
#7
Re: Creating a Connection String
Did you set a reference to ADO?
-tg
-
Jan 24th, 2006, 02:39 PM
#8
Thread Starter
Addicted Member
Re: Creating a Connection String
OK so I'm still working on creating a function that inputs a database name and outputs a connection to that database. Currently I am getting a "Run-time error '91': Object variable or With block variable not set" whenever I try to return the connection (i.e. connect = connection). Any help would be greatly appreciated. Thanks
VB Code:
Private Sub Form_Load()
Dim conn As ADODB.connection
conn = connect("Test")
Dim sql As String
sql = "SELECT * FROM [TEST]"
conn.Execute sql
Debug.Print sql
End Sub
Private Function connect(database As String) As ADODB.connection
Dim connection As ADODB.connection
Set connection = New ADODB.connection
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= C:\Documents and Settings\P6B0438\My Documents\Test\" & _
database & ".mdb"
connection.Open
connect = connection
End Function
Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.
-
Jan 25th, 2006, 07:45 AM
#9
Thread Starter
Addicted Member
Re: Creating a Connection String
-
Jan 25th, 2006, 07:52 AM
#10
Re: Creating a Connection String
As it is an object variable you need to use Set, eg:
You should also clear the memory used by the local variable, eg:
-
Jan 25th, 2006, 08:00 AM
#11
Re: Creating a Connection String
There are several things that you need to change.
First, your connection object declaration, as it is, is confined to your Function and is not available outside of your function (I'm guessing that is your object not set.) Move your these declares to the Form's declaration section so they they will be available throughout your form.
VB Code:
Option Explicit
Private conn As ADODB.Connection
Private adoRS As ADODB.Recordset
Private sSQL As String
Second, you Execute action queries such as INSERT, UPDATE, DELETE etc. SELECT statements create recordsets. They are not executed. The code for this would be
VB Code:
Set adoRS = NEW ADODB.Recordset
sSQL = "SELECT * FROM [TEST]"
adoRS.Open sSQL, conn
-
Jan 25th, 2006, 08:52 AM
#12
Thread Starter
Addicted Member
Re: Creating a Connection String
Thanks did not know that about when to use the execute command
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|