单击保存按钮时,将现有记录从主窗体添加到子窗体

时间:2013-06-20 17:26:11

标签: ms-access ms-access-2007 access-vba

当我努力完成最终结果时,任何人都可以提供帮助:

我有一个未绑定的mainform,得到了一个绑定的子表单,得到了两个表Masterplant和PlantTransaction。

当我编辑它在主窗体上显示的记录时,当我保存记录时,它必须复制子窗体中的现有记录,这是有效的,但诀窍是新记录中的开放时间必须成为我的截止时间以前的记录,一切正常,只是开放时间不会从截止时间前一个记录显示,而且TransactionID是一个必须使用不同的TransactionID号自动递增的数字字段。任何帮助将不胜感激,提前谢谢!!!

以下代码:

Private Sub cmdSave_Click()
Dim strSQL As String
Dim rst As DAO.Recordset
If IsNull(txtOpeningHRS) Then
  Set rst = Me.RecordsetClone
  If rst.RecordCount > 0 Then
    If Me.NewRecord Then
      rst.MoveLast
    Else
      rst.Bookmark = Me.Bookmark
      rst.MovePrevious
    End If
    txtOpeningHRS = rst!CloseHrs
  End If
End If

If IsNull(Me.TransactionID) Or Me.TransactionID = 0 Then
  Me.TransactionID = Nz(DMax("TransactionID", "PlantTransaction") + 1, 1234)
End If

strSQL = "INSERT INTO PlantTransaction(TransactionID, [Plant Number], Opening_Hours, 
[TransactionDate], [FuelConsumption], [Hour Meter Replaced], Comments, [Hours Worked]) & _ 
   strSQL & "VALUES(" & Me.txtTranID & ",'" & Me.txtPlantNo & "','" & Me.txtOpeningHRS & "',#" & 
   Me.txtTransDate & "#,'" & Me.txtFuelConsFuelHr & "','" & Me.txtHrMtrRep & "','" & Me.txtComments   
   & "','" & Me.txtHrsWorked & "');"
CurrentDb.Execute strSQL
Me.PlantTransactionQuery.Form.Requery
cmdNew_Click
End Sub

1 个答案:

答案 0 :(得分:0)

将TransactionID设置为AutoNumber数据类型,这将自动增加它。

您也可以尝试替换:

CurrentDb.Execute strSQL
Me.PlantTransactionQuery.Form.Requery

使用:

CurrentDb.Execute strSQL
Me!PlantTransactionQuery.Form.RowSource = "Select * from PlantTransaction"
Me.PlantTransactionQuery.Form.Refresh