日期之间的VBA查询问题(在Access窗体中 - Access 2010)

时间:2016-01-15 16:50:34

标签: vba access-vba

我在使用ADO测试Access VBA中的以下查询时遇到了困难:

Private Function SearchAvailability()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim StartDate As Date
Dim EndDate As Date
StartDate = Me.txtIN
EndDate = Me.txtOut
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
With rst
    Set .ActiveConnection = cnn
    .Source = "SELECT RoomCalendar.RateRoomCombinationId, RateType.RateTypeName, RoomTypes.RoomName, SUM(RoomCalendar.Rate) AS TotalSum FROM RateType INNER JOIN (RoomTypes INNER JOIN (RateRoomCombination INNER JOIN RoomCalendar ON RateRoomCombination.RateRoomCombinationId = RoomCalendar.RateRoomCombinationId) ON RoomTypes.RoomTypeId = RateRoomCombination.RoomTypeId) ON RateType.RateTypeId = RateRoomCombination.RateTypeId WHERE RoomCalendar.PricingDate Between #" & StartDate & "#  And #" & EndDate & "#-1 AND RoomTypes.MaxOccupancy=" & cboAdults & " AND  RoomCalendar.RateRoomCombinationId NOT IN (SELECT RateRoomCombinationId FROM RoomCalendar WHERE RoomCalendar.FinalAvailability< " & cboRooms & " AND RoomCalendar.PricingDate Between #" & StartDate & "#  And #" & EndDate & "#-1 AND RateRoomCombinationId IS NOT NULL )GROUP BY RoomCalendar.RateRoomCombinationId, RateType.RateTypeName, RoomTypes.RoomName"
     .LockType = adLockOptimistic
       .CursorType = adOpenKeyset
       .CursorLocation = adUseClient
       .Open
       Debug.Print .RecordCount
End With
If rst.EOF Or rst.EOF Then
Label41.Visible = True
List20.Visible = True
Else
Set Me.List20.Recordset = rst
List20.Visible = True
Label41.Visible = False
End If
End Function

以上查询仅在我将上述日期之间的调用替换为实际日期时才有效:

介于#1/1/2016#和#1/4/2016#-1

由于我从2个文本框中检索签入和签出日期,实际上我用以下内容替换了上面的内容:

在#“&amp; StartDate&amp;”#和#“&amp; EndDate&amp;”#-1 之间(它应该是正确的)

我真的无法弄清楚为什么代码不应该返回任何数据。根本没有错误,但查询错误地不会检索数据。

提前感谢您的帮助

0 个答案:

没有答案