1

我有一个按钮,它使用两个单独的查询从两个表中提取数据并插入到另一个表中:

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 & ")"

有人知道我可能会出错吗?

谢谢 :-)

4

2 回答 2

1

在这种情况下,您只是插入键值,所以您需要做的就是

strSQL1 = _
        "INSERT INTO Members_Callouts (MemberID, CalloutID) " & _
            "VALUES (" & lngID & ", " & lngIDCallout & ")"

换句话说,您无需为诸如...之类的事情烦恼

"(SELECT MemberID FROM Members WHERE MemberID=" & lngID & ")"

...因为它返回的值只是lngId(假设该值存在于 [Members] 表中)。

于 2013-04-18T06:31:06.060 回答
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

(从 Func5() 中选择 Col7)作为 f5

于 2013-04-18T06:33:42.347 回答