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
Solved

Ms Access Referencing a form from a non-form Module

Posted on 2016-09-27
4
43 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
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…
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…

829 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