Hi Everyone
Please anyone tell me how can I use the following code in vb6 Module.
Dim db As Database
Dim rs As Recordset

Private Sub Form_Load()
Set db = OpenDatabase("C:\MyDatabase.mdb")
End Sub

Thanks in advance

Dani AI

Generated

Short diagnosis and root cause (concise):
Run-time error 91 means an object variable was Nothing when you tried to use it. In this thread the usual cause is that the shared database object was never properly initialized before a form tried to open a recordset. In ’s snippets the initializer was declared with the wrong return/signature, so the module-level object stayed unset. and were on the right track about scope — the database object must live in a standard module and be initialized exactly once (or lazily) so every form can use it.

Fix checklist (follow in order):

  • Confirm the DAO library is referenced in Project->References.
  • Put the database variable in one standard module (module scope), and provide a single initialization routine that actually assigns the DAO database object before any form uses it. Avoid returning wrong types from that routine.
  • If both ADO and DAO are referenced, explicitly qualify types with DAO to avoid using the wrong library.
  • Always assign object returns with the VB Set semantics and always check that the db variable is not Nothing before opening recordsets.
  • Verify the .mdb path is correct and accessible by the running user.
  • Close recordsets and release objects when finished.

Debugging tips and workflow:
Set a breakpoint just before any recordset open and inspect the module-level DB variable. If it is Nothing, trace back to why the initializer did not run. A simple guard (check and initialize on demand) prevents forms from assuming the DB is ready. This addresses ’s point about including the initialization and ’s idea of reducing per-form boilerplate.

Reference:
For behavior of the DAO open call see Microsoft’s OpenDatabase documentation: .

Recommended Answers

All 7 Replies

You have to copy the code of OpenDatabase as well.

Hi
I tried four five times but it's not working, its show "Run Time erorr No 91". please write to me in detals.

thanks in advance.

can you please specify what exactly is the error you are getting, for my reference.

Hi Everyone
Please anyone tell me how can I use the following code in vb6 Module.
Dim db As Database
Dim rs As Recordset

Private Sub Form_Load()
Set db = OpenDatabase("C:\MyDatabase.mdb")
End Sub

Thanks in advance

The above program code is correct provided, there exists MyDatabase.mbd in C:\

If it exists try the following:

Private Sub Form_Load()
Dim db As Database
Dim rs As Recordset
Set db = OpenDatabase("C:\MyDatabase.mdb")
End Sub

If the above works then it is a case of Scope rule. Get back to me.

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.

hi,

"Module"

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

regards
Shailu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.