Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 使用表单中的外键插入到表中_Ms Access_Vba_Foreign Keys_Insert Into - Fatal编程技术网

Ms access 使用表单中的外键插入到表中

Ms access 使用表单中的外键插入到表中,ms-access,vba,foreign-keys,insert-into,Ms Access,Vba,Foreign Keys,Insert Into,我正在尝试创建一个表单,用于在某些场景中手动输入数据。大多数数据是从CSV文件输入的,工作正常。我有4个表,零件,装配,机器订单,和作业。我能够从表单中编写代码来输入基表,Part,没有问题。现在的问题是将数据输入到Assembly和MachineOrder表中,其中零件由其PID自动编号字段引用,组件由其AID自动编号字段引用。我已经尝试了许多不同的方法来执行这项任务,您可以在我的注释代码中看到一些。有什么是迄今为止我认为最接近正确的代码,现在的错误是Access要求我输入rPID的参数值,尽

我正在尝试创建一个表单,用于在某些场景中手动输入数据。大多数数据是从CSV文件输入的,工作正常。我有4个表,
零件
装配
机器订单
,和
作业
。我能够从表单中编写代码来输入基表,
Part
,没有问题。现在的问题是将数据输入到
Assembly
MachineOrder
表中,其中零件由其
PID
自动编号字段引用,组件由其
AID
自动编号字段引用。我已经尝试了许多不同的方法来执行这项任务,您可以在我的注释代码中看到一些。有什么是迄今为止我认为最接近正确的代码,现在的错误是Access要求我输入
rPID
的参数值,尽管它在
Dlookup
函数中找到的值很好。我假设
rAID
部分也是如此

否则,当使用
INSERT
then
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

哇,我想我已经试过了,但我认为在设置变量后使用sqlstr语句真的很管用。非常感谢。