在Access 2007中。我们在表单上输入新记录。然后我们单击一个具有宏操作的按钮来打开另一个表单。我该怎么做才能打开表单但链接到新记录?我知道我需要新表单上的主键等。如果保存了记录,我可以使用该数据打开第二个表单。但我的问题是,当它仍然是一个新的记录/表格时,我们需要直接导航到链接的表格......
请分享一些方向......
答案 0 :(得分:1)
如果我理解您的问题,您需要执行以下操作:
我有一个在我的数据库中做类似事情的过程,我们这样做。
我点击按钮的代码是在VBA中:
Private Sub OK_Click()
Dim rst As DAO.Recordset
Dim rst1 As DAO.Recordset
Dim sqlStr As String
Dim RptID As Variant
Set rst = CurrentDb.OpenRecordset("tble_Investigations", dbOpenDynaset, dbSeeChanges)
' here you need to add each of your fields from the form
rst.AddNew
rst![Table.Column1] = Me![FormField1]
rst![Table.Column2] = Me![FormField2]
rst![Table.Column3] = Me![FormField3]
rst.Update
' my sql string to return the new ID of the record I just added
sqlStr = "Select Max([ID]) as [MaxOfID] from tble_Investigations;"
Set rst1 = CurrentDb.OpenRecordset(sqlStr, dbOpenDynaset, dbSeeChanges)
rst1.MoveFirst
RptID = rst1![MaxOfID]
'here we open the Form2 with the new id.
DoCmd.OpenForm "Frm_Details", acNormal, , "[ID]= " & RptID, acFormEdit, acWindowNormal
DoCmd.Close acForm, "Frm_New", acSaveYes
End Sub
编辑:
根据您所说的,听起来您正在执行以下操作:
Private Sub OK_Click()
Dim rst As DAO.Recordset
Dim rst1 As DAO.Recordset
Dim sqlStr As String
Dim RptID As Variant
Set rst = CurrentDb.OpenRecordset("t_Evaluation", dbOpenDynaset, dbSeeChanges)
' here you need to add each of your fields from the form
rst.AddNew
rst![ExecutionLeadOrg] = Me![ExecutionLeadOrg] 'the field from your form that matches the table column
rst![TitleID] = Me![TitleID]
rst![t_Evaluation.EvalTypeID] = Me![t_Evaluation.EvalTypeID]
rst![SectionID] = Me![SectionID]
rst![LOBEvaluation] = Me![LOBEvaluation]
'you need to continue doing this for each field on your form
rst.Update
' my sql string to return the new ID of the record I just added
sqlStr = "Select Max([EvaluationID]) as [MaxOfID] from t_Evaluation;"
Set rst1 = CurrentDb.OpenRecordset(sqlStr, dbOpenDynaset, dbSeeChanges)
rst1.MoveFirst
RptID = rst1![MaxOfID]
'here we open the Form2 with the new id.
DoCmd.OpenForm "f_LOBevalPopUpEntry", acNormal, , "[EvaluationID]= " & RptID, acFormEdit, acWindowNormal
DoCmd.Close acForm, "F_EvalNew", acSaveYes
End Sub
答案 1 :(得分:0)
您只需要保存当前记录,然后将下一个表单启动到SAME记录。只要推出的表格是模型,那么你就没事了。并且为了在这里提出任何令人困惑的建议,模型形式是巨大的差异,然后启动对话框形式。
但是,您也可以考虑启动对话框表单。
当您返回上一个表单时,Access作为一般规则将显示任何更新。
您需要的代码是:
If me.Dirty = True then Me.Dirty = false
Docmd.OpenForm "name of next form",,,"id = " & me!id
此外,在一系列操作中启动另一个表单时,最好强制按照上面的方式保存记录。
因此,只需要上面两行代码。