Solved

Calling code from various procedures

Posted on 1998-11-13
2
144 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

20 Experts available now in Live!

Get 1:1 Help Now