我有一个名为 TimeQuery 的查询。我希望MsgBox会显示所有 [start_hour] 值,其中 [id] = 1然后2等... 我有个问题。当我将使用:
a = DLookup("Hour([start_hour])", "TimeQuery", "[id]=1")
效果很好,但是当使用 [id] =计数器时,它不显示它。在For循环之后我有一个MsgBox,当 [id] =计数器时,它也没有显示MsgBox。怎么了?
For counter = 1 To 3
Dim a As Variant
a = DLookup("Hour([start_hour])", "TimeQuery", "[id]=counter")
MsgBox (counter)
Next
Debug.Print ("")
答案 0 :(得分:2)
如果要使用此变量,则需要将变量连接到字符串,如下所示:
For counter = 1 To 3
Dim a As Variant
a = DLookup("Hour([start_hour])", "TimeQuery", "[id]=" & counter)
MsgBox (counter)
Next
Debug.Print ("")
但是,如果要正确执行此操作,请使用记录集
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset ("SELECT Hour([start_hour]) As hr FROM TimeQuery WHERE [id] <=3 ORDER BY ID ASC")
Do While Not rs.EOF
MsgBox rs!hr
rs.MoveNext
Loop