Code for Shift-Click?

C

croy

How do I code for a command-button on a form where a regular
left-mouse click does one thing, and a <Shift> + left-mouse
click does another (or <Alt> + left-mouse click)?

The built-in help is no help, and it's a difficult one to
web-search for...
 
M

Marshall Barton

croy said:
How do I code for a command-button on a form where a regular
left-mouse click does one thing, and a <Shift> + left-mouse
click does another (or <Alt> + left-mouse click)?

The built-in help is no help, and it's a difficult one to
web-search for...


Check VBA Help for the MouseUp (and MouseDown) Event
where you have the mouse button and Shift arguments.

Select Case True
Case Shift And acShiftMask
'shift key is down
Case Else
'shift key is not down
End If

You might want to deal with the other mouse buttons and the
Ctrl and Alt keys too?
 
C

croy

Check VBA Help for the MouseUp (and MouseDown) Event
where you have the mouse button and Shift arguments.

Select Case True
Case Shift And acShiftMask
'shift key is down
Case Else
'shift key is not down
End If

You might want to deal with the other mouse buttons and the
Ctrl and Alt keys too?

Thanks for the reply.

I tried this:

Dim stDocName As String
stDocName = "qryY_M_S_TtlHrs_R"

Select Case True
Case Shift And acShiftMask
'shift key is down
DoCmd.OpenQuery stDocName, acViewDesign
Case Else
'shift key is not down
DoCmd.OpenQuery stDocName
End Select

.... but whether I hold the shift key down or not, it only
opens the query in data view.
 
M

Marshall Barton

croy said:
I tried this:

Dim stDocName As String
stDocName = "qryY_M_S_TtlHrs_R"

Select Case True
Case Shift And acShiftMask
'shift key is down
DoCmd.OpenQuery stDocName, acViewDesign
Case Else
'shift key is not down
DoCmd.OpenQuery stDocName
End Select

... but whether I hold the shift key down or not, it only
opens the query in data view.


My fault. That should have been:
Case CBool(Shift And acShiftMask)
 
C

croy

My fault. That should have been:
Case CBool(Shift And acShiftMask)


Hmmm.

I tried this:

*****
Dim stDocName As String
stDocName = "qryY_M_S_TtlHrs_R"

Select Case True
Case CBool(Shift And acShiftMask)
'shift key is down
DoCmd.OpenQuery stDocName, acViewDesign
Case Else
'shift key is not down
DoCmd.OpenQuery stDocName
End Select
*****

.... and the query just opens in data view whether I hold the
shift key down or not.

