Hi Friends
Actually I m working on a database project. I am using in this project D.A.O 3.6 Object Library. I added lots of forms absolutely for many purposes. Now the problem I faced is that for every form I have to write the following code.
Option Explicit
Dim db As Database
Dim rs As Recordset
Private Sub Form_Load()
Set db = OpenDatabase("C:\MyDatabase.mdb")
End Sub
I want to use one Module and set the above codes for all the forms.
I tried the following codes in Module but is showing "Error No 91"
Option Explicit
Dim db As Database
Public Function MyDataBase() As String
Set db = OpenDatabase("C:\MyDatabase.mdb")
End Function
Private Sub Command1_Click()
call MyDataBase
Set Tb = db.OpenRecordset("Select * From Table1")
End Sub
Please tell me any method that you think will solve my problem.
Regard,
Naveed
Hi Friends
Actually I m working on a database project. I am using in this project D.A.O 3.6 Object Library. I added lots of forms absolutely for many purposes. Now the problem I faced is that for every form I have to write the following code.
Option Explicit
Dim db As Database
Dim rs As Recordset
Private Sub Form_Load()
Set db = OpenDatabase("C:\MyDatabase.mdb")
End Sub
I want to use one Module and set the above codes for all the forms.
I tried the following codes in Module but is showing "Error No 91"
Option Explicit
Dim db As Database
Public Function MyDataBase() As String
Set db = OpenDatabase("C:\MyDatabase.mdb")
End Function
Private Sub Command1_Click()
call MyDataBase
Set Tb = db.OpenRecordset("Select * From Table1")
End Sub
Please tell me any method that you think will solve my problem.
Regard,
Naveed
Couldn't you just create a Standard Module and write the code there as a function and then call the function on all of your forms? Still be some coding involved, but you'd only ned 1 line instead of 5 on each form. Maybe I'm wrong.
Public db As dao.Database 'Declare variable for database connection
Public Sub connectdatabase()
Set db = DBEngine.OpenDatabase("C:\MyDatabase.mdb") 'Set the connection path
End Sub
"Form"
Dim rb As dao.Recordset 'Declare variable for recordset
Private Sub Command1_Click()
Call connectdatabase ' call the module
Set rb = db.OpenRecordset("table1") specidfy the table to open recordset
rb.AddNew
rb(0) = "1"
rb(1) = "Success"
rb.Update
End Sub
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.