我可以用事务包装 Access 表单吗?

发布于 2024-07-26 23:36:43 字数 177 浏览 5 评论 0原文

我想制作一个表单,本质上是创建发票,但使用一些其他相关数据作为输入或限制; 在向发票添加项目的过程中,我需要减少另一个表中的项目。 由于用户一次会输入多个项目,因此我想在表单加载时发出“START TRANSACTION”,然后在表单更新时发出“COMMIT”。 因此,如果他们取消表单,其他相关表(通过子表单显示)将回滚到以前的值。

I want to make a form the essentially creates an invoice, but using some other related data as inputs or limits; In the process of adding items to the invoice, I need to reduce the items in another table. Since the user will enter several items at a time, I'd like to issue a "START TRANSACTION" when the form loads, and then do a "COMMIT" when the form updates. Thus, if they cancel the form, the other related tables (shown via subforms) would roll back to the previous values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何必那么矫情 2024-08-02 23:36:43

无法使用绑定表单来完成。 您可以使用临时表来存储数据,然后更新主表。 有点拼凑,但我过去就这样做过。

请参阅我网站上的 TempTables.MDB 页面,其中说明了如何使用临时表您的应用程序中的 MDB。

Can't be done using bound forms. You could use temporary tables to store the data and then update the main tables. A bit of a kludge but I've done that in the past.

See the TempTables.MDB page at my website which illustrates how to use a temporary MDB in your app.

拔了角的鹿 2024-08-02 23:36:43

是的,可以做到,要以您需要使用以下代码的形式控制交易:

Private Sub Form_Open(Cancel As Integer)
    Set Me.Recordset = CurrentDb.OpenRecordset("NAME_OF_YOUR_TABLE_OR_QUERY")
End Sub

之后就可以使用DBEngine来控制事务了。

它对我有用(我使用 Access 2007)

注意:如果使用表单界面插入新记录,则在引发 Form_AfterInsert 事件时它是可见的,因此您可以在该事件中使用 DbEngine.Rollback 来撤消更改。

Yes it can be done, to take control over the transaction in the form you need use this code:


Private Sub Form_Open(Cancel As Integer)
Set Me.Recordset = CurrentDb.OpenRecordset("NAME_OF_YOUR_TABLE_OR_QUERY")
End Sub

After that, you can use DBEngine to control the transaction.

It work for me (Im using Access 2007)

Note: If you insert a new record using the form interface it is visible when the Form_AfterInsert event is raised, therefore you can use DbEngine.Rollback in that event to undo the changes.

夏有森光若流苏 2024-08-02 23:36:43

我发现可以将它放在装订表格上。 您需要在任何父控件的更改事件上分配包含 ID 号的变量所需的一切。 您需要将该 ID 值传输到子表单连接字段中,并在主表单和子表单上执行事务。 这是我如何做到这一点的示例。

Primary Form VBA

Option Compare Database
Option Explicit

Private boolFrmDirty As Boolean
Private boolFrmSaved As Boolean

Private Sub EmpolyeesID_Change()
Dim ordID As Integer
Dim subFormOrdID As Object

Set subFormOrdID = Forms!Order.OrderInstallation.Form!OrderID

ordID = Me.Form!OrderID

subFormOrdID.DefaultValue = ordID

End Sub

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
'Check if form has got new values in it
Private Sub Form_Dirty(Cancel As Integer)
    If Me.Dirtied = False Then DBEngine.BeginTrans
    Me.Dirtied = True
End Sub
'Open Form as a Record Set and set the variables for it
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 Orders", dbOpenDynaset, dbAppendOnly)
    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

Private Sub ProductID_AfterUpdate()
'Calculations of VAT and Floor Price
Dim clcVAT As Integer
Dim sqlQry As String
Dim instID As Integer

instID = Me.Form!ProductID.Value

sqlQry = "SELECT Products.Price FROM Products WHERE Products.ProductID =" & instID & ""

