Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ms Access Referencing a form from a non-form Module

Posted on 2016-09-27
4
Medium Priority
?
60 Views
Last Modified: 2016-11-08
I am writing a procedure in a non-form module (ModHasGlobals).

The procedure has references to a form. I call the code from a  button click event :
Private Sub cmdCloseAcMeasurements_Click()
On Error GoTo Err_cmdCloseAcMeasurements_Click
    If Me.Dirty Then Me.Dirty = False
    Call ModHasGlobals.RefreshTurtleEvents
    DoCmd.Close

The code of ModHasGlobals.RefreshTurtleEvents is attached.

When I run the click event, I get an error message: Cannot find form Main.  Is there something different that I need to do to reference a form from a code module that is not form-based?
TurtleMainForm.png
RefreshEvents.txt
0
Comment
Question by:dblankman
[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
4 Comments
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 total points
ID: 41818119
Code that executes in a standard module can't use the keyword Me

 But it can reference a form as long as that form is open.  But rather than "hard coding" it to a specific form, what you do is pass a reference for the form:

Public Sub RefreshTurtleEvents(frm as form)

then:

Call ModHasGlobals.RefreshTurtleEvents([Form])

or

Call ModHasGlobals.RefreshTurtleEvents(Me)

Now within that procedure, you can use the variable frm to carry out methods, refer to properties, etc:

  frm.Refresh

  frm.mytextcontrolname.visible = False


and so on.

Jim.
0
 

Author Comment

by:dblankman
ID: 41819737
Thanks. It took me a bit to figure out what you were saying. but I got it to work.
0
 
LVL 58

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 total points
ID: 41819777
Good to hear!

Note that you can pass any object; a form, report, control, recordset, etc in this manor besides the usual data types you might pass (string, long, variant, etc).

Below is a procedure that takes a recordset as a parameter, and here's a call:

                     Dim rstCheck As ADODB.Recordset

120               OpenADORecordset rstCheck, "Select * from qry_CboSelectAvItemID"


Jim.

Public Sub OpenADORecordset(rstName As ADODB.Recordset, strSQL As String, Optional intCursorType As Integer = adOpenKeyset, Optional intLockType As Integer = adLockOptimistic)

          ' Opens a recordset on the current JET connection
          ' This is the old call and uses JET as the OLEDB provider.

          Const RoutineName = "OpenADORecordset"

10        On Error GoTo OpenADORecordset_Err

20        Set rstName = New ADODB.Recordset
30        rstName.ActiveConnection = CurrentProject.Connection
40        rstName.Open strSQL, rstName.ActiveConnection, intCursorType, intLockType

OpenADORecordset_Exit:
50        Exit Sub

OpenADORecordset_Err:
60        UnexpectedError ModuleName, RoutineName, Version, Err.Number, Err.Description, _
                          Err.Source, VBA.Erl
70        Resume OpenADORecordset_Exit

End Sub
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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