set default value of text box using on click event button

siamandm
BronzeLounger
Posts: 1259
Joined: 01 May 2016, 09:58

set default value of text box using on click event button

Post by siamandm »

Hi

if i have a text box called txtDate and a button btnSetDate

1- i want to program the btnSetDate, when the user select a date using date picker, this day stays there for the future use and do not change until the user pick another date and click the btnSetDate

i used

Code: Select all

Private Sub btnSetDate_Click()

Me.txtDate.DefaultValue = Me.txtDate.Value
but didnt work !!

your help please

regards

User avatar
HansV
Administrator
Posts: 78613
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: set default value of text box using on click event butto

Post by HansV »

The default value must be text, so try

Code: Select all

Private Sub btnSetDate_Click()
    Me.txtDate.DefaultValue = Chr(34) & Me.txtDate & Chr(34)
End Sub
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1259
Joined: 01 May 2016, 09:58

Re: set default value of text box using on click event butto

Post by siamandm »

its not saving the date for next time when you open the form ... :(

User avatar
HansV
Administrator
Posts: 78613
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: set default value of text box using on click event butto

Post by HansV »

No, that would require opening the form in design view.

You could do the following:

- Create a new table in design view.
- Add a single field of type Date/Time and name it DefaultDate.
- Save the table as tblSettings.
- Open the table in datasheet view.
- Enter a date, for example today's date, or whatever you prefer.

- Add the following code to the On Load event of the form:

Code: Select all

    Me.txtDate.DefaultValue = Chr(34) & DLookup("DefaultDate", "tblSettings") & Chr(34)
Edit the code for the button as follows:

Code: Select all

Private Sub btnSetDate_Click()
    ' Set the new default value
    Me.txtDate.DefaultValue = Chr(34) & Me.txtDate & Chr(34)
    ' Store the default value in tblSettings
    CurrentDb.Execute "UPDATE tblSettings SET DefaultDate=# & Format(Me.txtDate, "yyyy-mm-dd") & "#", dbFailOnError
End Sub
Last edited by HansV on 31 May 2017, 11:31, edited 1 time in total.
Reason: to correct SetValue to DefaultValue
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1259
Joined: 01 May 2016, 09:58

Re: set default value of text box using on click event butto

Post by siamandm »

[quote="HansV"]No, that would require opening the form in design view.

You could do the following:

- Create a new table in design view.
- Add a single field of type Date/Time and name it DefaultDate.
- Save the table as tblSettings.
- Open the table in datasheet view.
- Enter a date, for example today's date, or whatever you prefer.

- Add the following code to the On Load event of the form:

Code: Select all

    Me.txtDate.SetValue = Chr(34) & DLookup("DefaultDate", "tblSettings") & Chr(34)
is this SetValu or DefaultValue ... as i cant fidn SetValue when i type the code.

User avatar
HansV
Administrator
Posts: 78613
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: set default value of text box using on click event butto

Post by HansV »

Sorry, I meant DefaultValue.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1259
Joined: 01 May 2016, 09:58

Re: set default value of text box using on click event butto

Post by siamandm »

thanks a lot ...
after this im getting this error :
Capture.PNG

i tried to play with the quotes "" but no success

regards
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78613
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: set default value of text box using on click event butto

Post by HansV »

Please try to understand the code instead of merely copying and pasting it. That way you can correct the mistakes in my code yourself...

Code: Select all

    CurrentDb.Execute "UPDATE tblSettings SET DefaultDate=#" & Format(Me.txtDate, "yyyy-mm-dd") & "#", dbFailOnError
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1259
Joined: 01 May 2016, 09:58

Re: set default value of text box using on click event butto

Post by siamandm »

Thanks Alot