2

I want to run a for-loop from sometime to some specific time. Let's say from the first day of the year to the last day:

I am given the year and I need to add the month and the day to it:

I am trying to concatenate into a full date string and the compiler keeps throwing errors:

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

any help is appreciated. or any hint is welcome

1
  • Can you edit your question with the error message? Commented Jun 15, 2015 at 12:41

2 Answers 2

5

DateSerial should make this easier.

Give it values for year, month, and day in that order, and it will give you back the corresponding date as a Date/Time value.

for dt = DateSerial(Year(Date), 1, 1) to DateSerial(rs!dan, 2, 2)
    msgbox dt
Next
Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome. I was uncertain what you had in mind when you said last day of year. That made me think DateSerial(rs!dan, 12, 31) But I decided to just translate what you already had to DateSerial
No problem. I usually don't ask the exact thing i am looking for, I look for similar stuff so I can figure the rest out. If I ask the exact thing I will get lazy :D
1

Nevermind, I found a solution quite fast: This works fine

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

2 Comments

No, don't go the string route, use DateSerial.
ok. as you can see above, i accepted the answer with dateserial not mine. and the string route is a possibility even if it is the one that is more efficient @Gustav

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.