Solved Error 3467 object is closed (2 Viewers)

moke123

AWF VIP
Local time
Today, 01:49
Joined
Jan 11, 2013
Messages
3,920
the 2450 error is because you are opening the form in dialog mode. This pauses the code in the calling form so the form variable is not set. In your edit form go to the properties and set the form to modal and remove the WindowMode:=acDialog in your docmd.openform arguments.. This will have the same effect as dialog without pausing the code in the calling form.


This code should be in the calling form, not the edit form.

What this is is a custom event for the edit form but it is contained in the calling form.

When the edit form is closed the Private Sub MyForm_Close() is run (from the calling form). You do not have to put any code in the edit form for this to work.

Set MyForm = Forms("frmAddEdit") sets a reference to the edit form.
This allows you to do a few things with the edit form from the calling form.
for instance if you wanted to set the form caption you can simply write MyForm.Caption = "My Edit form" after the set statement.
Or set a field value on the edit form with MyForm.SomeField = "Xyz"

Private Sub MyForm_Close() is a custom event that "listens" for the MyForm variable to close and then handles the event if and when it happens. This is all from the calling form.


Code:
Option Compare Database
Option Explicit

Dim WithEvents MyForm As Form   ' a form variable

Private Sub btnEditRecipe_Click()
Dim strWhere As String
strWhere = "RecipeID =" & Me.txtRecipeIDFK

    DoCmd.OpenForm "frmAddEdit", _
        WhereCondition:=strWhere

Set MyForm = Forms("frmAddEdit")    'set the form variable
MyForm.OnClose = "[Event Procedure]"    'make sure that the form variable has event procedure in the close event.
      
End Sub

Code:
Private Sub MyForm_Close()
    Me.txtRecipe = MyForm.txtRecipe
    Me.txtNotes = MyForm.txtPrepNotes

Set MyForm = Nothing
 
End Sub
 

moke123

AWF VIP
Local time
Today, 01:49
Joined
Jan 11, 2013
Messages
3,920
Here's another example to demonstrate that no code is needed in the called form.
Note there is no code in form 2, its all in the calling form Form1.

It takes a little bit to wrap your head around this but it is a great trick to have in your arsenal.
 

Attachments

  • frmevent.accdb
    512 KB · Views: 45

Gasman

Enthusiastic Amateur
Local time
Today, 06:49
Joined
Sep 21, 2011
Messages
14,299
Here's another example to demonstrate that no code is needed in the called form.
Note there is no code in form 2, its all in the calling form Form1.

It takes a little bit to wrap your head around this but it is a great trick to have in your arsenal.
Thank you Moke.
 

ClaraBarton

Registered User.
Local time
Yesterday, 22:49
Joined
Oct 14, 2019
Messages
463
Thank you very much. I had it set for dialog so it would come back and complete. This makes a lot of sense.
 

spaLOGICng

Member
Local time
Yesterday, 22:49
Joined
Jul 27, 2012
Messages
127
Code:
Private Sub btnClose_Click()
10    On Error GoTo btnClose_Click_Error
      Dim frm1 As Access.Form

20    DoCmd.Close _
            objectType:=acForm, _
            ObjectName:=Me.name
      
30     Select Case CallingForm
            Case Is = "frmCalendarAppt"
40            Set frm1 = Forms("frmCalendarAppt")
50            frm1!txtRecipe.Value = Me.txtRecipe.Value
60            frm1!txtNotes.Value = Me.txtPrepNotes.Value
70           Exit Sub
        
80        Case Is = ""
90           DoCmd.OpenForm "frmMenu"
100         Exit Sub

110       Case Else
              Dim frm As Form
120                   For Each frm In Application.Forms
130                       If frm.name = CallingForm Then
140                            frm.Visible = True
150                    Exit For
160                       End If
170                   Next frm
        
180   End Select

190   On Error GoTo 0
200   Exit Sub

btnClose_Click_Error:
210       MsgBox "Error " & Err.Number & " (" & Err.description & ") " & _
        " in procedure btnClose_Click, line " & Erl & "."

End Sub
I have a form that calls a second form to edit an entry. The calling form is frmCalendarAppt and is still open. Everytime I run this, however, I get Error 2467.
I've tried Forms!frmCalendarAppt. and every other way I can imagine. How do I carry the edited fields back to the calling form?
Are you setting the "CallingForm" as a Form Variable or as a String Variable (See Line 30). If a String Variable, this should be fine, but if a Form Variable, you will need to express "Select Case CallingForm.Name"

It makes good practice to preface your varaibles with their object tyoe. For example:

Form could be like "frmCallingForm"
String could be like "strCallingForm"

qry is used for Queries.
rpt is used for Reports.

int is used for Integers.
dbl is used for Double.
lng is used for Long.

If you adopt that method there should not be any question as to how the object or variable should be referred to.

Please let us know how the "CallingForm" is dimensioned, and we can go from there.
 

ClaraBarton

Registered User.
Local time
Yesterday, 22:49
Joined
Oct 14, 2019
Messages
463
The callingform is not the problem. It is all working fine. My question here was how to pass values back to the calling form and Moke123 took care of it very nicely.
 

Users who are viewing this thread

Top Bottom