如何在MS Access VBA中改进我的SQL查询

时间:2013-09-23 04:34:15

标签: vba ms-access access-vba ms-access-2007 subquery

如何改进我的查询,以便只选择那些在Pasenger_Detail表中尚未选择的记录。

Private Sub BR_ID_LostFocus()
   Dim s As String

   s = "Select seat_no.seat_no" & _
       " FROM Seat_No" & _
       " Where seat_no.seat_no <=  " & _
       " (select br_info.Seats_Reserved from br_info where "  & _
       " Br_info.br_id=forms!pasenger_detail!br_id);"

  Me.Seat_No.RowSource = s
  Me.Seat_No.Requery
End Sub

1 个答案:

答案 0 :(得分:2)

您可以在WHERE子句中使用Not In,如下所示。如果它在子查询中找不到座位,那么它将在结果中列出seat_no。

SELECT Seat_No.seat_no 
FROM Seat_No 
WHERE Seat_No.seat_no <= 
(
  SELECT br_info.Seats_Reserved 
  FROM br_info 
  WHERE br_info.br_id = forms!pasenger_detail!br_id
) 
AND Seat_No.seat_no NOT IN 
(
  SELECT pasenger_detail.seat_no 
  FROM pasenger_detail
)