Command Buttons Linked to [Date] Filed

Anonymous
2021-05-22T09:01:01+00:00

Hi,

I have [Date] Filed and I want to link it to the following command buttons as follows:

What is the code that let me add or remove dates?

> add one day code?

< remove one day code?

==================

How can I link those to Date Filed?

Today button code Date()

Tomorrow button code Date()+1

Yesterday button code  Date()-1

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2021-05-25T09:43:11+00:00

kindly, show me in VBA I will try to converted to Macro

Hi Sarah,

This is the code. I hope you can transform it.

The nice thing is that this routine is standard available in any of my applications.

As I work quite a lot with incomplete dates (e.g. ??-feb-1911"  or "after 1920"), it works also for these kind of "pseudo" date-controls.

Make_datum is a function that displays the date in my favourite format.

Sub Set_datum(datum_ctl As Control, cur_key As Integer, cur_shift As Integer)

Dim tmp_datum As Date

Dim incr

If (datum_ctl & "" = "") Then

Select Case Chr(cur\_key) 

Case ".", "&gt;", ",", "&lt;": tmp\_datum = Date: incr = 0 

End Select 

ElseIf (IsDate(datum_ctl)) Then

tmp\_datum = datum\_ctl & "" 

Select Case Chr(cur\_key) 

Case ".", "&gt;": incr = 1 

Case ",", "&lt;": incr = -1 

End Select 

End If

If (Not IsEmpty(incr)) Then

cur\_key = 0 

If (cur\_shift = 1) Then 

  tmp\_datum = DateAdd("m", incr, tmp\_datum) 

Else 

  tmp\_datum = tmp\_datum + incr 

End If 

datum\_ctl = Make\_datum(tmp\_datum) 

datum\_ctl.SelStart = 0 

End If

End Sub

Imb.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points MVP Volunteer Moderator
2021-05-22T10:30:31+00:00

Open the form in design view.

Select the cmdNext button.

Activate the Event tab of the Property Sheet.

Click in the On Click box, then click the builder dots ... to the right of the dropdown arrow.

Select Macro Builder, then click OK.

On the Design tab of the ribbn, click to highlight the Show All Actions button. (This is only possible if the database is in a trusted location or is a trusted document).

First, add the If action.

Enter   Not IsNull([txtDate])    in the box next to If.

In the box below it, add the SetValue action. This is only available if you have turned on Show All Actions.

In the Item box, enter    [txtDate]

In the Expression box, enter    [txtDate]+1

Click Close and confirm that you save the changes.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points MVP Volunteer Moderator
2021-05-22T09:21:12+00:00

Let's say that the text box is named txtDate, and the command buttons are named cmdPrevious, cmdNext, cmdYesterday, cmdToday and cmdTomorrow. The event procedures for the command buttons could look as follows:

Private Sub cmdNext_Click()
    If Not IsNull(Me.txtDate) Then
        Me.txtDate = Me.txtDate + 1
    End If
End Sub

Private Sub cmdPrevious_Click()
    If Not IsNull(Me.txtDate) Then
        Me.txtDate = Me.txtDate - 1
    End If
End Sub

Private Sub cmdToday_Click()
    Me.txtDate = Date
End Sub

Private Sub cmdTomorrow_Click()
    Me.txtDate = Date + 1
End Sub

Private Sub cmdYesterday_Click()
    Me.txtDate = Date - 1
End Sub

(Please check your spelling)

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-05-22T14:14:33+00:00

    Thank you so much! indeed

    I'm curious.

    Why didn't you just use SetValue only?

    I want to learn why you are using If function with Not isNull?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-05-22T09:37:18+00:00

    Hi, Hans

    Thanks!

    Kindly, can you show me how to do it by Macro

    Show me only the first one

    Plz indeed

    Was this answer helpful?

    0 comments No comments