在MS-Access 2019中,我想像这样从tblLink
中检索可容纳3位客人的房间数:
Dim rs As ADODB.Recordset, sSQL As String
sSQL = "SELECT Count(id) FROM tblLink WHERE bID=1 "
sSQL = sSQL & "GROUP BY rID HAVING Count(id)=3"
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = conn ' globally defined
.CursorLocation = adUseClient
.LockType = adLockReadOnly
.Source = sSQL
.Open <---- Runtime Error
End With
...导致
运行时错误3265:在此集合中找不到项目
将SQL粘贴到查询中时,将获得所需的结果。
我试图通过精简SQL来找出错误,但没有成功。我认为是COUNT
引起了麻烦。
如何使用ADODB.Recordset
在VBA中完成这项工作?
修改
tblLink
:
id bID rID
-----------------
1 3 2
2 3 2
3 3 2
4 3 5
5 3 5
6 2 8
bID
:预订ID,rID
:房间ID
预订3
有2个房间,房间2
有3人,房间5
有2人。
修改
tblLink
链接到tblRoom
和tblBooking
...
tblRoom tblBooking
id name type etc. id start end status
long string string long date date integer
---------------------- ---------------------------------
2 A4 3B 3 1/28/2020 1/30/2020 1
5 B1 2B 2 1/24/2020 1/28/2020 1
8 B4 1B
tblRoom.id ---- tblLink.rID
tblLink.bID ---- tblBooking.id
修改
conn
是在全局模块中定义的,并且运行良好:
Dim m_conn As ADODB.Connection
Public Function conn() As ADODB.Connection
If m_conn Is Nothing Then Set m_conn = CurrentProject.AccessConnection
Set conn = m_conn
End Function