基于变量访问VBA DLookup Multiple Critiera

时间:2017-10-23 23:29:17

标签: vba ms-access

我一直在寻找下面的代码而无法让它发挥作用。

Dim db As Database
Dim RecRLs As Recordset
Dim sTripCode, sVanNum As Integer
Dim sDepDate, sArrivalDate As Date
Dim sRoomRate As Currency
Set RecRLs = db.OpenRecordset("qryRLRoomListRates", dbOpenSnapshot)

sTripCode = DLookup("[TourCodeID]", "tblTripCodes", "[TourCode]=[Forms]![frmRMSBuildRLs]![tboxTourCode]")
sDepDate = [Forms]![frmRMSBuildRLs]![tboxDepartureDate]
sVanNum = [Forms]![frmRMSBuildRLs]![tboxVanNumber]
sArrivalDate = RecRLs!ArrivalDate


sRoomRate = DLookup("[RateTwinHosCab]", "tblRLRatesByTrip", "[TourCode] = " 
& sTripCode & " AND [DepartureDate] = " & sDepDate & " AND [VanNumber] = " & 
sVanNum & " AND [ArrivalDate] = " & sArrivalDate)

问题是sRoomRate返回null。 我给每个变量添加了MsgBox:sTripCode,sDepDate,sVanNum和sArrivalDate。他们每个都返回正确的结果。

为什么sRoomRate会返回null的任何想法?非常感谢你!

1 个答案:

答案 0 :(得分:3)

解决方案是在下面的日期添加#s:

sRoomRate = DLookup("[RateTwinHosCab]", "tblRLRatesByTrip", "[TourCode] = " & 
sTripCode & " AND [DepartureDate] = #" & sDepDate & "# AND [VanNumber] = " & 
sVanNum & " AND [ArrivalDate] = #" & sArrivalDate & "#")