I experimented by putting a msgbox line in the code under
"Else", and the message box fired when I held the shift key
down. This is on Access 2002--might that make a difference
(I can't find "acShiftMask" in the help anywhere)?
 
M

Marshall Barton

croy said:
I tried this:

*****
Dim stDocName As String
stDocName = "qryY_M_S_TtlHrs_R"

Select Case True
Case CBool(Shift And acShiftMask)
'shift key is down
DoCmd.OpenQuery stDocName, acViewDesign
Case Else
'shift key is not down
DoCmd.OpenQuery stDocName
End Select
*****

... and the query just opens in data view whether I hold the
shift key down or not.

I experimented by putting a msgbox line in the code under
"Else", and the message box fired when I held the shift key
down. This is on Access 2002--might that make a difference
(I can't find "acShiftMask" in the help anywhere)?


acShiftMask is included in the VBA Help topics for MouseUp
and MouseDown Event.

Are you sure you are using one of those events? If you put
that code in the Click event, you should have received a
compile error about Shift being an undefined variable (if
you didn't get a compile error, you are missing the
Option Explicit
statement at the top of the module).
 
C

croy

acShiftMask is included in the VBA Help topics for MouseUp
and MouseDown Event.

Hmmm. I'm not able to find that here. I did find this:

*****
MouseDown, MouseUp Events
Occur when the user clicks a mouse button. MouseDown occurs
when the user presses the mouse button; MouseUp occurs when
the user releases the mouse button.
Syntax
For MultiPage, TabStrip
Private Sub object_MouseDown( index As Long, ByVal Button As
fmButton, ByVal Shift As fmShiftState, ByVal X As Single,
ByVal Y As Single)

Private Sub object_MouseUp( index As Long, ByVal Button As
fmButton, ByVal Shift As fmShiftState, ByVal X As Single,
ByVal Y As Single)
For other controls
Private Sub object_MouseDown( ByVal Button As fmButton,
ByVal Shift As fmShiftState, ByVal X As Single, ByVal Y As
Single)

Private Sub object_MouseUp( ByVal Button As fmButton, ByVal
Shift As fmShiftState, ByVal X As Single, ByVal Y As Single)
The MouseDown and MouseUp event syntaxes have these parts:
Part Description
object Required. A valid object.
index Required. The index of the page or tab in a
MultiPage or TabStrip with the specified event.
Button Required. An integer value that identifies which
mouse button caused the event.
Shift Required. The state of SHIFT, CTRL, and ALT.
X, Y Required. The horizontal or vertical position, in
points, from the left or top edge of the form, Frame, or
Page.

Settings
The settings for Button are:
Constant Value Description
fmButtonLeft 1 The left button was pressed.
fmButtonRight 2 The right button was pressed.
fmButtonMiddle 4 The middle button was pressed.

The settings for Shift are:
Value Description
1 SHIFT was pressed.
2 CTRL was pressed.
3 SHIFT and CTRL were pressed.
4 ALT was pressed.
5 ALT and SHIFT were pressed.
6 ALT and CTRL were pressed.
7 ALT, SHIFT, and CTRL were pressed.

You can identify individual keyboard modifiers by using the
following constants:
Constant Value Description
fmShiftMask 1 Mask to detect SHIFT.
fmCtrlMask 2 Mask to detect CTRL.
fmAltMask 4 Mask to detect ALT.


Remarks
For a MultiPage, the MouseDown event occurs when the user
presses a mouse button over the control.
For a TabStrip, the index argument identifies the tab where
the user clicked. An index of -1 indicates the user did not
click a tab. For example, if there are no tabs in the upper
right corner of the control, clicking in the upper right
corner sets the index to -1.
For a form, the user can generate MouseDown and MouseUp
events by pressing and releasing a mouse button in a blank
area, record selector, or scroll bar on the form.
The sequence of mouse-related events is:
1. MouseDown
2. MouseUp
3. Click
4. DblClick
5. MouseUp
MouseDown or MouseUp event procedures specify actions that
occur when a mouse button is pressed or released. MouseDown
and MouseUp events enable you to distinguish between the
left, right, and middle mouse buttons. You can also write
code for mouse-keyboard combinations that use the SHIFT,
CTRL, and ALT keyboard modifiers.
If a mouse button is pressed while the pointer is over a
form or control, that object "captures" the mouse and
receives all mouse events up to and including the last
MouseUp event. This implies that the X, Y mouse-pointer
coordinates returned by a mouse event may not always be
within the boundaries of the object that receives them.
If mouse buttons are pressed in succession, the object that
captures the mouse receives all successive mouse events
until all buttons are released.
Use the Shift argument to identify the state of SHIFT, CTRL,
and ALT when the MouseDown or MouseUp event occurred. For
example, if both CTRL and ALT are pressed, the value of
Shift is 6.
*****

....but no mention of "acShiftMask" that I could find.

Are you sure you are using one of those events? If you put
that code in the Click event, you should have received a
compile error about Shift being an undefined variable (if
you didn't get a compile error, you are missing the
Option Explicit
statement at the top of the module).

Just to be sure, I remade the module, and got the same
results. I made sure to put it on the MouseDown event.
Perhaps this function(?) was added after v2002?

Maybe I should switch to a keypress or keydown event--I've
gotten those to work before. I'll just have to find where
I've done it and try to understand what I did!

I had no idea it would be so difficult to do a Shift-Click!

Thanks for your suggestions.
 
S

Stuart McCall

croy said:
You can identify individual keyboard modifiers by using the
following constants:
Constant Value Description
fmShiftMask 1 Mask to detect SHIFT.
fmCtrlMask 2 Mask to detect CTRL.
fmAltMask 4 Mask to detect ALT.

Is this Access 2007? I ask because I've never seen these constants in any
previous Access version. MS have changed 'ac' to 'fm', with no explanation,
naturally. I wonder why?
 
M

Marshall Barton

I haven't used A2007 enough to seriously complain about its
Help system until now. Try the topic for the MouseUp event
for a command button, it seems to be a generic description
that applies to most ordinary controls. I guess that raises
the question of what kind of control you are using?

Attempting to avoid being diverted to a discussion of Help
system confusions, did you make sure you were using the
MouseUp event? Do you get a compile error in your code?

FYI, the acShiftMask constant has the value 1
 
C

croy

Is this Access 2007?

Access 2002.
I ask because I've never seen these constants in any
previous Access version. MS have changed 'ac' to 'fm', with no explanation,
naturally. I wonder why?

I know not why, but it wouldn't surprise me if it caused a
collective roar of curse-words heretofor unknown in these
parts... ;-)
 
C

croy

I haven't used A2007 enough to seriously complain about its
Help system until now.

I'm using A2002.
Try the topic for the MouseUp event
for a command button, it seems to be a generic description
that applies to most ordinary controls. I guess that raises
the question of what kind of control you are using?

Command button.
Attempting to avoid being diverted to a discussion of Help
system confusions [...]

Gritting my teeth... ;-l
... did you make sure you were using the MouseUp event?

I was actually using the MouseDown event. Does that make a
difference?
Do you get a compile error in your code?
Nope.

FYI, the acShiftMask constant has the value 1

Ok...
 
M

Marshall Barton

croy said:
I'm using A2002.


Command button.

Then that's the version pf the Mouse Up/Down Help topic you
should have looked at.
Attempting to avoid being diverted to a discussion of Help
system confusions [...]

Gritting my teeth... ;-l

Careful there. The dentist bill can be steep ;-)
I was actually using the MouseDown event. Does that make a
difference?

