shortcut key for yesterday, today and tomorrow entry

G

Guest

Hi and thanks for the help. I am using Access 2003. We consistently use the
dates for yesterday, today and tomorrow. I know that I can type Ctrl+; to get
the current date. In Access is there a way to assign keystrokes to
automatically get yesterday's date and tomorrow's date? Or a button to use on
a form?
 
A

Allen Browne

You can use the KeyPress event of the control on your form to assign a value
to the field.

For example:

Select Case KeyAscii
Case vbKeyY, vbKeyY + 32
Me.[MyDate] = Date - 1
Case vbKeyT, vbKeyT + 32
Me.[MyDate] = Date + 1
End Select
 
D

Douglas J Steele

Buttons are easy: create an Event Procedure that assigns Date()-1 or
Date()+1 to the text box in question. If you want to be able to update more
than one text box on your form with the same button, you can use code like
the following:

If Screen.PreviousControl.ControlType = acTextBox Then
Screen.PreviousControl = Date - 1
End If

With this, select the text box you want updated, then click on the button.

Another approach would be to create shortcut keys by creating an AutoKeys
macro.
 
W

Wayne Morgan

Yes.

You can have a button insert and date that Access will recognize. Just place
the appropriate code in the button's Click event.

Example:
Me.txtMyTextbox = Date
Me.txtMyTextbox = Date -1
Me.txtMyTextbox = Date + 1

Create 3 buttons and use the appropriate line above for each button. Adjust
the name of the textbox to the name or your textbox.

You can also create keystrokes to do this. You can turn on KeyPreview in the
form's properties and have it check the keys you press in the form's KeyDown
event then adjust the entry. You can also use an AutoKeys macro to define
shortcut keys.

If you use the form event, you would use code similar to this:

If Me.ActiveControl.Name = "txtMyTextbox " Then
If (Shift And acCtrlMask + acShiftMask) Then
Select Case KeyCode
Case Asc("1")
Me.txtMyTextbox = Date - 1
Case Asc("2")
Me.txtMyTextbox = Date
Case Asc("3")
Me.txtMyTextbox = Date + 1
Case 16, 17
'Ignore the Ctrl and Shift keys until a 3rd key is pressed
Case Else
MsgBox "Invalid Entry"
'Cancel the key by setting the code to zero
KeyCode = 0
End Select
End If
End If
 
G

Guest

Allen ~ A bit more help is needed please. I don't know what to do with the
example you typed. Where do I put that text and how does it make my keys
work. I tried putting in the KeyPress Event but nothing happened.

Allen Browne said:
You can use the KeyPress event of the control on your form to assign a value
to the field.

For example:

Select Case KeyAscii
Case vbKeyY, vbKeyY + 32
Me.[MyDate] = Date - 1
Case vbKeyT, vbKeyT + 32
Me.[MyDate] = Date + 1
End Select

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

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

SheriTingle said:
Hi and thanks for the help. I am using Access 2003. We consistently use
the
dates for yesterday, today and tomorrow. I know that I can type Ctrl+; to
get
the current date. In Access is there a way to assign keystrokes to
automatically get yesterday's date and tomorrow's date? Or a button to use
on
a form?
 
G

Guest

Douglas, I need a bit more help here too...
Where do I create the Event Procedure and where do I put the code? I tried
making a button and attaching the Date()-1 to the button but nothing
happened. Thanks for a bit more details.

I also tried the AutoKeys macro but what I can't figure out there is where
to tell the autokey to get the info for Date()-1.

Sorry to be dense. :(
 
A

Allen Browne

1. Open the form in design view.

2. Right-click the date box, and choose Properties.

3. Set the On Key Press property (Event tab of Properties box) to:
[Event Procedure]

4. Click the Build button (...) beside the property.
Access opens the Code window.

5. Paste the lines in there.

The, pressing the Y key in your date control inserts yesterday's date, and
the T key inserts tomorrow's date.

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

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

SheriTingle said:
Allen ~ A bit more help is needed please. I don't know what to do with the
example you typed. Where do I put that text and how does it make my keys
work. I tried putting in the KeyPress Event but nothing happened.

Allen Browne said:
You can use the KeyPress event of the control on your form to assign a
value
to the field.

For example:

Select Case KeyAscii
Case vbKeyY, vbKeyY + 32
Me.[MyDate] = Date - 1
Case vbKeyT, vbKeyT + 32
Me.[MyDate] = Date + 1
End Select

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

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

SheriTingle said:
Hi and thanks for the help. I am using Access 2003. We consistently use
the
dates for yesterday, today and tomorrow. I know that I can type Ctrl+;
to
get
the current date. In Access is there a way to assign keystrokes to
automatically get yesterday's date and tomorrow's date? Or a button to
use
on
a form?
 
D

Douglas J Steele

Add a Command Button to the form, and look at the properties of the button
you just added. (if you've got the wizard icon depressed, so that the
Command Button Wizard starts when you add the button, simply click on the
Cancel button to get out of the wizard)

You'll see a list of Events associated with the control (in fact, there's a
tab in the Properties window that will let you limit what you're seeing to
only the events.

Find the event labelled On Click. The white box to the right of that is
actually a combo box: select [Event Procedure] from it, then click on the
ellipsis (...) to the right. You'll get taken into the VB Editor, with
something like the following added for you:

Private Sub Command1_Click()

End Sub

Simply type those 3 lines of code I showed between those two lines of text,
so you end up with something like:

Private Sub Command1_Click()

If Screen.PreviousControl.ControlType = acTextBox Then
Screen.PreviousControl = Date - 1
End If

End Sub

That's it. You're done. Save the form, and test that it does what you want.

In terms of an AutoKeys macro, make sure you have the Macro Name column
visible. Put whatever key combination you want to use as your shortcut in
the Macro Name column (say ^y for yesterday and ^t for tomorrow), and select
SetValue as the Action. In the dialog at the bottom, put
[Screen].[ActiveControl] as the Item, and Date() - 1 or Date() + 1 as the
expression. Name the macro AutoKeys when you save it, and you're done.
 

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