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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sendkeys with Windows 7 4

Status
Not open for further replies.

MikeCt

Programmer
Nov 6, 2001
44
US
Hi
I've been using the following statement in my apps to trigger the tab key when the user presses the enter key.
I use this to move from textbox to textbox with the use of the enter key for data entry.

If KeyAscii = Asc(vbCrLf) Then SendKeys "{Tab}"

I'm writing a new app for a new computer running Windows 7.
When the enter key is pressed I recieve an error message
saying permission denied which exits the program. Is there another way to do this, and if possible is there a way that will work with both old & new.

Thank you
Mike
 
This has been an issue since Vista. The basic workaround is to totally disable UAC, but this may not be acceptable. Having said that, the VB runtime was updated to deal with this so the error only occurs in the IDE not in runtime. Having said that ...

...here have an article written on the subject (with potential fixes) by the excellent Karl E Peterson back in 2007 (it discusses the issue with Vista but, since Windows 7 is actually Vista with a new name, it is still relevant)

 
I'm guessing this is only happening in the IDE and that can be overcome in Vista by popping this into a module;

Public Sub Sendkeys(Text$, Optional wait As Boolean = False)

Static init As Boolean, IsIDEUnderVista As Boolean, WshShell As Object

'wrapper for Sendkeys which does not cause an Error 70 in the IDE under Windows Vista
'WshShell does not get set to Nothing before App ends but any problems related to that only happen in the IDE
'Errors due to WScript disablement on the OS can only happen under the IDE
'Extra overhead for the WshShell object is only required under the IDE

If Not init Then
If IsDevEnv() Then
IsIDEUnderVista = (OsVersion() >= 6)
If IsIDEUnderVista Then Set WshShell = CreateObject("WScript.Shell")
End If
init = True
End If

If Not IsIDEUnderVista Then
VBA.Sendkeys Text$, wait
Else
WshShell.Sendkeys Text$, wait
End If

End Sub

You'll need an IsDevEnv() function but I'll leave that to you.
 
First let me say Thanks again

This seems to be a little over my head
I added a new module to my project Module(Module1) which is the default. Then cut and pasted the sub routine you listed here into that module. I'm not sure about the
IsDevEnv() function ?

If this is to complicated ( I don't want you to waste your time ) then I understand. What can I read about that might point me in the right direction?

Will this IsDevEnv() function have to be in every form in the project?
Will it have to be in every sub routine along with the sendKeys line of code?



 
Personally I would only use SendKeys as a last resort, as you are dependent on the environment as to how any particular key press is interpreted (as you have found out).

I just did a little test with a control array of 3 textboxes, and this code works exactly as if the Tab key was pressed, but with the benefit of there being no ambiguity as to what will happen.

Code:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
    Dim NewIndex As Integer
    
    If KeyAscii <> vbKeyReturn Then Exit Sub
    
    If Index = 2 Then
        NewIndex = 0
    Else
        NewIndex = Index + 1
    End If
    
    Text1(NewIndex).SetFocus
End Sub
 
Joe
Worked great and kept things simple, could not ask for more
Thanks again
 
In a module;

Public Function IsDevEnv() As Boolean

Dim strFileName$
Dim lngCount&

'returns True when the app is being run in the IDE

strFileName = String(255, 0)
lngCount = GetModuleFileName(App.hInstance, strFileName, 255&)
strFileName = Left(strFileName, lngCount)

IsDevEnv = (UCase(Right(strFileName, 7)) Like "VB?.EXE")

End Function

 
I prefer Matt Curland's variant:

Private Function IsDevEnv() As Boolean
Dim x As Long
Debug.Assert Not TestIDE(x)
IsDevEnv = x = 1
End Function

Private Function TestIDE(x As Long) As Boolean
x = 1
End Function
 
Worked great ... but you should really be tabbing to the next Control in the TabIndex with TabStop=True and not looping through the index numbers of the current Control Array (if indeed you are in one at all). If one of the members of the Control Array were disabled or invisible setting Focus to it would give an error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top