Not really, but most user interfaces wait till MouseUp to
determine if the user dragged the mouse off of the control
before releasing the button. Our code doesn't check for
that, but at least it's the "standard" event.

Well I copied/pasted the code into a test form and it worked
ok for me. Try setting a break point in the procedure and
checking the values of everything to see if they are as
expected.
 
M

Marshall Barton

I just has another stray thought. If the query was already
open, OpenQueryt won't do anything except give it the focus.
 
C

croy

Then that's the version pf the Mouse Up/Down Help topic you
should have looked at.

Version of a help topic? Now I'm really stumped. How do I
find a version of a help topic? All I've ever done in VB
Help is to open it from the Help menu and do a search for,
in this case, "MouseUp". It offers only one topic for
"MouseUp Event".
Well I copied/pasted the code into a test form and it worked
ok for me. Try setting a break point in the procedure and
checking the values of everything to see if they are as
expected.

What Access version are you using?
 
C

croy

I just has another stray thought. If the query was already
open, OpenQueryt won't do anything except give it the focus.

That would be fine, but the query isn't open when the button
is "Shift-Clicked", so I guess it's moot.
 
M

Marshall Barton

croy said:
Version of a help topic? Now I'm really stumped. How do I
find a version of a help topic? All I've ever done in VB
Help is to open it from the Help menu and do a search for,
in this case, "MouseUp". It offers only one topic for
"MouseUp Event".

