Link to home
Start Free TrialLog in
Avatar of TimKestermont
TimKestermont

asked on

onclose event

I want to bring up a yes no confirmation dialog on close event.  Then on choice to Save I don't want to close the form but run a save command button on a subform.  The subform has a save button  with code.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of shanesuebsahakarn
shanesuebsahakarn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TimKestermont
TimKestermont

ASKER

I understand the idea, I'm abit unsure of the SubformControl.Form portion.   You've answered several of my questions prior to this but I am not for sure what I am actually putting there.  

Thanks.
Basically, replace MyMainForm and MySubformControl with the names of your main form and the subform control that actually holds your subform. MyButton is the name of the button on your subform whose code you want to run. Let me know if you need any clarification.
I apologize but are you referring to a field when you say control?

No - let me explain a bit further. Subforms are held inside "subform controls". This acts as a container for the form that is acting as a subform. If you design your main form, then click once on the subform, click View->Properties, you'll see the subform control's name in the Name property on the Other page. This is often (but not always) the same as the name of the form it contains, if you created it using the wizard. Otherwise, it might have a name like "Child11". The form that the subform control "holds" is determined by the Source Object property.
Forms!frmtelephone!subfrmtele.Form.command13_OnClick     What is wrong with this code?
That looks right - what error did it give?

Did you change the Private to Public?
inproper reference to the property form/report.   I am using Access 2k and my library references  (not that it matters but I've come across this issue several times in my db and had to do some crazy stuff)
VB for apps
ms access 9.0 object library
ms access DAO 3.6 object library
ole automation
ms forms 2.0 object library
Sorry, try this, my error:

Forms!frmtelephone!subfrmtele.Form.command13_Click

(i.e. remove the On)
Same error...
Private Sub Form_Close()

Select Case MsgBox("Do you want to save?", vbYesNo, "Save Changes")
Case vbYes
  Forms!frmtelephone!subfrmtele.Form.Command13_Click
Can you post the command button's code from the subform (including the Sub and End Sub lines)?
Public Sub Command13_Click()

Dim TRN_Eff As String
 
DoCmd.SetWarnings False

If IsNull(Me.TELE_Date) = True Then
TRN_Eff = False
Else: TRN_Eff = True
End If

If TRN_Eff = False Then
Select Case MsgBox("Do you want to save?", vbYesNo, "Save Changes")
Case vbYes
MsgBox "Please Enter A Date", vbExclamation
Me.TELE_Date.SetFocus

Case vbNo
       If Dirty = True Then
       DoCmd.RunCommand acCmdUndo
             
   End If

  End Select
End If

If TRN_Eff = True Then
Select Case MsgBox("Do you want to save?", vbYesNo, "Save Changes")
Case vbYes
 
  DoCmd.SetWarnings False
 
  DoCmd.RunCommand acCmdSaveRecord
  DoCmd.OpenQuery "qrycra1"
  CurrentDb.Execute "Delete * From tblcra1"
   
  Me.Requery  
 
Case vbNo
 
    If Dirty = True Then
       DoCmd.RunCommand acCmdUndo      
 
End If
 
End Select
End If

End Sub
That all looks correct - the "Invalid reference to property form/report" seems to suggest that the name of the subform control is incorrect. Can you double check it? If not, there is another (uglier) way to do it, but I'm not certain why this doesn't work.
both name and source object is subfrmtele.  
Ok, try this:

Me!subfrmtele.SetFocus
Me!subfrmtele.Form!Command13.SetFocus
SendKeys "{ENTER}"
This is an issue throughout the DB and I was thinking the reference library maybe an issue.
Ah - when you say it's an issue throughout the db, where does it tend to give you that error?

This *sometimes* indicates database corruption although that wouldn't be the first conclusion. Have you compacted the database recently?
Yes I have compacted the db.  I shouldn't say that it's throughout the db.  It's everytime I refer to a subform.
Could you try the snippet that I posted above,with SendKeys?
I have tried it with another form and the same issue.
That's very strange - are you using any of the Microsoft Forms 2.0 controls? Could you try unchecking that library and see if it works?
It won't let me uncheck.  It says it is in use
Are you able to reference controls on the subform? For example, if you run this line of code somewhere on your main form:
MsgBox Me!subfrmtele.Form!SomeTextbox

Substitute MyTextBox with the name of a control on the subform - does it correctly give you a message box showing the value in the text box?
Hey I think I have found out what the issue was.  My relationships were not there.  I have no idea why that happened.
I will try the reference again
the reference you supplied is working now!  So now how do I keep the close event from happening
I missed that bit, sorry. You'll have to move the code to the form's Unload event. Then, you can prevent the form from closing with this line:
Cancel=True

This cancels the Unload event and keeps the form open.
Well still having a bit of an issue ...my save record above isn't happening.  Hey I am done this evening.  I have to be up in a few hours.  Thanks and I will get you the points because you have spent an abnormal amount of time on this because of my relationships.

Thanks for everything
OK, post back when you return and we can try to troubleshoot the remaining problem.
Sure will.  Thanks