【问题标题】:open from from newly entered record before save保存前从新输入的记录打开
【发布时间】:2011-07-22 09:48:03
【问题描述】:

在 Access 2007 中。我们在表单上输入新记录。然后我们单击一个具有宏操作的按钮来打开另一个表单。我该怎么做才能使表单打开但链接到新记录?我知道我需要新表单上的主键等。如果记录已保存,我可以使用该数据打开第二个表单。但我的问题是,当它仍然是新记录/表单时,我们需要直接导航到链接表单...

请在这方面分享一些方向..

【问题讨论】:

标签: ms-access vba


【解决方案1】:

如果我理解您的问题,您需要执行以下操作:

  1. 在 Form1 上添加新记录
  2. 单击 Form1 上的按钮打开 Form2
  3. 当 Form2 打开时,它包含来自 Form1 的信息

我有一个进程在我的数据库中执行类似的操作,我们以这种方式执行操作。

  1. 用户有一个表单来输入新记录
  2. 他们使用按钮单击将新数据发布到表中。在此过程中,我将主键返回到新记录。然后是按钮单击的最后一个过程,我告诉它打开新表单,并用我刚刚抓取的主键填充它。
  3. 使用 PK 记录打开新表单。

我的按钮点击代码在 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

【讨论】:

  • 我将您的建议添加到我的按钮代码中 - 它会爆炸,当我调试时它处于 rst!Status = "OPEN" 我不确定用什么替换 "OPEN" ??????
  • 正如我所说,这是我的代码。您需要做的是从表中添加字段。如果你用你的表结构编辑你的 OP,我会告诉你我的意思。
  • 是的,我明白了,我已经输入了代码来反映我的表格和 ID 字段等。但我不明白这个 rst!Status = "OPEN"???
  • 这是我要添加到表格中的字段之一。让我编辑我的代码
  • 这就是我所拥有的。复制到 table=t_Evaluation,key=EvaluationID。新表=f_LOBevalPopUpEntry。我认为除了 rst!Status... ("t_Evaluation", dbOpenDynaset, dbSeeChanges) rst!Status = "EvaluationID" rst.Update sqlStr = "Select Max([Eval_ID]) as [MaxOfID] from t_Evaluation; " RptID = rst1![MaxOfID] DoCmd.OpenForm "f_LOBevalPopUpEntry", acNormal, "[Eval_ID]= " & DoCmd.close acForm, "F_EvalNew", acSaveYes
【解决方案2】:

您只需要保存当前记录,然后启动下一个表单到 SAME 记录。只要启动的表格是模型,那么你就可以了。并且为了避免任何混淆,模型表单与启动对话框表单有很大的不同。

不过,您也可以考虑启动对话框表单。

当您返回之前的表单时,Access 通常会显示任何更新。

你需要的代码是这样的:

If me.Dirty = True then Me.Dirty = false
Docmd.OpenForm "name of next form",,,"id = " & me!id

此外,在一系列操作中启动另一个表单时,最好按照上述方式强制保存记录。

所以,只需要上面两行代码。

【讨论】:

  • 我试图让它工作,这就是我所拥有的,包括打开和关闭表单的最终命令。你能帮我翻译一下语法吗? Private Sub Label143_Click() If me.Dirty = True then Me.Dirty = false Docmd.OpenForm "f_LOBevalPopUpEntry",,,"Eval_ID = " & me!Eval_ID; DoCmd.OpenForm "f_LOBevalPopUpEntry", acNormal, , "[Eval_ID]= " & RptID, acFormEdit, acWindowNormal DoCmd.close acForm, "F_EvalNew", acSaveYes End Sub
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 2018-07-24
相关资源
最近更新 更多