Error when closing a form (1 Viewer)

109bow

Registered User.
Local time
Today, 03:23
Joined
Oct 24, 2007
Messages
134
I have one form, operator_details, that has a close button, which when clicked on runs code;
Private Sub close_Click()
Forms!training_record_tableview1.Refresh
On Error GoTo HandleError
DoCmd.RunCommand acCmdSaveRecord
DoCmd.close ObjectType:=acForm, ObjectName:="operator_details", Save:=acSave
HandleExit:
Exit Sub
HandleError:
MsgBox Err.Description
Resume HandleExit
End Sub

My problem is that the form operator_details is accessed via 2 different forms. One being training_record_tableview1, which is being refreshed when operator_details closes, the other form it is accessed from is signin.
The problem is, if the form operators_details is accessed from signin and then closed I get a runtime error 2450, cannot reference training_record_tableview1.
The reason operator_details is opened from 2 different forms is that the information in the form is relevant to the two forms that can open it.
My question is, I hope simple, how do I stop this error occurring.

Many thanks for your time.
 

109bow

Registered User.
Local time
Today, 03:23
Joined
Oct 24, 2007
Messages
134
moke123, thanks very much for the code, works a treat.:)
 

109bow

Registered User.
Local time
Today, 03:23
Joined
Oct 24, 2007
Messages
134
just a follow up on my post.
I have applied a similar code to requery another form, so that the signin form is updated from changes made to operator_details form.
The peculiar thing is that the changes only happen if operator_details is closed, then reopened and then closed for a second time.
Any thoughts!
The code is;
Private Sub close_Click()
If CurrentProject.AllForms("signin").IsLoaded = True Then
Forms!signin.Refresh
End If
If CurrentProject.AllForms("training_record_tableview1").IsLoaded = True Then
Forms!training_record_tableview1.Refresh
End If
On Error GoTo HandleError
DoCmd.RunCommand acCmdSaveRecord
DoCmd.close ObjectType:=acForm, ObjectName:="operator_details", Save:=acSave
HandleExit:
Exit Sub
HandleError:
MsgBox Err.Description
Resume HandleExit
End Sub
 

MarkK

bit cruncher
Local time
Yesterday, 19:23
Joined
Mar 17, 2004
Messages
8,185
Refresh and Requery are not the same thing. You may need to run Requery rather than Refresh in order get the results you are looking for. Consider code like...
Code:
Private Sub close_Click()
    Me.Refresh                                              [COLOR="Green"]'save pending edits in the current form[/COLOR]
    If IsLoaded("SignIn") Then Forms("SignIn").Requery      [COLOR="Green"]'requery other forms[/COLOR]
    If IsLoaded("Rraining") Then Forms("Training").Requery
    
    DoCmd.Close acForm, Me.name                             [COLOR="Green"]'Close the current form[/COLOR]
End Sub

Function IsLoaded(FormName As String) As Boolean            [COLOR="Green"]'example of how a function can...[/COLOR]
    IsLoaded = CurrentProject.AllForms(FormName).IsLoaded   [COLOR="Green"]'...simplify your code.[/COLOR]
End Function
hth
Mark
 

109bow

Registered User.
Local time
Today, 03:23
Joined
Oct 24, 2007
Messages
134
Thanks MarkK, I have tried your suggestion and it seems to give the same result, ie I have to close the form operator_details twice for the change to take effect on the forms that are being refreshed/requeried.
As I'm an absolute beginner at VBA I'm at a loss as to how to resolve this.
The code I'm got so far is;

Private Sub close_Click()
If CurrentProject.AllForms("signin").IsLoaded = True Then
Forms!signin.Refresh
End If
If CurrentProject.AllForms("training_record_tableview1").IsLoaded = True Then
Forms!training_record_tableview1.Refresh
End If


DoCmd.close acForm, Me.Name
End Sub
Function IsLoaded(FormName As String) As Boolean
IsLoaded = CurrentProject.AllForms(FormName).IsLoaded
End Function
 

moke123

AWF VIP
Local time
Yesterday, 22:23
Joined
Jan 11, 2013
Messages
3,940
requery instead of refresh

Forms!signin.Requery

edit:
I would probably also force a save before the requery

Code:
Private Sub close_Click()
me.dirty = false
If CurrentProject.AllForms("signin").IsLoaded = True Then
Forms!signin.Requery
End If
If CurrentProject.AllForms("training_record_tableview 1").IsLoaded = True Then
Forms!training_record_tableview1.Requery
End If


DoCmd.close acForm, Me.Name
End Sub
 
Last edited:

109bow

Registered User.
Local time
Today, 03:23
Joined
Oct 24, 2007
Messages
134
Thanks for your help, I have tried all the suggestions but unfortunately the problem of not updating the required still exsists.
As a way round it, I now access operator_details whilst either of the 2 forms are closed. This then means the updates take place. Although not what I was after, it will do.
Thanks for taking the time to look into the problem.
 

Users who are viewing this thread

Top Bottom