Solved

Ms Access Referencing a form from a non-form Module

Posted on 2016-09-27
4
49 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 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 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 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculation in a Report 13 43
Database (Access Table) Security Access 8 60
Batch convert csv to xlsx 10 60
Create two calculated fields in a table based on one field entered 4 40
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

751 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