Solved

Calling code from various procedures

Posted on 1998-11-13
2
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 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