Me.flPrice.RowSource = sqlQry

End Sub

Sub Form VBA 

Option Compare Database
Option Explicit
'Transaction for sub-form
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 OrderInstallation")
    Set Me.Recordset = rs
End Sub
Private Sub Form_AfterUpdate()
    Dim emplID As Object
    Dim cstmID As Object
    Dim prdcID As Object
    Dim DataArray As Variant
    Dim RqrdFieldErorr As String
    Dim qry As String

    Set emplID = Me.Parent!EmpolyeesID
    Set cstmID = Me.Parent!CustomerID
    Set prdcID = Me.Parent!ProductID

    If IsNull(emplID.Value) Or IsNull(cstmID.Value) Or IsNull(prdcID.Value) Then
        MsgBox ("Please enter select required fields first")

        Else
    End If
End Sub
'Restrict updates of Installation subform if Employee, Customer and Product is not selected
Private Sub InstallationID_AfterUpdate()
    Dim instID As Integer
    Dim instPrice As Integer
    Dim strQry As String

    ' Create query based on InstallationID value
    instID = InstallationID.Value
    strQry = "SELECT Installation.Price, Installation.InstallationID FROM Installation WHERE Installation.InstallationID =" & instID & ""
    Me.Price.RowSource = strQry
End Sub

I have figured it out its possible to have it on bound forms. Everything you need to assign variable that contain an ID number on change event of any of the parent control. Than you need to transmit that ID value into the subform connected field and perform transaction on both forms the primary and the subform. Here is the example of how I did it.

Primary Form VBA

Option Compare Database
Option Explicit

Private boolFrmDirty As Boolean
Private boolFrmSaved As Boolean

Private Sub EmpolyeesID_Change()
Dim ordID As Integer
Dim subFormOrdID As Object

Set subFormOrdID = Forms!Order.OrderInstallation.Form!OrderID

ordID = Me.Form!OrderID

subFormOrdID.DefaultValue = ordID

End Sub

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
'Check if form has got new values in it
Private Sub Form_Dirty(Cancel As Integer)
    If Me.Dirtied = False Then DBEngine.BeginTrans
    Me.Dirtied = True
End Sub
'Open Form as a Record Set and set the variables for it
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 Orders", dbOpenDynaset, dbAppendOnly)
    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

Private Sub ProductID_AfterUpdate()
'Calculations of VAT and Floor Price
Dim clcVAT As Integer
Dim sqlQry As String
Dim instID As Integer

instID = Me.Form!ProductID.Value

sqlQry = "SELECT Products.Price FROM Products WHERE Products.ProductID =" & instID & ""

Me.flPrice.RowSource = sqlQry

End Sub

Sub Form VBA 

Option Compare Database
Option Explicit
'Transaction for sub-form
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 OrderInstallation")
    Set Me.Recordset = rs
End Sub
Private Sub Form_AfterUpdate()
    Dim emplID As Object
    Dim cstmID As Object
    Dim prdcID As Object
    Dim DataArray As Variant
    Dim RqrdFieldErorr As String
    Dim qry As String

    Set emplID = Me.Parent!EmpolyeesID
    Set cstmID = Me.Parent!CustomerID
    Set prdcID = Me.Parent!ProductID

    If IsNull(emplID.Value) Or IsNull(cstmID.Value) Or IsNull(prdcID.Value) Then
        MsgBox ("Please enter select required fields first")

        Else
    End If
End Sub
'Restrict updates of Installation subform if Employee, Customer and Product is not selected
Private Sub InstallationID_AfterUpdate()
    Dim instID As Integer
    Dim instPrice As Integer
    Dim strQry As String

    ' Create query based on InstallationID value
    instID = InstallationID.Value
    strQry = "SELECT Installation.Price, Installation.InstallationID FROM Installation WHERE Installation.InstallationID =" & instID & ""
    Me.Price.RowSource = strQry
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文