passing ADO objects to functions

E

everymn

This is kind of a noob question but I'm having problems passing
objects to functions. Is this not allowed? I'm getting a type
mismatch error when I call Connect()

Thank You

Option Compare Database
Option Explicit
Dim connection1 As ADODB.Connection

Private Sub Command0_Click()
Set connection1 = New ADODB.Connection
Connect (connection1)
connection1.Close
End Sub

Public Function Connect(ByRef conn As ADODB.Connection)
Dim strConnect$
strConnect$ = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;" &
_
"DATABASE=odbc_insert_test;USER=root;PASSWORD=mypass;OPTION=3;"
conn.Open strConnect$
End Function
 
R

RoyVidar

This is kind of a noob question but I'm having problems passing
objects to functions. Is this not allowed? I'm getting a type
mismatch error when I call Connect()

Thank You

Option Compare Database
Option Explicit
Dim connection1 As ADODB.Connection

Private Sub Command0_Click()
Set connection1 = New ADODB.Connection
Connect (connection1)
connection1.Close
End Sub

Public Function Connect(ByRef conn As ADODB.Connection)
Dim strConnect$
strConnect$ = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;"
& _
"DATABASE=odbc_insert_test;USER=root;PASSWORD=mypass;OPTION=3;"
conn.Open strConnect$
End Function

By enclosing the arguement in parenthesis, you are trying to pass
it by value, which I think might be what is creating this error.
Try either

Call Connect (connection1) ' with Call statement
or

Connect connection1 ' without parenthesis

and see if that helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top