#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Access Key

I asked last week about access keys and I need to ask a bit more.

Sub SetupF2()
Application.OnKey "{F2}", "OpenForm"
End Sub

Sub OpenForm()
UserForm1.Show
End Sub


This was the code I was given but it does not seem to work with userform. I
placed the code in the userform activate macro, and when I press f2 it does
not do a thing.

Is it possible to use the function keys from a userform?

Thanks
Greg

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Access Key

Same problem I cant trigger it from the userform. I start the program and
it loads the MAIN userform. It is on this userform I want to have the
ability to use short cut keys.

Sorry I wasnt clear.

Greg

"Office_Novice" wrote in message
...
Put This in The 'ThisWorkbook Module:

Sub SetupF2()
Application.OnKey "^{F2}", "OpenForm" 'I added the Ctrl key so Ctrl +F2
End Sub

in a standard module put:

Function OpenForm()
UserForm1.Show
End Function

HTH

"Greg B" <browgregory"nospam" wrote:

I asked last week about access keys and I need to ask a bit more.

Sub SetupF2()
Application.OnKey "{F2}", "OpenForm"
End Sub

Sub OpenForm()
UserForm1.Show
End Sub


This was the code I was given but it does not seem to work with userform.
I
placed the code in the userform activate macro, and when I press f2 it
does
not do a thing.

Is it possible to use the function keys from a userform?

Thanks
Greg


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 245
Default Access Key

Put This in The 'ThisWorkbook Module:

Sub SetupF2()
Application.OnKey "^{F2}", "OpenForm" 'I added the Ctrl key so Ctrl +F2
End Sub

in a standard module put:

Function OpenForm()
UserForm1.Show
End Function

HTH

"Greg B" <browgregory"nospam" wrote:

I asked last week about access keys and I need to ask a bit more.

Sub SetupF2()
Application.OnKey "{F2}", "OpenForm"
End Sub

Sub OpenForm()
UserForm1.Show
End Sub


This was the code I was given but it does not seem to work with userform. I
placed the code in the userform activate macro, and when I press f2 it does
not do a thing.

Is it possible to use the function keys from a userform?

Thanks
Greg


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 214
Default Access Key

Hi Greg,
To do this, you must subclass then "Main" userform and use API functions as
follows :

For example, in a standard module:
Option Explicit
Declare Function SetWindowLong& Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd&, ByVal nIndex&, ByVal wNewWord&)
Declare Function RegisterHotKey& Lib "user32" (ByVal hWnd& _
, ByVal id&, ByVal fsModifiers&, ByVal vk&)
Declare Function UnregisterHotKey& Lib "user32" (ByVal hWnd&, ByVal id&)
Declare Function CallWindowProc& Lib "user32.dll" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc&, ByVal hWnd&, ByVal Msg&, ByVal wParam&, ByVal
lParam&)

Public OldWinProc&
Public Const HOTKEY_ID& = 0
Public Const GWL_WNDPROC = (-4)

Function NewWinProc&(ByVal hWnd&, ByVal Msg&, ByVal wParam&, ByVal lParam&)
If Msg = &H82 Then ' WM_NCDESTROY
UnregisterHotKey hWnd, HOTKEY_ID
SetWindowLong hWnd, GWL_WNDPROC, OldWinProc
ElseIf Msg = &H312 Then ' WM_HOTKEY
Call Main.HotkeyExecute
End If
NewWinProc = CallWindowProc(OldWinProc, hWnd, Msg, wParam, lParam)
End Function

Sub Test()
Main.Show
End Sub

In the Main module:
Option Explicit
Private Declare Function FindWindow& Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$)

Private Sub UserForm_Initialize()
Dim hWnd&: hWnd = FindWindow(vbNullString, Me.Caption)
' Register the hotkey.
If RegisterHotKey(hWnd, HOTKEY_ID, 0&, vbKeyF2) Then
' Subclass the UserForm to watch for WM_HOTKEY messages.
OldWinProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf NewWinProc)
End If
End Sub

Sub HotkeyExecute()
Me.Hide
Sales.Show
Me.Show
End Sub

Regards,
MP

"Greg B" a écrit dans le message de
...
I asked last week about access keys and I need to ask a bit more.

Sub SetupF2()
Application.OnKey "{F2}", "OpenForm"
End Sub

Sub OpenForm()
UserForm1.Show
End Sub


This was the code I was given but it does not seem to work with userform.
I placed the code in the userform activate macro, and when I press f2 it
does not do a thing.

Is it possible to use the function keys from a userform?

Thanks
Greg


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trust Access to Visual Basic Project - Access to Excel and back tcb Excel Programming 0 January 8th 08 02:43 AM
How do I access the access data via Excel 2002 with auto update ? karthik Excel Programming 1 February 9th 07 01:56 PM
Importing data from Access to Excel, but I need to vary the table from Access Liz L. Excel Programming 3 June 6th 06 02:12 AM
Access.Application.Quit - opens Access again then won't close - He Hluhluwe Excel Programming 0 January 30th 06 11:17 AM
Getting Access Error Messages when running Access through Excel Dkline[_2_] Excel Programming 0 October 12th 04 09:35 PM


All times are GMT +1. The time now is 02:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"