访问从一个表添加记录

时间:2014-12-23 03:40:51

标签: ms-access-2010

我有一张表EmpRequirements,字段是:

EmpRequirementsID (AutoNumber), 
EmpID (from the Employees Table), 
RequirementID (from the Requirement Table), 
RequirementName (from the Requirement Table), 
Received_OnFile (Check), 
NotApplicable (Check), 
DateReceived (Date), 
ExpirationDate (Date)

我想要做的是有一个按钮(添加要求),在Click上将添加需求表(需求ID和要求名称)中的所有数据,用于表单上最新的员工。如果我添加要求,我希望能够单击按钮,它将向该员工添加当前不存在的任何和所有要求。 然后,这将在表单中显示为子表单数据表,然后用户可以编辑EmpRequirements中的所有其他字段。 我曾尝试使用追加和更新,迄今为止尚未提出答案。我想知道这是否只是一个容易写的宏。

我尝试使用此语句添加记录,我已经出错了处理程序,并且得到运行时错误3134 - 语法错误插入语句

Private Sub Add_Click()

'  On Error GoTo Err_Add_Click

    Dim strSQL1 As String

    strSQL1 = "INSERT IGNORE INTO [EmpRequirements]( [EmpID], [RequirementID], [RequirementName] )" & _
          "SELECT DISTINCT [Employees].[EmpID], [Requirement].[RequirementID], [Requirement].[RequirementName]" & _
          "FROM [Employees], [Requirement]  " & _
          "WHERE (((Employees.EmpID)=[Forms]![Employee Details]![EmpID]));"

   DoCmd.RunSQL strSQL1

 [Forms]![Employee Details].Refresh          'Refresh after update EmpRequirements Subform

'Exit_Add_Click:                             ' Label to resume after error.
'     Exit Sub                               ' Exit before error handler.
'Err_Add_Click:                              ' Label to jump to on error.
'     MsgBox Err.Number & Err.Description    ' Place error handling here.
'     Resume Exit_Add_Click                  ' Pick up again and quit.


End Sub

这会起作用,你可以帮忙解决错误吗?

1 个答案:

答案 0 :(得分:0)

以下是我提出的答案

Private Sub Add_Click()

On Error GoTo errHandler

Dim strSQL1 As String

DoCmd.SetWarnings False

strSQL1 = "INSERT INTO EmpRequirements ( EmpID, RequirementID, RequirementName ) " & _
            "SELECT SQ1.EmpID, SQ1.RequirementID, SQ1.RequirementName " & _
            "FROM (SELECT Employees.EmpID, Requirement.RequirementID, Requirement.RequirementName " & _
            "FROM Employees, Requirement WHERE (((Employees.EmpID)=[Forms]![Employee Details]![EmpID]))) " & _
            "AS SQ1 LEFT JOIN EmpRequirements ON (SQ1.EmpID=EmpRequirements.EmpID) " & _
            "AND (SQ1.RequirementID= EmpRequirements.RequirementID) " & _
            "WHERE EmpRequirements.RequirementID IS NULL;"

DoCmd.RunSQL strSQL1

DoCmd.SetWarnings True

[表格]![员工详细信息] .Refresh'更新后需要刷新EmpRequirements子表单

在错误处理程序之前退出Sub'Exit。

errHandler:'标签跳转到错误。         MsgBox“错误”& Err.Number& “:”& Err.Description& “在”& _         VBE.ActiveCodePane.CodeModule,vbOKOnly,“错误”

End Sub

全部谢谢