Solved

Calling code from various procedures

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

809 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