我有一个表单打开并从表中获取数据;并将其放在文本框中。此表单上有一个按钮,名为“CustomerInfoBackBtn”。
我内部的代码不起作用(好吧,它可能......只是在编辑文本框时Access自动保存数据)是这样的:
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
如何让它弹出msgbox询问他们是否要保存,如果他们按下它会保存,然后刷新子表单然后打开上一个表单(CustomerListF),如果他们不推,它就不会保存,将信息恢复到之前的状态,并打开上一个表格?我认为我真正需要的是一种阻止访问自动保存数据更改的方法,但我不确定。
编辑回答:
按钮中的代码可以提取错误:
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
答案 0 :(得分:2)
以下是我控制是否保存记录的方法。
确保表单的 关闭按钮 属性设置为 否 ,以便用户可以必须单击 返回 按钮,然后使用以下代码:
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
然后当表单关闭时,您可以使用以下命令强制刷新任何其他表单:
Private Sub Form_Close()
[Forms]![MyFormName].Refresh
End Sub
答案 1 :(得分:1)
表单绑定DAO.Recordset由DAO Engine自动保存,无需任何用户操作。在您的情况下,可以在没有警告的情况下,或在单击[返回]或[关闭]按钮之前随时保存修改。
您可以使用此处的交易或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
如果您使用ADODB.Recordset作为form.Recordset,
Dim rst As ADODB.Recordset
'
'... Create it by querying a remote db.
'
Set Me.Recordset = rst
您可以控制保存或放弃用户修改,因为ADO驱动程序无法保存到数据库本身......它更复杂。