Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 1st, 2003, 05:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default Procedure visibility

Why is it that sometimes my procedures (Functions or Subroutines) are not visible to other code? Is there a difference between procedures in modules and procedures in forms?

Why can I be able to see a procedure in the code window and not be able to execute it in the immediate window?



Rand
__________________
Rand
 
Old December 1st, 2003, 07:14 PM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rand,

In most cases, the probable cause is in relation to the scope of the function or subroutine. Functions and subroutines that are declared as "Private" are available only to the module, form, or report in which they are defined or reside. Functions and subroutines that are declared as "Public" are available to ALL forms, reports, and modules within your application.

For example, a form may have a command button to close the form named "cmdCloseForm". The subroutine to execute that is associated with that button on that form will be named "Private Sub cmdCloseForm_Click()". Because this subroutine in this form was declared as "Private", the cmdCloseForm_Click() process will only run for the current form in which it is declared. Also, though, because cmdCloseForm_Click was declared as Private to that form, you can include a subroutine with the exact same name in every form, as long as each of them is declared as Private.

On the other hand, you may have a function that needs to be available for all forms to check for a valid part number that would be entered by a user named "ValidatePartNumber()". In this case, you would probably want to include this function in a module where it would be easy for you to find, and then declare the function as "Public" as in "Public Function ValidatePartNumber()" which would make it not only available to all forms, but any other part of the application as well.

Hope that helps.

Warren
:D
 
Old December 3rd, 2003, 03:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Unfortunately, "Public" vs. "Private" does not seem to be the problem.

The error occurs even when the procedure is "Public".

As I said, I cannot even run the procedures from the Immediate window.

Rand
 
Old December 3rd, 2003, 08:50 PM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Are you trying to run a procedure which is stored in a module from code within a form or vice-versa? I should have done this the first time, but I just tried to replicate an example. What I did was create a Public function declared as "Public Function FormFunction()" within an existing form and then created a function within a module that tried to tried to call this public function - when I tried to compile it, I got an error message of "Sub or Function not defined" in the code where I tried to call that function. However, if a function or sub that needs to be called from multiple places in an application is declared as Public within a module (as opposed to a form or report), there should be no problem with the call - I use this method successfully with shared functions in all of the applications that I have written. Apparantly, a function or subroutine that is declared as public within a form or report will only be visible to that form or report.

I haven't myself tried to run a subroutine myself via the immediate window, but I'd usually create a temporary macro using the RunCode option and then would indicate the name of the function under the "Function Name" option along with any necessary arguments.

If you have an example you'd like me to look at, I would be more than happy to try to help out.
 
Old December 4th, 2003, 09:41 AM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Show your code.



Sal
 
Old December 4th, 2003, 10:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
Default

wscheiman

A sub or function declared as publice within a form or report becomes a method of that form or report.

So to run the public function Foo in form frmMyForm - assuming frmMyForm is already open:
Code:
Forms.frmMyForm.Foo()

Brian Skelton
Braxis Computer Services Ltd.
 
Old December 10th, 2003, 05:03 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I usually use Form_Form1.FrmMethod





Similar Threads
Thread Thread Starter Forum Replies Last Post
Codebehind for conditional visibility Nostromo77 ASP.NET 2.0 Basics 4 September 21st, 2008 08:00 PM
problem in calendar visibility premnaath ASP.NET 2.0 Professional 1 November 16th, 2007 09:25 AM
visibility dynamic in report ermy78 Access 2 December 18th, 2006 06:19 PM
Chart Visibility baaul Reporting Services 1 September 28th, 2006 03:53 PM
Visibility Issue braheem ASP.NET 1.0 and 1.1 Basics 1 July 14th, 2005 05:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.