MS Access: Save form data to 2 tables at the click of a button

后端 未结 2 525
有刺的猬
有刺的猬 2020-12-20 03:39

I am creating an asset management database with 2 tables (Assets and AssetMovements) and one form (Assets). I need to keep records of every asset movement, so every time a

2条回答
  •  情书的邮戳
    2020-12-20 04:19

    The button has a click event that is fired when you click it. Use VBA to write some code that is executed when the button is clicked.
    When you click the button, you would then save the record to Assets. Then copy that record to the AssetMovements table using a query. So, the code will look something like this:

    Dim OldLocation As String    
    
    Private Sub CmdSave_Click()
       DoCmd.RunCommand acCmdSaveRecord
    End Sub
    
    Private Sub Form_AfterUpdate()
       Dim strSQL
       If OldLocation <> Location.Value Then
          strSQL = "INSERT INTO AssetMovements SELECT T1.* FROM Assets WHERE Assets.ID = "
          strSQL = strSQL & Me.ID
          CurrentDb.Execute strSQL
       End If
    End Sub
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
       OldLocation = Me.Location.OldValue
    End Sub
    
    Private Sub Form_Current()
       OldLocation = Me.Location.Value
    End Sub
    

    This will then copy the current record of the form, using a unique ID (I guessed at AssetID), to the AssetMovement table.

提交回复
热议问题