How to insert date at caret position in text control.

G

Guest

In the Form's KeyDown event I test for say 'Alt D' thus:

blnAlt = (Shift And acAltMask)

then if TRUE I perform

If KeyCode = vbKeyD Then SendKeys strDate, True

where strDate contains a date.

However nothing happens. What am I missing?
 
G

Guest

Hi, Tom.

Try:

Private Sub txtSomeValue_KeyDown(KeyCode As Integer, Shift As Integer)

On Error GoTo ErrHandler

Dim sDate As String
Dim nAltKey As Integer

sDate = "10/31/2005"
nAltKey = (Shift And acAltMask) > 0

If (nAltKey And (Chr(KeyCode) = "d")) Then
SendKeys sDate, False
End If

Exit Sub

ErrHandler:

MsgBox "Error in txtSomeValue_KeyDown( ) in " & _
vbCrLf & Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear

End Sub

.. . . where txtSomeValue is the name of the text box needing the date
inserted at the caret whenever <ALT><D> is pressed on the keyboard.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
G

Guest

Same problem with your code as with mine.

The problem is that if 'sDate' contains anything other than alpha characters
then what is sent is garbage.

eg sDate = "123" and I get garbage.
sDate = "abc" then works fine
sDate = "01/01/2005" then garbage
sDate = "abc-xyz" then garbage

Any ideas.
 
G

Guest

Same problem with your code as with mine.

Your code made "nothing" happen. My code made garbage. ;-)

For each of the four examples given, what did the garbage string look like?
And was there an error message?

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
G

Guest

Sorry, I initially should have said my code made garbage.

OK

sDate = "abc-xyz" makes it repeat endlessly until stack space runs out.

Others are too hard to explain.
 
G

Guest

Hi, Thomas.

Do you have any hotkeys defined for the "D" character, such as in the
commandbars or on the form that is currently open? Do you have any tables,
queries, or fields in the form's data source, or controls on the form using
names that are Reserved words, and which your code is also using (such as
Date)?

What happens if you change the character to something other than "D"? How
about testing with an uncommon character such as "Z" instead? Does this
still produce the same garbage?

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
G

Guest

OK I know what's causing it but not the solution.

I have other shortcuts set up.
eg Alt+1 will SENDKEYS of "Mary had a little lamb."

so if
sDate = "123" then SENDKEYS will push out:
"Mary had a little lamb.23"

The '1' in sDate causes "Mary..." because the ALT key is down when sDate is
being pushed out.

Um...get it?
 
G

Guest

Hi, Tom.
Um...get it?

Yup. That's why I asked you what the garbage string looked like, because
that's one of the things I suspected.

You can use a different key combination that does not use the <ALT> key for
the date insertion, or you can get rid of the other shortcuts that are
interfering, or you can use the form timer to delay the insert of the date
string until a time after the <ALT> key is no longer being pressed.

This last choice is problematic and will likely cause occasional annoyances
when stepping through code in the VB Editor if the form is open at the same
time, or when closing the application, or when you can least afford to have
those SendKeys activated.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
G

Guest

Oh...I understand and thank you very much for your help.

Rhetorical statement - You'd think then there would be a workaround possibly
without using SENDKEYS. In general SENDKEYS seems to be a zero effective
solution for most objectives. At least that's my take on it after thinking
more about it.
 
6

'69 Camaro

You're welcome. The work-around likely entails a lengthy algorithm using
the Windows API's, which is why it's much easier to just use SendKeys.
Since one can't control when the SendKeys command is going to be executed,
it should be avoided unless there's no alternative.

Good luck.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.
 
T

Tim Ferguson

Rhetorical statement - You'd think then there would be a workaround
possibly without using SENDKEYS. In general SENDKEYS seems to be a
zero effective solution for most objectives. At least that's my take
on it after thinking more about it.

It's not just rhetorical: it's a statement of bald truth. There is
generally no decent reason to use Sendkeys. It's prone to all kinds of
error, not least the user simply swapping to a new application and your
keystrokes ending up in completely the same place. If Sendkeys is your
best solution, then you need a new problem.

As for this one:
If KeyCode = vbKeyD Then SendKeys strDate, True

why not simply

Screen.ActiveControl.SelText = strDate

or, if you believe in defensive programming

With Screen.ActiveControl
If .controltype = acTextBox or .controltype = acComboBox Then
.SelText = strDate
End If
End with


Hope that helps


Tim F
 
6

'69 Camaro

Hi, Tim.
why not simply

Screen.ActiveControl.SelText = strDate

Because in the text box's OnKeyDown( ) event (where one wants to test which
key has been typed), this produces -- in Tom's own words -- garbage.
Depending upon how long the <ALT> key was pressed before the <D> key was
pressed, the buffer could repeat this value until one runs out of stack
space -- exactly one of the problems Tom complained about.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.
 
T

Tim Ferguson

Depending upon how long the <ALT> key was pressed before the <D> key
was pressed, the buffer could repeat this value until one runs out of
stack

In that case it's just a problem of unbouncing the ALT key:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

' remember the last time round this event
Static isUsingAlt As Boolean

' avoid re-iteration if it's simply a auto-repeat
If (Shift And acAltMask) And isUsingAlt Then Exit Sub

' remember the new current status
isUsingAlt = CBool(Shift And acAltMask)

' and now process the keystroke if you still want to
If (Shift And acAltMask) And (KeyCode = vbKeyD) Then
If TypeOf etc etc etc


Obviously, this is air code and not tested. A few more random thoughts:

ALT- combinations typically cause menu commands, occasionally
button-commands, or else move focus. Alt+ctrl combinations are
meant for desktop-level shortcuts. Developers who try to redo
Windows standards deserve all the difficulties they encounter.
A Ctrl- combination is much more user-friendly for this kind
of functionality.

Isn't it more common to use the KeyUp event for this kind of
key trapping anyway? Can't remember the source, but it's in a
UI bible somewhere...

All the best


Tim F
 

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