Key Press

K

K

How do I run the following code when the user press's the F1 Key? I
appreciate the help. Kyle.

Dim LResponse As Integer

LResponse = MsgBox("Do you wish to continue?", vbYesNo, "Continue")

If LResponse = vbYes Then
Dim V1 As Variant
Dim V2 As Variant
Dim V3 As Variant

V1 = Me!OD.Value
V2 = Me!WALL.Value
V3 = Me!SUBALLOY.Value
RunCommand acCmdRecordsGoToNew

Me!OD = V1
Me!WALL = V2
Me!SUBALLOY = V3

Else
DoCmd.CancelEvent
End If

End Sub
 
P

Paolo

Hi K,
you can add it in the keyup event of the form you are working on in this way

if keycode=vbKeyF1 then 'you check if F1 was pressed
Dim LResponse As Integer

LResponse = MsgBox("Do you wish to continue?", vbYesNo, "Continue")

If LResponse = vbYes Then
Dim V1 As Variant
Dim V2 As Variant
Dim V3 As Variant

V1 = Me!OD.Value
V2 = Me!WALL.Value
V3 = Me!SUBALLOY.Value
RunCommand acCmdRecordsGoToNew

Me!OD = V1
Me!WALL = V2
Me!SUBALLOY = V3

Else
DoCmd.CancelEvent
End If
endif

HTH Paolo
 
K

K

Thanks for the response but it does not work?

Paolo said:
Hi K,
you can add it in the keyup event of the form you are working on in this way

if keycode=vbKeyF1 then 'you check if F1 was pressed
Dim LResponse As Integer

LResponse = MsgBox("Do you wish to continue?", vbYesNo, "Continue")

If LResponse = vbYes Then
Dim V1 As Variant
Dim V2 As Variant
Dim V3 As Variant

V1 = Me!OD.Value
V2 = Me!WALL.Value
V3 = Me!SUBALLOY.Value
RunCommand acCmdRecordsGoToNew

Me!OD = V1
Me!WALL = V2
Me!SUBALLOY = V3

Else
DoCmd.CancelEvent
End If
endif

HTH Paolo
 
D

Dirk Goldgar

K said:
Thanks for the response but it does not work?


There are two issues here. First, in order for a key to be handled by the
form-level event handlers, you have to set the form's KeyPreview property to
Yes. Paolo didn't mention that, so you may not have done that.

Second, and more important, you are trying to use a key, F1, that is already
defined by Access as the built-in "help" key. This is a bad idea in
principle, since the use of the F1 key for help is part of the standard
Windows user-interface guidelines. Redefining it is likely to cause
confusion to (at least) casual users of your application. For this reason
alone, it would be better to choose another key for your purpose; one that
is not predefined.

If you insist on redefining the F1 key, you'll have to disable the built-in
handling for it. I believe you can do that by creating an AutoKeys macro
for the F1 key, and setting the macro action to SendKeys {F1}. Note,
though, that this will cauise problems *everywhere* in your application
except on this particular form. I just tried it, and it seems to get into a
nasty loop when the form doesn't have the focus. There is probably a more
elaborate workaround, but I'm not going to pursue it because I think it's a
bad idea in the first place.
 
K

K

Thanks for your response. Okay any key will do, Hopw about the end key? Just
need hwo to correctly get this to work and I will define a key later. Can you
help?
 
D

Dirk Goldgar

K said:
Thanks for your response. Okay any key will do, Hopw about the end key?
Just
need hwo to correctly get this to work and I will define a key later. Can
you
help?


You could use the End key, though you should be aware that you'll be
supplanting its normal use as a navigation key (in text boxes, for example).
You might do better to use some Alt+{key} combination. Be sure to set the
form's KeyPreview property to Yes, and change your code to check for the key
you've selected, and also clear the key when intend to handle it yourself.
I normally use the KeyDown for this, rather than the KeyUp event, so an
example (using the End key) would be:

'----- start of example code -----
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyEnd Then
KeyCode = 0

' ... your application-specific code goes here ...

End If

End Sub
'----- end of example code -----

If you want to use an Alt+{key} combination, you need to check the event
procedure's Shift argument, as well as the KeyCode argument. For example:

