Access 2003 - Setting default value of a text box

A

Amanda

I have 2 text boxes in a form that default to two set dates. I'd like to
update the form so that txtStartDate is the first date of last month and
txtEndDate is the last date of this month. For example, if I were to pull
the form up today, I'd like txtStateDate's default to be 9/1/09 and
txtEndDate's default to be 10/31/09. If I pull it up the form in November,
txtStartDate should be 10/1/09 and txtEndDate is 11/31/09.

Both text boxes are unbound. Any suggestions?
 
A

Arvin Meyer [MVP]

Amanda said:
I have 2 text boxes in a form that default to two set dates. I'd like to
update the form so that txtStartDate is the first date of last month and
txtEndDate is the last date of this month. For example, if I were to pull
the form up today, I'd like txtStateDate's default to be 9/1/09 and
txtEndDate's default to be 10/31/09. If I pull it up the form in
November,
txtStartDate should be 10/1/09 and txtEndDate is 11/31/09.

Both text boxes are unbound. Any suggestions?

Add these 2 functions to a standard module and call them from the
DefaultValue property:

Function BeginLastMonth()
BeginLastMonth = DateAdd("m", -1, DateSerial(Year(Date), Month(Date), 1))
End Function

Function LastDOM (DateInAMonth)
LastDOM = Day(DateSerial(Year(DateInAMonth), Month(DateInAMonth) + 1, 0))
End Function
 
K

Klatuu

?dateserial(year(date),month(date)-1,1)
9/1/2009
?dateserial(year(date), month(date)+1,0)
10/31/2009
 
A

Amanda

Thank you for your response! However, if I input

?dateserial(year(date),month(date)-1,1)

Access says there's an invalid operator in my expression. If I take the
question mark away, Access automatically updates the line to:

DateSerial(Year("Date"),Month("Date")-1,1)

And my text box gives me an #Error value. I've tried taking the quotes
away, but Access keeps putting 'em back.
 
A

Amanda

Thank you Arvin!

The first function did what I wanted beautifully, but as I played with the
second, I realized that you didn't need the Day() function in there. If you
just use

Function LastDOM(DateInAMonth)
LastDOM = DateSerial(Year(DateInAMonth), Month(DateInAMonth) + 1, 0)
End Function

It returns the last day of the month. Thank you both though for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top