我有一个按钮,它使用两个单独的查询从两个表中提取数据并插入到另一个表中:
Dim lngID As Long
Dim lngIDCallout As Long
Dim strSQL1 As String
lngID = CalloutAttendance_MultiSelect.Value
lngIDCallout = Forms![Callouts].[CalloutID].Value
strSQL1 = "INSERT INTO Members_Callouts(MemberID) SELECT MemberID FROM Members WHERE MemberID=" & lngID
strSQL2 = "INSERT INTO Members_Callouts(CalloutID) SELECT CalloutID FROM Callouts WHERE CalloutID=" & lngIDCallout
CurrentDb.Execute strSQL1
CurrentDb.Execute strSQL2
CalloutAttendance_MultiSelect.Requery
虽然它几乎完成了我想要它做的事情,但是它将两个值作为两个单独的新记录插入,而我希望将它插入到一个新记录中。我有一个去,但我得到语法错误,或在下面的情况下,我得到一个3067运行时错误“查询输入必须包含至少一个表或查询”
strSQL1 = "INSERT INTO Members_Callouts(MemberID, CalloutID) SELECT
(SELECT MemberID FROM Members WHERE MemberID=" & lngID & "),
(SELECT CalloutID FROM Callouts WHERE CalloutID=" & lngIDCallout & ")"
任何人都知道我可能会出错的地方?
谢谢: - )
答案 0 :(得分:1)
在这种情况下,您只需插入键值,所以您需要做的就是
strSQL1 = _
"INSERT INTO Members_Callouts (MemberID, CalloutID) " & _
"VALUES (" & lngID & ", " & lngIDCallout & ")"
换句话说,你不需要打扰像...这样的东西。
"(SELECT MemberID FROM Members WHERE MemberID=" & lngID & ")"
...因为它返回的值只是lngId
(假设该值存在于[Members]表中)。
答案 1 :(得分:0)
INSERT INTO
MyTable (Col1,Col2,Col3,Col4,Col5,Col6,Col7)
SELECT
f1.col1, f2.col2, f3.col3, f3.col4, f3.col5, f4.col6, f5.col7
FROM
(SELECT Col1 FROM Func1()) AS f1
CROSS JOIN
(SELECT Col2 FROM Func2()) AS f2
CROSS JOIN
(SELECT Col3,Col4,Col5 FROM Func3()) AS f3
CROSS JOIN
(SELECT Col6 FROM Func4()) AS f4
CROSS JOIN
(选择Col7 FROM Func5())AS f5