Allow users to set default date

  • Thread starter Thread starter larry
  • Start date Start date
L

larry

I want to give my users an option to set a default date without allowing
them to go to the properties of the control.
Using the following seems to work, but I wonder if it'll cause problems
using a string (from the inputbox) in a date field. Will access make the
conversion automatically?

Private Sub cmdSetDefaultDate_Click()

Dim strDate As String

strDate = InputBox("Enter default date", "Set Date")
Me![myDate] = strDate

End Sub

Also, the command button cmdSetDefaultDate will be on a main form & the
field myDate is on it's subform. Would I still refer to the control with
Me! or does it need to be further identified? What's the proper way to
refer to controls in on a subform vs. a form?

Thanks :)
 
1. Test that the user's input can be interpreted as a date.
2. Add quote marks to the string: default value is a string.
3. Include the name of your subform control.

Result:

Private Sub cmdSetDefaultDate_Click()
Dim strDate As String

strDate = InputBox("Enter default date", "Set Date")
If IsDate(strDate) then
Me.[NameOfYourSubformControlHere].Form![myDate].DefaultValue = """"
& strDate & """"
Else
MsgBox "No valid date entered."
End If
End Sub


If the ".Form" bit is new, see:
Referring to Controls on a Subform
at:
http://allenbrowne.com/casu-04.html
 
Thanks Allen!


Allen Browne said:
1. Test that the user's input can be interpreted as a date.
2. Add quote marks to the string: default value is a string.
3. Include the name of your subform control.

Result:

Private Sub cmdSetDefaultDate_Click()
Dim strDate As String

strDate = InputBox("Enter default date", "Set Date")
If IsDate(strDate) then
Me.[NameOfYourSubformControlHere].Form![myDate].DefaultValue = """"
& strDate & """"
Else
MsgBox "No valid date entered."
End If
End Sub


If the ".Form" bit is new, see:
Referring to Controls on a Subform
at:
http://allenbrowne.com/casu-04.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

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

larry said:
I want to give my users an option to set a default date without allowing
them to go to the properties of the control.
Using the following seems to work, but I wonder if it'll cause problems
using a string (from the inputbox) in a date field. Will access make the
conversion automatically?

Private Sub cmdSetDefaultDate_Click()

Dim strDate As String

strDate = InputBox("Enter default date", "Set Date")
Me![myDate] = strDate

End Sub

Also, the command button cmdSetDefaultDate will be on a main form & the
field myDate is on it's subform. Would I still refer to the control with
Me! or does it need to be further identified? What's the proper way to
refer to controls in on a subform vs. a form?

Thanks :)
 
Back
Top