Solved

Calling code from various procedures

Posted on 1998-11-13
2
143 Views
Last Modified: 2010-04-30
I have a basic question.  I know that if I have a public function I can call that from another sub.  Here is my problem. in about 30 different places on a form, I need to open a database and recordset(always the same).

so if I have a function that says:

Sub dbs()
Dim db As DAO.Database
Set db = OpenDatabase("S:\risk\performancesource\dashdatadelete.mdb")
Dim rs As DAO.Recordset
end sub


When I Use this sub:

private sub command1_click

dbs
set rs=db.openrecordset("name")

end sub

I get "object required"
how do I write code in a sub to recognize from another sub?

I am offering 100 points.  Please give me a good explanation and some sample code.

Thanks
0
Comment
Question by:windows1
2 Comments
 
LVL 2

Accepted Solution

by:
Poddy earned 100 total points
ID: 1444783
the scope of the database is contained within the procedure.

it is only visible within sub DBS

1. put option explicit at the start of your form module
2. you need to define the database so it is visible to the command button - so define it as
private mdb as database

and set that within sub DBS


0
 

Expert Comment

by:Ultras
ID: 1444784
Sub dbs()
 Dim db As DAO.Database
 Set db = OpenDatabase  ("S:\risk\performancesource\dashdatadelete.mdb")
 Dim rs As DAO.Recordset
end sub

private sub command1_click

 dbs
 set rs=db.openrecordset("name")
end sub

You may be declare db and rs on header module or form.

Private db as DAO.Database
private rs As DAO.Recordset
or
Public db as DAO.Database
Public rs As DAO.Recordset


Sub dbs()
 Set db = OpenDatabase  ("S:\risk\performancesource\dashdatadelete.mdb")
end sub

private sub command1_click
 dbs
 set rs=db.openrecordset("name")
end sub

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now