Results 1 to 15 of 15
  1. #1
    Lxmanager is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    24

    Closing and saving a form

    Hi guys



    Just got a quick question.. I have a form linked to a table, on the form I have a button on it to exit the form. I have also created a small form as a measage box. So if the form has been changed (is dirty) then the pop up form appeares asking if the form is to be saved, if there is no changes then the form just closes.

    If I open the very first record in the table in this form and save it both as changed or not changed the code works well (ie the pop up box appears when changed and the form just closes if not).

    The problem is for every other form in the table the pop up box appears if the form has been changed or not so the code is assuming the form is dirty every time...

    Where have I gon wrong???

    Thanks

    Andy

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    How are you testing for Dirty?

  3. #3
    Lxmanager is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    24
    Hi, the code I have on the forms close button is:-

    Private Sub cmdSave_Click()

    If Me.Dirty = True Then
    Me.Dirty = False
    DoCmd.OpenForm "frmCloseInspFrm"
    Else
    DoCmd.Close
    DoCmd.OpenForm "Records_Switchboard"

    End If

    End Sub

    As I said, it seams to work ok for the first record in the table bot for all others in opens the pop up form "frmCloseInspFrm". Also I have a problem with that, on the form it has a statment stating that the form has changed and do i want to save the changes, then there at two buttons, yes & no. If I select yes the form closes and saves the record, great... But if I select no, the form closes but still saves the record on the form, I want this to close the form without saving, the codes I have on the yes no buttons are:-

    Private Sub cmdCloseYes_Click()

    DoCmd.Close acForm, "frmInspSht", acSaveYes
    DoCmd.Close acForm, Me.Name
    DoCmd.OpenForm "Records_Switchboard"
    End Sub

    Private Sub cmdCloseNo_Click()

    DoCmd.Close acForm, "frmInspSht", acSaveNo
    DoCmd.Close acForm, Me.Name
    DoCmd.OpenForm "Records_Switchboard"
    End Sub

    Can someone help?

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Let's start here:
    DoCmd.Close acForm, "frmInspSht", acSaveYes
    ..the highlighted parameter is for saving changes to a FORM and *not* the recordset behind it.

  5. #5
    Lxmanager is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    24
    Ive got no problem savin the form, the problem is it saves the form even when I dont want to, and the is dirty command only works with the first record in the tale and not with the rest.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The only time you want to save changes to a FORM is when you have modified a form which is usually done in design mode. Are you modifying the form? I'm not talking about the Recordset display in a form.

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Unless you make other arrangements, a form will *always* save the current record if it is Dirty.

  8. #8
    Lxmanager is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    24
    I understand that but the record is being saved even if there are no changes made (ie not dirty) except for the first record.

    And yes it is the recordset I want to save or not save, not the form.

    Basically what I want is when the operator goes into the form and enters data into it, then clicks on the close button I have created a pop up form appears and asks if they want to save the changes made, if they click YESthen the record displayed by the form is saved and the form is closed, if press NO then all the changes to the record are ignored and the form closses without saving thhe record.

    If nothing was touched on the form when the close button was clicked the form just closes.

    I know the code I have shown above is not right but don't know why.

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    All you need to do what you ask is this small piece of code in the BeforeUpdate event of the form.
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
       If MsgBox("Do you want to save the changes?", vbYesNo) = vbNo Then
          Me.Undo
       End If
    End Sub
    The BeforeUpdate event *only* executes just before the form is about to save a record for *any* reason, including pressing the RED X in the upper right corner of the form. The close button just needs to close the form with: DoCmd.Close acForm, Me.Name, acSaveNo
    You could also open the Switchboard next with: DoCmd.OpenForm "Records_Switchboard" if that is always where you want to take the user when they close this form. Try it.

  10. #10
    Lxmanager is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    24
    Hi, thanks for the advice, I have tried that code on the BeforeUpdate event, it seams to work ok but the msgBox is now poping up when I open up the form from the switchboard, why is this.

    Also what would I need to do if I want to use my own measage box I created as a pop up form.

    Thanks

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by Lxmanager View Post
    Hi, thanks for the advice, I have tried that code on the BeforeUpdate event, it seams to work ok but the msgBox is now poping up when I open up the form from the switchboard, why is this.
    The switchboard must be modifying the RecordSource of the form in some way. What is the code in the Switchboard that opens this form?
    Quote Originally Posted by Lxmanager View Post
    Also what would I need to do if I want to use my own measage box I created as a pop up form.
    You would need to pass back the selection from Your PopUp. Here is one method but there are many ways to skin this cat. http://www.pacificdb.com.au/Support/...stomdialog.htm

  12. #12
    Lxmanager is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    24
    I have several buttons to open the form depending on what is being filled in, one of them has code as follows

    Private Sub cmdGotoCompRecord_Click()
    On Error GoTo Err_cmdGotoCompRecord_Click

    Dim strReport As Integer
    Dim tmp As Integer

    strReport = Me.cboSelectCompAction

    DoCmd.OpenForm "frmInspSht", acNormal
    'abort if number not selected
    If Len(Trim(strReport)) = 0 Then
    MsgBox "Report number Not Selected"
    End If

    'get current ReportNo
    tmp = Forms!frmInspSht.ReportNo
    Forms!frmInspSht!ReportNo.Enabled = True
    Forms!frmInspSht!ReportNo.SetFocus
    DoCmd.FindRecord strReport
    ' move to some other control to
    ' be able to be able to set
    ' ReportNo Enabled to false
    Me.cmdGotoCompRecord.SetFocus
    Forms!frmInspSht!ReportNo.Enabled = False
    'check if the report no. was found and
    If (tmp = Forms!frmInspSht!ReportNo) And (Forms!frmInspSht!ReportNo <> strReport) Then
    MsgBox "Report number " & strReport & " Not found"
    End If
    Forms!frmInspSht!CompYesNo.Enabled = True
    Forms!frmInspSht!CompBy.Enabled = True
    Forms!frmInspSht!AccCompDate.Enabled = True
    Forms!frmInspSht!CompBy.BackColor = vbYellow
    Forms!frmInspSht!AccCompDate.BackColor = vbYellow
    Forms!frmInspSht!cmdGotoRecord.Visible = False
    Forms!frmInspSht!cboSelectRptNo.Visible = False
    Forms!frmInspSht!boxGoTo.Visible = False

    Const acScrollBarsNeither = 0
    Forms!frmInspSht.PictureTiling = True

    Forms!frmInspSht.NavigationButtons = False
    Forms!frmInspSht.RecordSelectors = False
    Forms!frmInspSht.DividingLines = False
    Forms!frmInspSht.ScrollBars = acScrollBarsNeither
    DoCmd.Close acForm, Me.Name
    If Forms!frmInspSht.CompYesNo = -1 Then
    DoCmd.OpenForm "frmAlreadyComp"
    Forms!frmInspSht!CompYesNo.Enabled = False
    Forms!frmInspSht!CompBy.Enabled = False
    Forms!frmInspSht!AccCompDate.Enabled = False
    Forms!frmInspSht!CompBy.BackColor = vbWhite
    Forms!frmInspSht!AccCompDate.BackColor = vbWhite
    Forms!frmInspSht!cmdSave.Visible = False
    Forms!frmInspSht!cmdReport.Visible = False
    End If
    Exit_cmdGotoCompRecord_Click:
    Exit Sub
    Err_cmdGotoCompRecord_Click:
    MsgBox Err.Description
    Resume Exit_cmdGotoCompRecord_Click
    End Sub

    Is the problem because i am enabling fields etc???

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by Lxmanager View Post
    Is the problem because i am enabling fields etc???
    At the very least! What is your background? VB? I mean no offense so don't take this wrong but you really could use a refresher in Object Oriented - Event Driven Software. You are manhandling that next form from this form when the next form could be doing all of the work for you and keep the logic local to the form doing the work.

  14. #14
    Lxmanager is offline Novice
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Posts
    24
    Yeah I know what you are saying, the story is I was asked to do this database because I knew Access. The problem is I didn't know VBA so I have been self teaching myself which as you can see is hard work.

    The reason why I did the code as above on the first form is because on the main form there are certain parts of it that are filled in by certain people so to save having all the fields open all the time making it confusing to them I have tried to set it up so that the person filling in say material properties on the form clicked on that button on the switchboard and the form opens with only those fields enabled ect.

    That was the only way I could think of with my limited knowlage. That is why I am on here asking for help from you profesionals,

    It looks as though I will have to do some more research and figure this out myself.

  15. #15
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    We would be glad to assist in your efforts but a full project is beyond the scope of this Forum. You know you can pass a large amount of data to the next form in the OpenArgs argument of the OpenForm command, right. You could move most of the posted code to the OnLoad event of the next form and decode the OpenArgs to make your decisions. Some of what you are doing to the next form is not necessary and some may be accomplished with Conditional Formatting. You could certainly stop the form from wanting to save the altered Recordset.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Prevent a form from closing
    By ksmithson in forum Forms
    Replies: 0
    Last Post: 07-15-2010, 12:49 PM
  2. Replies: 1
    Last Post: 06-25-2010, 09:56 AM
  3. Closing a Form Problem
    By MuskokaMad in forum Forms
    Replies: 2
    Last Post: 03-18-2010, 05:58 AM
  4. Replies: 2
    Last Post: 03-14-2010, 08:21 AM
  5. Replies: 9
    Last Post: 12-28-2009, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums