Default value using dateserial

B

bd

I have a form with a textbox. The format of the textbox is set to mm//dd/yyyy.
In the form's module, I want to set the default value of the textbox using the dateserial function:

Private Sub Form_Open(Cancel As Integer)
Me!Textbox.DefaultValue = DateSerial(Year(Now), Month(Now), Day(Now))
End Sub

This returns 12/30/1899 as the value in the text box. Does anyone see what the problem may be?
PS The reason I use the dateserial function here is that this is a simplification of my code, I will be using the dateserial to manipulate the date in the final code.
Thanks!
 
A

Allen Browne

DefaultValue is a string property, you need to assign it a string like this:
"=#02/14/2004#"

Private Sub Form_Open(Cancel As Integer)
Dim dt As Date
dt = Date
Me.Textbox.DefaultValue = Format(dt, "\=\#mm\/dd\/yyyy\#")
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

bd said:
I have a form with a textbox. The format of the textbox is set to mm//dd/yyyy.
In the form's module, I want to set the default value of the textbox using the dateserial function:

Private Sub Form_Open(Cancel As Integer)
Me!Textbox.DefaultValue = DateSerial(Year(Now), Month(Now), Day(Now))
End Sub

This returns 12/30/1899 as the value in the text box. Does anyone see what the problem may be?
PS The reason I use the dateserial function here is that this is a
simplification of my code, I will be using the dateserial to manipulate the
date in the final code.
 

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