Ms access 保存前从新输入的记录打开

Ms access 保存前从新输入的记录打开,ms-access,vba,Ms Access,Vba,在Access 2007中。我们在表格上输入新记录。然后我们点击一个按钮,它有一个宏操作来打开另一个窗体。我可以做些什么使表单打开但链接到新记录?我知道我需要新表单上的主键等。如果记录已保存,我可以使用该数据打开第二个表单。但我的问题是,当它还是一个新的记录/表单时,我们需要直接导航到链接表单 请在此分享一些指导。如果我理解您的问题,您需要执行以下操作: Private Sub OK_Click() Dim rst As DAO.Recordset Dim rst1 As DAO.Rec

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


请在此分享一些指导。

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

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
  • 在Form1上添加新记录
  • 单击Form1上的按钮打开Form2
  • 当Form2打开时,它包含来自Form1的信息
  • 我的数据库中有一个做类似事情的流程,我们就是这样做的

  • 用户有一个表单来输入新记录
  • 他们使用单击按钮将新数据发布到表中。在此过程中,我将主键返回到新记录。然后点击按钮的最后一个过程,我告诉它打开新表单,并用我刚才抓取的主键填充它
  • 使用记录的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
    

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

    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
    
  • 在Form1上添加新记录
  • 单击Form1上的按钮打开Form2
  • 当Form2打开时,它包含来自Form1的信息
  • 我的数据库中有一个做类似事情的流程,我们就是这样做的

  • 用户有一个表单来输入新记录
  • 他们使用单击按钮将新数据发布到表中。在此过程中,我将主键返回到新记录。然后点击按钮的最后一个过程,我告诉它打开新表单,并用我刚才抓取的主键填充它
  • 使用记录的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
    

    您只需保存当前记录,然后启动同一记录的下一个表单。只要启动的表单是模型,那么您就可以了。为了避免混淆,模型表单和启动对话框表单有很大区别

    但是,您也可以考虑启动一个对话框窗体。

    当您返回到上一个表单时,Access作为一般规则将显示任何更新

    您需要的代码如下:

    If me.Dirty = True then Me.Dirty = false
    Docmd.OpenForm "name of next form",,,"id = " & me!id
    
    此外,当在一系列操作中启动另一个表单时,强制按上述方式保存记录是一个好主意


    因此,只需要上述两行代码

    您只需要保存当前记录,然后启动同一记录的下一个表单。只要启动的表单是模型,那么您就可以了。为了避免混淆,模型表单和启动对话框表单有很大区别

    但是,您也可以考虑启动一个对话框窗体。

    当您返回到上一个表单时,Access作为一般规则将显示任何更新

    您需要的代码如下:

    If me.Dirty = True then Me.Dirty = false
    Docmd.OpenForm "name of next form",,,"id = " & me!id
    
    此外,当在一系列操作中启动另一个表单时,强制按上述方式保存记录是一个好主意


    因此,只需要上述两行代码

    我把你的建议添加到我的按钮代码中——它爆炸了,当我调试它时,它是第一次!Status=“OPEN”我不确定用什么替换“OPEN”,因为我说过这是我的代码。您需要做的是从表中添加字段。如果你用你的表格结构编辑你的OP,我会告诉你我的意思。是的,我明白,我已经在代码中加入了我的表格和ID字段等,但我不明白这一点!Status=“OPEN”?这是我要添加到表中的字段之一。让我编辑我的代码这是我的。复制到表=t_Evaluation键=EvaluationID。新表格=f_LOBevalPopUpEntry。我想除了rst我什么都做对了!地位(“t_评估”,dbOpenDynaset,dbSeeChanges)rst!Status=“EvaluationID”rst.Update sqlStr=“从t_Evaluation中选择Max([Eval_ID])作为[MaxOfID];RptID=rst1![MaxOfID]DoCmd.OpenForm“f_LOBevalPopUpEntry”,acNormal,“[Eval_ID]=”&DoCmd.close acForm,“f_EvalNew”,acsaveyes我将您的建议添加到我的按钮代码中-它会爆炸,当我调试时它会重新启动!Status=“OPEN”我不确定用什么替换“OPEN”,因为我说过这是我的代码。您需要做的是从表中添加字段。如果你用你的表格结构编辑你的OP,我会告诉你我的意思。是的,我明白,我已经在代码中加入了我的表格和ID字段等,但我不明白这一点!Status=“OPEN”?这是我要添加到表中的字段之一。让我编辑我的代码这是我的。复制到表=t_Evaluation键=EvaluationID。新表格=f_LOBevalPopUpEntry。我想除了rst我什么都做对了!地位(“t_评估”,dbOpenDynaset,dbSeeChanges)rst!Status=“EvaluationID”rst.Update sqlStr=“从t_Evaluation中选择Max([Eval_ID])作为[MaxOfID];RptID=rst1![MaxOfID]DoCmd.OpenForm“f_LOBevalPopUpEntry”,acNormal,“[Eval_ID]=”&DoCmd.close acForm,“f_EvalNew”,acsaveyes我已经尝试过让它工作,这就是我所拥有的,包括打开和关闭表单的最后命令。你能帮我拿一下syntex吗?私有子标签143_Click()如果me.Dirty=True,那么me.Dirty=false Docmd.OpenForm“f_LOBevalPopUpEntry”、“Eval_ID=”&me!评估ID;DoCmd.OpenForm“f_LOBevalPopUpEntry”,acNormal,“[Eval_ID]=”&RptID,acFormEdit,acWindowNormal DoCmd.close acForm,“f_EvalNew”,acSaveYes End sub我已经试着让它工作了,这就是我所拥有的,包括打开和关闭表单的最后命令。库尔