473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Global Get Form Name sub

78
I have the following code for a form:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CommentsButton()
  2. On Error GoTo Err_CommentsButton_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.     gPreviousForm = Me.Form.Name
  7.     stDocName = "Comments Form"
  8.     DoCmd.Close
  9.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  10.  
  11. Exit_CommentsButton_Click:
  12.     Exit Sub
  13.  
  14. Err_CommentsButton_Click:
  15.     MsgBox Err.Description
  16.     Resume Exit_CommentsButton_Click
  17.  
  18. End Sub
I have this code in numerous forms (code works great). The issue is, I would like to make a public sub in a standard module, so I can just call a sub rather than have it in the code of each form. But when I paste it into a standard module, and change Private to Public, I get an error because you can't use "Me" in a standard module. How can I edit the code to reference the current form (Me) without using that term?
Apr 3 '08 #1
7 3755
Scott Price
1,384 Expert 1GB
Try
Expand|Select|Wrap|Line Numbers
  1. Forms!Form.Name
.

Regards,
Scott
Apr 3 '08 #2
kpfunf
78
Scott,
With that change, I get an error message. The code is looking for a form named "Form".
Apr 3 '08 #3
Scott Price
1,384 Expert 1GB
I'm sorry, I should have explained that one a little better.

Expand|Select|Wrap|Line Numbers
  1. Forms![Form Name].[Control Name]
OR
Expand|Select|Wrap|Line Numbers
  1. Forms![Form Name].[Property]
is the correct syntax when referring to a form from outside of it's form module.

I suspect this isn't going to be helpful to you in this particular instance, since if I understand you correctly, you are attempting to write the form name to a global variable to be used later in your code processing.

A function that would do this is:

Expand|Select|Wrap|Line Numbers
  1. Public Function GetPreviousForm(formName As Form) As String
  2.  
  3. GetPreviousForm = formName.Name
  4.  
  5. End Function
  6.  
This code would go into a standard code module, the global variable pForm would be declared as String at the top of the module. (i.e. Global pForm As String)

The code to call this function from any form's code module:

Expand|Select|Wrap|Line Numbers
  1. pForm = GetPreviousForm(Me)
  2. Debug.Print pForm
  3.  
Regards,
Scott
Apr 3 '08 #4
kpfunf
78
Scott,
I think we're almost done. I have the following code in the Global module:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Global pForm As String
  4.  
  5. Public Function GetPreviousForm(formName As Form) As String
  6.  
  7. GetPreviousForm = formName.Name
  8.  
  9. End Function
  10. Public Sub CommentOpen()
  11. On Error GoTo Err_CommentsButton_Click
  12.  
  13.     Dim stDocName As String
  14.     Dim stLinkCriteria As String
  15.     pForm = GetPreviousForm(Me)
  16.     stDocName = "Comments Form"
  17.     DoCmd.Close
  18.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  19.  
  20. Exit_CommentsButton_Click:
  21.     Exit Sub
  22.  
  23. Err_CommentsButton_Click:
  24.     MsgBox Err.Description
  25.     Resume Exit_CommentsButton_Click
  26.  
  27. End Sub
For each form, in it's code I have
Expand|Select|Wrap|Line Numbers
  1. Call CommentOpen
When run, this error: "Compile Error: Invalid use of Me keyword" on line 15 above.
Apr 4 '08 #5
NeoPa
32,556 Expert Mod 16PB
If you consider that Me only has any meaning in the form's module, you need to take out any references to Me in the global procedures, and change your calling code (from the form module) to :
Expand|Select|Wrap|Line Numbers
  1. Call CommentOpen(Me)
The declaration for CommentOpen() should now be :
Expand|Select|Wrap|Line Numbers
  1. Public Sub CommentOpen(frmForm As Form)
...and line 15 should now be :
Expand|Select|Wrap|Line Numbers
  1. pForm = GetPreviousForm(frmForm)
Apr 4 '08 #6
kpfunf
78
Thanks Scott and NeoPa.


NeoPa, do you have any links to articles explaining Subs and arglists? I've learned programming "on-the-fly" and don't have a complete understanding of the building blocks. Thanks!
Apr 4 '08 #7
NeoPa
32,556 Expert Mod 16PB
If you go to the VBA window (Alt-F11 from Access) and select Help / Microsoft Visual Basic Help, you will find a Contents tab.

From here navigate to Visual basic Language reference / Keywords / Public and then select the link for Function Statement or Sub Statement.

You should find all the details you need in here.
Apr 4 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On...
9
by: Tian | last post by:
I want to create a object directory called Context in my program, which is based on a dict to save and retrieve values/objects by string-type name. I have the definition like this: utils.py...
38
by: MLH | last post by:
I have 2 global constants declared in the same global module: Global Const MY_VERSION$ = "1.04" Global Const ProjectName$ = "MyProj" I have 2 global procedures designed to return the value of...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
3
by: CsProviders | last post by:
i am a newbie to .net programming, i am having problem with global data and form instanciation. My questions are How do i maintain global data in windows forms applications? How do i navigate from...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
8
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project...
5
by: tech.rawsteak | last post by:
I have a function that retrieves a user's login name from their workstation and looks it up on an employee table to return their full name (ie: jsmith -John Smith). Their full name is then...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.