Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SendKeys Replacement

Status
Not open for further replies.

KevinJohnston

Technical User
Feb 11, 2011
4
US
I'm trying to update my database from 32-bit to 64-bit. SendKeys doesn't work any more, and I'm trying to replace SendKeys code with 64-bit friendly code (I've got about 32 instances of the SendKeys statement). For several instances, I've been able just to use Control.SetFocus statements to replace SendKeys Tab statements.

My problem comes when I want to go to the next field (Title) based on the key the user enters in the first field. For example, I want to move to the next field whenever the user presses "m" or "M". I've tried this with KeyUp, KeyDown and KeyPress and it doesn't work.

Here's the code associated with the KeyUp event for the first field with remarks.

Note that Me.Title.SetFocus works if I don't have it within Select Case statement, but doesn't if I do.

--------------

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

On Error GoTo Err_ISBN_KeyUp

'Me.Title.SetFocus 'works here if I type any key

Select Case KeyAscii
Case 77, 109 'Capital or lower case "m"
'SendKeys "{Tab}" 'Works in 32-bit only
Me.Title.SetFocus 'Doesn't work here
Exit Sub
'Case Next, etc.
End Select

Exit_ISBN_KeyUp:
Exit Sub

Err_ISBN_KeyUp:
MsgBox Error$
Resume Exit_ISBN_KeyUp
End Sub

Hope the above is clear and someone can give me some guidance.

Thanks very much.
 
look at the parameters
(KeyCode As Integer, Shift As Integer)

Now look at your select case
Select Case KeyAscii

There is no keyascii parameter.

If you use Option Explicit you would have found the error.

You really should never use sendkeys, unless you absolutely have to, to work with applications that do not support automation. There is never a good reason I can think of to use internally within access.
 
How are ya KevinJohnston . . .

[blue]KeyAscii[/blue] is used in the [blue]On Key Press[/blue] event. You could simply move your code there. However ... I agree with [blue]MajP[/blue], SendKeys is a bad idea.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
MajP and TheAceMan1,

Thanks for your replies. First, I apparently wasn't clear that I was trying to GET RID of the sendkeys; I was just trying to find out how to do so. I included the old code (notated with ') just how the code used to be.

Your suggestions to use Option Explicit and to use the OnKeyPress event by moving the code there was helpful and a step in the right direction. I am still having some problems, but I won't ask you for help on them right now. I'm having enough problems that I may just go back to Windows 7 32-bit version on my PC. I'm thinking it over.

Thanks.

 
KevinJohnston . . .

Not trying to sound this way or that, but I don't see the point in redirecting [blue]Tab[/blue] to the letter [blue]M[/blue] [surprise] ...

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Sorry for the delay in responding. The reason I was directing "M" to a tab was the following: This is for a homegrown point of entry bookstore database. Most books we sell we scan ISBN numbers when making the sale. Some items do not have ISBN numbers and we just sell these under some miscellaneous codes for categorization. When the person making the sale types in "M" we want to go to the next field to bring up a miscellaneous item form.

Oh well, I just downgraded back to the 32-bit version of Office and of the database since there is no driving need to upgrade to 64-bit at this time (and may never be). I had numerous other issues as well with the upgrade which just doesn't make sense to upgrade at this time.

Thanks you both for your time and replies anyway!

 
Everyone here is a code guy (gal?) and I'm not. Therefore you are all looking through the VBA lens.

I suck at code, but I've written dozens of very sophisticated databases using nothing but macros, and therefore I'm looking at the problem through the macro lens. To me, it is simple to solve. Just use a macro attached to the BeforeUpdate event.

In the conditions, put

[MyControl] Like "*M" Or [MyControl] Like "*m"

It will disregard any previous characters. Any time either case is struck it will be seen as true and the following instructions should be used for the Action. In fact, I didn't bother to try but it may not be case sensitive. Only one "m" may be required.

RunCommand, Undo (Removes the character you just typed in [MyControl])

Use elipses (...) under the condition statement, and in the second line Action enter

SendKeys {Tab}

That's it. The macro will only be run if an M is typed, and before the update is made the character is removed and you tab to the next field.

I know there are reasons given never to use the SendKeys feature, but I believe Microsoft would eliminate it if it were really as bad as some believe.

Regardless, this is a two-minute, two-line guaranteed solution to your problem. If you have no great concerns about conflict in your database with a once-in-a-million erroneous {Tab} key send, then go with it.

Have fun
 
How are ya MacroScope . . .
KevinJohnston said:
[blue]I'm trying to update my database from 32-bit to 64-bit. SendKeys doesn't work any more, ...[/blue]
Indicating that [blue]SendKeys[/blue] doesn't work in 64-bit or [blue]SendKeys[/blue] is the problem ... not the code.
His code would work if it were not for this. Your macro includes [blue]SendKeys[/blue], which is what he's trying to fix!


See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
KevinJohnston . . .

The [blue]On Key Press[/blue] event doesn't detect non-printable keys, in particular Shift, Alt, Ctrl. Neither can they be manipulated. The next best event is the [blue]On Key Down[/blue] event. Although it detects Shift, Alt, Ctrl with the [blue]Shift[/blue] arguement, it has the same problem that it can't manipulate these keys. However it can manipulate the key returned thru the [blue]KeyCode[/blue] arguement. All this means is when the user hits [blue]Shift+M[/blue] we can only convert to [blue]Shift+TAB[/blue]. Converting M to tab is no problem. In the [blue]On Key Down[/blue] event of the control, the code for this would be:
Code:
[blue]   If KeyCode = vbKeyM And Shift < 2 Then KeyCode = vbKeyTab[/blue]
... and you can get rid of [blue]SendKeys[/blue].

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
AceMan and MacroScope,

Thanks to both of you for your contributions! They both seem to work in 32-bit Access, and assumedly they will in 64-bit as well. I will keep these suggestions handy in case I actually do upgrade back up to 64-bit - they will be useful if and when that happens.

I'm content for now - thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top