Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 如何在vba中使用字符串创建日期?_Ms Access_Vba_Ms Access 2003 - Fatal编程技术网

Ms access 如何在vba中使用字符串创建日期?

Ms access 如何在vba中使用字符串创建日期?,ms-access,vba,ms-access-2003,Ms Access,Vba,Ms Access 2003,我想从某个时间到某个特定时间运行for循环。假设从一年的第一天到最后一天: 给我一年,我需要加上月份和日期: 我试图连接成完整的日期字符串,但编译器不断抛出错误: dim dt as Date dim rs as recordset set rs = currentdb.openRecordset("select max(date) as dan from Atbl") for dt = #1/1/year(now)# to #2/2/Year(rs!dan)# msgbox dt

我想从某个时间到某个特定时间运行
for循环。假设从一年的第一天到最后一天:

给我一年,我需要加上月份和日期:

我试图连接成完整的日期字符串,但编译器不断抛出错误:

dim dt as Date 
dim rs as recordset 
set rs = currentdb.openRecordset("select max(date) as dan from Atbl")

for dt = #1/1/year(now)# to #2/2/Year(rs!dan)#
    msgbox dt
Next

感谢您的帮助。或者任何提示都是受欢迎的

不管怎样,我很快找到了一个解决方案:这个很好用

dim dt as Date 
dim rs as recordset 
set rs = currentdb.openRecordset("select max(date) as dan from Atbl")

'save the first day as string first
Dim firstDay As String
firstDay = "1/1/" & Year(Now)
'convert to date
Dim firstDate As Date
firstDate = CDate(firstDay)

'save as string
Dim lastDay as string 
lastDay = "2/2/" & Year(rs!dan)

'convert to date
Dim lastDate As Date
lastDate = CDate(lastDay)

'works fine in here
for dt = firstDate to lastDate
    msgbox dt
next
这应该会更容易

按该顺序为它指定年、月和日的值,它将返回相应的日期作为日期/时间值

对于dt=DateSerial(年份(日期),1,1)到DateSerial(rs!dan,2,2)
msgbox dt
下一个

您能用错误信息编辑您的问题吗?不客气。当你说今年的最后一天时,我不确定你在想什么。这让我想到了
DateSerial(rs!dan,12,31)
,但我决定把你已经掌握的东西翻译成
DateSerial
没问题。我通常不会问我要找的东西是什么,我会找类似的东西,这样我就可以找出剩下的。如果我问确切的问题,我会变得懒惰:DNo,不要走字符串路线,使用DateSerial.ok。正如你在上面看到的,我接受了dateserial而不是我的答案。而字符串路由是一种可能性,即使它是一个更有效的@Gustav