Solved

Ms Access Referencing a form from a non-form Module

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now