使用表单中的外键插入 table

INSERT INTO table with Foreign Key from Form

我正在尝试创建一个用于在某些情况下手动输入数据的表单。大多数数据是从工作正常的 CSV 文件输入的。我有 4 个 table、PartAssemblyMachineOrderJob。我能够编写用于从表单进入基数 table、Part 的代码,没问题。现在的问题是在 AssemblyMachineOrder table 中输入数据,其中部件由其 PID 自动编号字段引用,而程序集由其 AID 自动编号字段。我已经尝试了许多不同类型的方法来执行此操作,您可以在我注释掉的代码中看到一些方法。到目前为止,我认为最接近正确代码的是什么,现在的错误是 Access 要求我提供 rPID 的参数值,即使它在 Dlookup 函数中找到该值美好的。我假设 rAID 部分也是如此。

否则,在使用 INSERT 然后 UPDATE 方法时,我会收到密钥违规错误,您会看到注释掉了。

表格被称为HOTEntry

非常感谢任何关于我的问题的建议,我是一名学生,这是我第一次尝试在专业应用中使用我所学的知识,因此需要任何和所有建设性的批评!抱歉,如果这是一个相当具体的问题,但我真的可以在这方面使用帮助,因为我已经研究了两天但无济于事...

我的代码:

Sub HOTParts2()

Dim rPID As Integer  
Dim rAID As Integer  
Dim dbs As DAO.Database  
Dim sqlstr1 As String  
Dim sqlstr2 As String  
Dim sqlstr3 As String  
Dim sqlstr4 As String  

Set dbs = CurrentDb  

'sqlstr1 = "INSERT INTO Assembly ( PID, ModelNum, ModelRev, ModelDescription ) " _  
'        & "SELECT (PID,Forms!HOTEntry!txtHotModel, Forms!HOTEntry!txtHotRev, Forms!HOTEntry!txtHotDes)" _
'        & "FROM Part " _
'        & "WHERE Part.PartName = Forms!HOTEntry!txtPartName AND Part.Config = Forms!HOTEntry!txtConfigEntry AND Part.Rev = Forms!HOTEntry!txtRevEntry"
sqlstr1 = "INSERT INTO Assembly ( ModelNum, ModelRev, ModelDescription,PID ) " _
        & "VALUES (Forms!HOTEntry!txtHotModel, Forms!HOTEntry!txtHotRev, Forms!HOTEntry!txtHotDes," & "rPID" & ");"
'
'sqlstr2 = "UPDATE Assembly " _
'        & "SET PID =" & rPID & " " _
'        & "WHERE Assembly.ModelNum = Forms!HOTEntry!txtHotModel And Assembly.ModelDescription = Forms!HOTEntry!txtHotDes And Assembly.ModelRev = Forms!HOTEntry!txtHotRev;"
'
'sqlstr3 = "INSERT INTO MachineOrder ( AID, Serial, CustName ) " _
'        & "SELECT (AID,Forms!HOTEntry!txtHotSerial, Forms!HOTEntry!txtHotCust)" _
'        & "FROM Assembly" _
'        & "WHERE Assembly.Model=Forms!HOTEntry!txtHotModel And ModelDescription= Forms!HOTEntry!txtHotDes And ModelRev = Forms!HOTEntry!txtHotRev; "
sqlstr3 = "INSERT INTO MachineOrder (Serial, CustName, AID ) " _
        & "VALUES (Forms!HOTEntry!txtHotSerial, Forms!HOTEntry!txtHotCust," & "rAID" & ");"
'
'sqlstr4 = "UPDATE MachineOrder " _
'        & "SET AID =" & rAID & " " _
'        & "WHERE AID IS NULL;"

rPID = DLookup("PID", "Part", "PartName = " & "'" & Forms!HOTEntry!txtPartName & "'" & " And " & "Config = " & "'" & Forms!HOTEntry!txtConfigEntry & "'" & " And " & "Rev = " & "'" & Forms!HOTEntry!txtRevEntry & "'")

DoCmd.RunSQL sqlstr1
'DoCmd.RunSQL sqlstr2

rAID = DLookup("AID", "Assembly", "ModelNum = " & "'" & Forms!HOTEntry!txtHotModel & "'" & " And " & "ModelDescription = " & "'" & Forms!HOTEntry!txtHotDes & "'" & " And " & "ModelRev = " & "'" & Forms!HOTEntry!txtHotRev & "'")

DoCmd.RunSQL sqlstr3
'DoCmd.RunSQL sqlstr4


End Sub

嗯,如果你想在查询中使用查找到的 rPID 和 rAID,你需要做的不仅仅是在 VBA 中设置它们。您可以在 SQL 语句中手动填写它们,使用参数和 QueryDef 并在 QueryDef 中填写参数,或者将 DLookUp 放入 SQL 语句中。 在这里使用第一种方法,在您的初始语句中只使用不带引号的 rPID,并将其放在设置 rPID 之后。:

rPID = DLookup("PID", "Part", "PartName = " & "'" & Forms!HOTEntry!txtPartName & "'" & " And " & "Config = " & "'" & Forms!HOTEntry!txtConfigEntry & "'" & " And " & "Rev = " & "'" & Forms!HOTEntry!txtRevEntry & "'")
sqlstr1 = "INSERT INTO Assembly ( ModelNum, ModelRev, ModelDescription,PID ) " _
    & "VALUES (Forms!HOTEntry!txtHotModel, Forms!HOTEntry!txtHotRev, Forms!HOTEntry!txtHotDes," & rPID & ");"
DoCmd.RunSQL sqlstr1