Confused? You're confused? I am gnashing my teeth (not
quite grinding) over my confusion about the help topic you
copied/pasted earlier in this thread. It was from A2007 and
was for some kind of multi page thingy, not the help topic
for an ordinary control (e.g. command button). When I used
A2002, the Help search found two topics for MouseUp Event,
one rather brief, the more detailed topic is essentially the
same as in A2003
What Access version are you using?

I am using A2003, but I am sure that the Mouse events are
the same as in A2002.

Did you try setting a breakpoint and checking the value of
all the variables/arguments? (WARNING: Do NOT edit any
code while in break mode.)
 
M

Marshall Barton

croy said:
That would be fine, but the query isn't open when the button
is "Shift-Clicked", so I guess it's moot.


Oh well, it was just a shot in the dark.
 
C

croy

Confused? You're confused?

I was born that way...
I am gnashing my teeth (not
quite grinding) over my confusion about the help topic you
copied/pasted earlier in this thread. It was from A2007 and
was for some kind of multi page thingy, not the help topic
for an ordinary control (e.g. command button). When I used
A2002, the Help search found two topics for MouseUp Event,
one rather brief, the more detailed topic is essentially the
same as in A2003

Well... I've never even *seen* Access 2007, much less
installed it or used it. That Help info I got from:

Help | Microsoft Visual Basic Help | Contents | Microsoft
Forms Reference | Microsoft Forms Object Model Reference |
Events | MouseDown, MouseUp Events

That was a little more verbose than what I got by just
searching the "Answer Wizard" for "MouseDown". For
MouseDown, I get:

*****
MouseDown Event
See Also Applies To Example Specifics
The MouseDown event occurs when the user presses a mouse
button.

Remarks
The MouseDown event applies only to forms, form sections,
and controls on a form, not controls on a report.
This event does not apply to a label attached to another
control, such as the label for a text box. It applies only
to "freestanding" labels. Pressing and releasing a mouse
button in an attached label has the same effect as pressing
and releasing the button in the associated control. The
normal events for the control occur; no separate events
occur for the attached label.
To run a macro or event procedure when these events occur,
set the OnMouseDown property to the name of the macro or to
[Event Procedure].

You can use a MouseDown event to specify what happens when a
particular mouse button is pressed or released. Unlike the
Click and DblClick events, the MouseDown event enables you
to distinguish between the left, right, and middle mouse
buttons. You can also write code for mouse-keyboard
combinations that use the SHIFT, CTRL, and ALT keys.

To cause a MouseDown event for a form to occur, press the
mouse button in a blank area or record selector on the form.
To cause a MouseDown event for a form section to occur,
press the mouse button in a blank area of the form section.

The following apply to MouseDown events:

If a mouse button is pressed while the pointer is over a
form or control, that object receives all mouse events up to
and including the last MouseUp event.
If mouse buttons are pressed in succession, the object that
receives the mouse event after the first press receives all
mouse events until all buttons are released.
To respond to an event caused by moving the mouse, you use a
MouseMove event.

Example
The following example shows how you can find out which mouse
button caused a MouseDown event.

To try the example, add the following event procedure to a
form:

Private Sub Form_MouseDown(Button As Integer, _
Shift As Integer, X As Single, _
Y As Single)
If Button = acLeftButton Then
MsgBox "You pressed the left button."
End If
If Button = acRightButton Then
MsgBox "You pressed the right button."
End If
If Button = acMiddleButton Then
MsgBox "You pressed the middle button."
End If
End Sub
*****

Still no mention of acShiftMask.

And if I use the Anwer Wizard to search for acShiftMask, I
get "Please rephrase your question." The only kind of
phrases I can think of probably aren't appropriate...

Here's a thought: could it be that it's only present in a
more robust "edition" of A2002 (more robust than simply what
comes with Office 2002 Pro)? One that was for developers?

Hmmm, again (rubbing chin).
I am using A2003, but I am sure that the Mouse events are
the same as in A2002.

