Solved

Ms Access Referencing a form from a non-form Module

Posted on 2016-09-27
4
53 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 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 58

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

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

628 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