L
Lucky Girl
i want to modularize database connectivity code so as to re use it later so
i've created a class module for database connectivity functionality.
my database class module code goes as follows
Option Explicit
Function dbconnect()
Dim conn As ADODB.Connection
'creating the database connection
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=D:\My Documents\PMB
files\CorporateFinanceSystem\main.mdb;"
dbconnect = conn
End Function
Function addUser(myconn As ADODB.Connection)
'Set rs = New ADODB.Recordset
'rs.Open "Employee_Details", myconn, adOpenKeyset, adLockOptimistic
'With rs
' .AddNew
' .Fields("Name") = "Mayukha"
' .Fields("Address") = "Pannipitiya"
' .Update
'End With
End Function
the code i call it with goes as follows
Option Explicit
Sub btnAdd_Click()
Dim mydb As Database
Dim myconn As ADODB.Connection
Set mydb = New Database
Set myconn = New ADODB.Connection
myconn = mydb.dbconnect()
mydb.addUser (myconn)
End Sub
but i get an error saying compile error : data type mismatch in the '
mydb.addUser (myconn)' line. i'm a complete beginner to VBA and can't figure
out what i'm doing wrong. can someone please help?
i've created a class module for database connectivity functionality.
my database class module code goes as follows
Option Explicit
Function dbconnect()
Dim conn As ADODB.Connection
'creating the database connection
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=D:\My Documents\PMB
files\CorporateFinanceSystem\main.mdb;"
dbconnect = conn
End Function
Function addUser(myconn As ADODB.Connection)
'Set rs = New ADODB.Recordset
'rs.Open "Employee_Details", myconn, adOpenKeyset, adLockOptimistic
'With rs
' .AddNew
' .Fields("Name") = "Mayukha"
' .Fields("Address") = "Pannipitiya"
' .Update
'End With
End Function
the code i call it with goes as follows
Option Explicit
Sub btnAdd_Click()
Dim mydb As Database
Dim myconn As ADODB.Connection
Set mydb = New Database
Set myconn = New ADODB.Connection
myconn = mydb.dbconnect()
mydb.addUser (myconn)
End Sub
but i get an error saying compile error : data type mismatch in the '
mydb.addUser (myconn)' line. i'm a complete beginner to VBA and can't figure
out what i'm doing wrong. can someone please help?