Did you try setting a breakpoint and checking the value of
all the variables/arguments? (WARNING: Do NOT edit any
code while in break mode.)

Not yet. Will do that soon, if my skills permit.

Thanks again for all your thoughts.
 
M

Marshall Barton

What can I say? This really is confusing. Maybe(?) your
help system went online to get the Help topic from the
MS-MSDN site and got it from the wrong version.

Regardless of all this Help bleep, you can either use
acShift. which is just a built in constant or its value,
which is 1
--
Marsh
MVP [MS Access]

Confused? You're confused?

I was born that way...
I am gnashing my teeth (not
quite grinding) over my confusion about the help topic you
copied/pasted earlier in this thread. It was from A2007 and
was for some kind of multi page thingy, not the help topic
for an ordinary control (e.g. command button). When I used
A2002, the Help search found two topics for MouseUp Event,
one rather brief, the more detailed topic is essentially the
same as in A2003

Well... I've never even *seen* Access 2007, much less
installed it or used it. That Help info I got from:

Help | Microsoft Visual Basic Help | Contents | Microsoft
Forms Reference | Microsoft Forms Object Model Reference |
Events | MouseDown, MouseUp Events

That was a little more verbose than what I got by just
searching the "Answer Wizard" for "MouseDown". For
MouseDown, I get:

*****
MouseDown Event
See Also Applies To Example Specifics
The MouseDown event occurs when the user presses a mouse
button.

Remarks
The MouseDown event applies only to forms, form sections,
and controls on a form, not controls on a report.
This event does not apply to a label attached to another
control, such as the label for a text box. It applies only
to "freestanding" labels. Pressing and releasing a mouse
button in an attached label has the same effect as pressing
and releasing the button in the associated control. The
normal events for the control occur; no separate events
occur for the attached label.
To run a macro or event procedure when these events occur,
set the OnMouseDown property to the name of the macro or to
[Event Procedure].

You can use a MouseDown event to specify what happens when a
particular mouse button is pressed or released. Unlike the
Click and DblClick events, the MouseDown event enables you
to distinguish between the left, right, and middle mouse
buttons. You can also write code for mouse-keyboard
combinations that use the SHIFT, CTRL, and ALT keys.

To cause a MouseDown event for a form to occur, press the
mouse button in a blank area or record selector on the form.
To cause a MouseDown event for a form section to occur,
press the mouse button in a blank area of the form section.

The following apply to MouseDown events:

If a mouse button is pressed while the pointer is over a
form or control, that object receives all mouse events up to
and including the last MouseUp event.
If mouse buttons are pressed in succession, the object that
receives the mouse event after the first press receives all
mouse events until all buttons are released.
To respond to an event caused by moving the mouse, you use a
MouseMove event.

Example
The following example shows how you can find out which mouse
button caused a MouseDown event.

To try the example, add the following event procedure to a
form:

Private Sub Form_MouseDown(Button As Integer, _
Shift As Integer, X As Single, _
Y As Single)
If Button = acLeftButton Then
MsgBox "You pressed the left button."
End If
If Button = acRightButton Then
MsgBox "You pressed the right button."
End If
If Button = acMiddleButton Then
MsgBox "You pressed the middle button."
End If
End Sub
*****

Still no mention of acShiftMask.

And if I use the Anwer Wizard to search for acShiftMask, I
get "Please rephrase your question." The only kind of
phrases I can think of probably aren't appropriate...

Here's a thought: could it be that it's only present in a
more robust "edition" of A2002 (more robust than simply what
comes with Office 2002 Pro)? One that was for developers?

Hmmm, again (rubbing chin).
I am using A2003, but I am sure that the Mouse events are
the same as in A2002.

Did you try setting a breakpoint and checking the value of
all the variables/arguments? (WARNING: Do NOT edit any
code while in break mode.)

Not yet. Will do that soon, if my skills permit.
 

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