Ms access 如何在VBA中传递DLookup中的日期变量?

Ms access 如何在VBA中传递DLookup中的日期变量?,ms-access,ms-access-2007,vba,Ms Access,Ms Access 2007,Vba,非常简单的问题。我要求用户输入日期。我使用该日期的月份来查找月份名称(5月份为5月份,6月份为6月份,等等)。我有一个名为months的ref表,带有month_id和month_名称(1-enero、2-febrero等),用西班牙语表示。我是用VBA写这篇文章的: Dim FileDate as Date Dim DateString as String DateString = InputBox("Enter the file date in MM/DD/YYYY format", "T

非常简单的问题。我要求用户输入日期。我使用该日期的月份来查找月份名称(5月份为5月份,6月份为6月份,等等)。我有一个名为months的ref表,带有month_id和month_名称(1-enero、2-febrero等),用西班牙语表示。我是用VBA写这篇文章的:

Dim FileDate as Date
Dim DateString as String

DateString = InputBox("Enter the file date in MM/DD/YYYY format", "Title")
FileDate = DateValue(DateString)
monthname = DLookup("[month_name]", "[months]", "Format(Month(FileDate),'0') = [month_id]")

但这给了我错误。如果我使用Date()而不是FileDate,则它工作正常。我是否错误地传递了变量

Microsoft Access SQL中的文字日期格式为#dd/mm/yyyy#

还有一个问题是,您发送的字符串“Format(Month(FileDate),'0')”不是变量FileDate的值。Access不知道“FileDate”是什么,只有VBA知道。因此,您应该将FileDate中的值连接到字符串

如果您有一个变量
variable=“foo”
,并且希望将其以字符串形式传递给类似DLookup的对象,那么您将编写
“string”&variable
而不是
“string variable”
。这就是DLookup获取“字符串foo”而不是“字符串变量”的原因

因此,当像使用Dlookup那样以字符串形式传递条件时,应该使用字符串连接将变量的值用两个#符号包围起来

monthname = DLookup("[month_name]", "[months]", "#" & FileDate & "# = [month_id]")
编辑 当然,我误解了你原来的问题。因为您仅使用月份作为标准,所以您需要类似以下内容:

monthname = DLookup("[month_name]", "[months]", Month(FileDate) & " = [month_id]")