If KeyCode = vbKeyX And (Shift And acAltMask) <> 0 Then

.... checks for Alt+X (though it will also accept Alt+Ctrl+X, Alt+Shift+X,
etc.).
 
K

K

HI Dirk, THanks it worked. I have another question.

I have a main form called frmMain and in this form I have two subforms
frmRates - Sub form
frmCap - Sub form

In frmRates when the user selects the Cost Center number I would like the
frm Cap to filter this number and display the information regarding the Cost
Center Number which are the capaabilities of it. I was thinking the Event on
got focus apply filter to the frmCap. How can I do this?

The field in frmRates is called SUBCC and in frmCap is CCNUM
 
D

Dirk Goldgar

K said:
HI Dirk, THanks it worked. I have another question.

I have a main form called frmMain and in this form I have two subforms
frmRates - Sub form
frmCap - Sub form

In frmRates when the user selects the Cost Center number I would like the
frm Cap to filter this number and display the information regarding the
Cost
Center Number which are the capaabilities of it. I was thinking the Event
on
got focus apply filter to the frmCap. How can I do this?

The field in frmRates is called SUBCC and in frmCap is CCNUM


Since this question is unrelated to the original one, it would have been
better to post it as the start of a new thread. That's better for other
people who may search the forums, and may be better for you in soliciting
the advice of more people than just me. But let me see if I can answer it.

If I understand your description, you may be able to do this with no code at
all. You can make one subform use information from the other subform as one
of its Link Master Fields, so that it automatically shows the related
information. Here's one way to do that:

1. Create a text box on frmMain with these properties:

Name: txtRatesCC
Visible: No
Control Source: =[frmRates].[Form]![SUBCC]

Note 1. This text box is invisible, so you can make it any size you want,
and put it anywhere on the form.

Note 2. I'm assuming that "frmRates" is the actual name of the subform
control on frmMain, not just the name of the form object that subform
control displays. Make sure of that -- you need to use the name of the
subform control in the control source of txtRatesCC, whether or not that's
the same as the form name.

2. For the subform control frmCap, set the following properties:

Link Master Fields: txtRatesCC
Link Child Fields: CCNUM

That ought to do it, unless I've overlooked something or there's something
you haven't told me.
 
K

K

Thank you. Excellet idea. I appreciate it.

Dirk Goldgar said:
K said:
HI Dirk, THanks it worked. I have another question.

I have a main form called frmMain and in this form I have two subforms
frmRates - Sub form
frmCap - Sub form

In frmRates when the user selects the Cost Center number I would like the
frm Cap to filter this number and display the information regarding the
Cost
Center Number which are the capaabilities of it. I was thinking the Event
on
got focus apply filter to the frmCap. How can I do this?

The field in frmRates is called SUBCC and in frmCap is CCNUM


Since this question is unrelated to the original one, it would have been
better to post it as the start of a new thread. That's better for other
people who may search the forums, and may be better for you in soliciting
the advice of more people than just me. But let me see if I can answer it.

If I understand your description, you may be able to do this with no code at
all. You can make one subform use information from the other subform as one
of its Link Master Fields, so that it automatically shows the related
information. Here's one way to do that:

1. Create a text box on frmMain with these properties:

Name: txtRatesCC
Visible: No
Control Source: =[frmRates].[Form]![SUBCC]

Note 1. This text box is invisible, so you can make it any size you want,
and put it anywhere on the form.

Note 2. I'm assuming that "frmRates" is the actual name of the subform
control on frmMain, not just the name of the form object that subform
control displays. Make sure of that -- you need to use the name of the
subform control in the control source of txtRatesCC, whether or not that's
the same as the form name.

2. For the subform control frmCap, set the following properties:

Link Master Fields: txtRatesCC
Link Child Fields: CCNUM

That ought to do it, unless I've overlooked something or there's something
you haven't told me.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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

Similar Threads

Yes No Message Box 7
add record in combo 6
code to change form recordsource 1
SendObject Email 2
SendObject code 6
SendObject and combo boxes 1
new email form in front of MsgBox 1
I am beginner 0

Top