2

I have a form that opens and takes data from a table; and puts it in text boxes. There is button on this form, named "CustomerInfoBackBtn".

The code I have inside of it that doesn't work (well, it might... just Access automatically saves the data anyways when I edit the text boxes) is this:

Private Sub CustomerInfoBackBtn_Click()
Dim LResponse As Integer

LResponse = MsgBox("Would you like to save?", vbYesNo, "Save?")

If LResponse = vbYes Then
   DoCmd.RunCommand acCmdSaveRecord
   DoCmd.Close
   DoCmd.OpenForm "CustomerListF"
Else
   DoCmd.Close
   DoCmd.OpenForm "CustomerListF"
End If
End Sub

How do I make it pop up the msgbox asking them if they would like to save, and if they push yes it saves, then refreshes the subform and THEN opens the previous form (CustomerListF) and if they push no, it doesn't save, reverts information to what it was before, and opens up the previous form? I think all I really need is a way to stop access from automatically saving the data changes, but I am not sure.

Edit for answer:

Code in button that pulls up that error:

Dim TempSaveRecord As Boolean

Private Sub CustomerNotesBackBtn_Click()
  If MsgBox("Do you want to save your changes?", vbInformation + vbYesNo, [Warning! Some data may be lost.]) = vbNo Then
    TempSaveRecord = False
  Else
    TempSaveRecord = True
  End If

  DoCmd.Close
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
  If (TempSaveRecord) Then
    DoCmd.Save
  Else
    DoCmd.RunCommand acCmdUndo
  End If
End Sub
4

2 回答 2

4

Here is what I do to control whether a record is saved or not.

Make sure that the Close Button property of the form is set to No so that the user will have to click on the Back button, Then use the following code:

Dim TempSaveRecord as Boolean

Private Sub cmdBack_Click()
  If MsgBox("Do you want to save your changes?", vbInformation + vbYesNo) = vbNo Then
    TempSaveRecord = False
  Else
    TempSaveRecord = True
  End If

  DoCmd.Close
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
  If (TempSaveRecord)  Then 
    DoCmd.Save
  Else
    DoCmd.RunCommand acCmdUndo
  End If
End Sub

Then when the Form closes you can force any other form to Refresh using the following:

Private Sub Form_Close()
  [Forms]![MyFormName].Refresh
End Sub
于 2013-10-29T14:45:57.607 回答
1

Form bounded DAO.Recordset are saved automatically by DAO Engine without any user action. In your case Modifications can be saved any time without warning, or before clicking on [Back] or [Close] button.

You can work around like this with transaction here or ADO:

Option Compare Database
Option Explicit

Private boolFrmDirty As Boolean
Private boolFrmSaved As Boolean

Private Sub Form_AfterDelConfirm(Status As Integer)
    If Me.Saved = False Then Me.Saved = (Status = acDeleteOK)
End Sub

Private Sub Form_AfterUpdate()
    Me.Saved = True
End Sub

Private Sub Form_Delete(Cancel As Integer)
    If Me.Dirtied = False Then DBEngine.BeginTrans
    Me.Dirtied = True
End Sub

Private Sub Form_Dirty(Cancel As Integer)
    If Me.Dirtied = False Then DBEngine.BeginTrans
    Me.Dirtied = True
End Sub

Private Sub Form_Open(Cancel As Integer)
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM Customers", dbOpenDynaset)
    Set Me.Recordset = rs
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim msg As Integer
    If Me.Saved Then
        msg = MsgBox("Do you want to commit all changes?", vbYesNoCancel)
        Select Case msg
            Case vbYes
                DBEngine.CommitTrans
            Case vbNo
                DBEngine.Rollback
            Case vbCancel
                Cancel = True
        End Select
    Else
        If Me.Dirtied Then DBEngine.Rollback
    End If
End Sub

Public Property Get Dirtied() As Boolean
    Dirtied = boolFrmDirty
End Property

Public Property Let Dirtied(boolFrmDirtyIn As Boolean)
    boolFrmDirty = boolFrmDirtyIn
End Property

Public Property Get Saved() As Boolean
    Saved = boolFrmSaved
End Property

Public Property Let Saved(boolFrmSavedIn As Boolean)
    boolFrmSaved = boolFrmSavedIn
End Property

If you use ADODB.Recordset as form.Recordset,

Dim rst As ADODB.Recordset
'
'... Create it by querying a remote db.
'
Set Me.Recordset = rst

You thus can control saving or abandon of user modifications, As ADO driver can not save to the Database itself... It's more complicated.

Code from Reference

于 2013-10-29T15:44:22.117 回答