By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,493 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,493 IT Pros & Developers. It's quick & easy.

Clarifying Questions

hyperpau
Expert 100+
P: 184
I just have some stupid questions but really want to know to fully understand vba more.


What are the arguments for in some of the events?

For example, the click event has the arguments (Shift As Integer, Cancel as Integer)


Then the DblClick event have (Cancel as Integer)

what are these for? specifically about these events. I am just curious as to why these appears although I do not use these variables in any of my procedures when I call these events.

The only argument I understand is the response in the NotInList event.

(Response As Integer) since I use it in the code as
Response = DataErrContinue


but regarding Shift and Cancel as Integer, what are these for?

where can I use them? does that mean the shift key? what about the Cancel argument?


Hope somebody can help me clear things up.
Sep 19 '07 #1
Share this Question
Share on Google+
5 Replies


FishVal
Expert 2.5K+
P: 2,653
Hi, hyperpau.

It depends on particular event.
Usually
Shift - represents Ctrl/Alt/Shift buttons state
Cancel - ByRef variable used to tell to the object that has raised the event whether to perform default action or no.
e.g.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Text0_BeforeUpdate(Cancel As Integer)
  2.     If IsNull(Me.Text0) Then Cancel = True
  3. End Sub
  4.  
This will tell Text0 object than update must be cancelled.

I recommend you to use object browser to get help on particular object events.

Regards,
Fish
Sep 19 '07 #2

hyperpau
Expert 100+
P: 184
Wow. thanks.
gives me more interest though.
I am very curious about the Shift.

Does this mean I can call different procedures depending on the key combination pressed with the mouse click?

if yes, please give me an example code.

let's say call a different message box when a button is clicked, then another message box when the button is clicked with the Shift, Ctl or Alt key.

please?.. :) Thanks in advance.


Hi, hyperpau.

It depends on particular event.
Usually
Shift - represents Ctrl/Alt/Shift buttons state
Cancel - ByRef variable used to tell to the object that has raised the event whether to perform default action or no.
e.g.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Text0_BeforeUpdate(Cancel As Integer)
  2. If IsNull(Me.Text0) Then Cancel = True
  3. End Sub
  4.  
This will tell Text0 object than update must be cancelled.

I recommend you to use object browser to get help on particular object events.

Regards,
Fish
Sep 19 '07 #3

FishVal
Expert 2.5K+
P: 2,653
Wow. thanks.
gives me more interest though.
I am very curious about the Shift.

Does this mean I can call different procedures depending on the key combination pressed with the mouse click?

if yes, please give me an example code.

let's say call a different message box when a button is clicked, then another message box when the button is clicked with the Shift, Ctl or Alt key.

please?.. :) Thanks in advance.
Hmm.

Don't know what object raises Click event passing Shift argument.
The following example is for Form_KeyDown event.
Form object passes keyboard state in integer argument "Shift" having the following format.

Expand|Select|Wrap|Line Numbers
  1. Bit#       Set to 1 when pressed
  2. 0            Shift
  3. 1            Ctrl
  4. 2            Alt
  5.  
additionally Access has constants corresponding to keys
acShiftMask = 1
acCtrlMask=2
acAltMask = 4
So to retrieve particular key state you may use bitwise logical operators AND and OR.

e.g.

if Shift AND acShiftMask = 1 Then 'Shift pressed
if Shift AND acCtrlMask = 0 Then 'Ctrl not pressed
if Shift AND (acCtrlMask OR acAltMask) = 1 Then MsgBox "Press DEL and go drink beer"
Sep 19 '07 #4

hyperpau
Expert 100+
P: 184
Wow. very cool.

but what scenarios can we use that Shift in the Click() event?

Hmm.

Don't know what object raises Click event passing Shift argument.
The following example is for Form_KeyDown event.
Form object passes keyboard state in integer argument "Shift" having the following format.

Expand|Select|Wrap|Line Numbers
  1. Bit# Set to 1 when pressed
  2. 0 Shift
  3. 1 Ctrl
  4. 2 Alt
  5.  
additionally Access has constants corresponding to keys
acShiftMask = 1
acCtrlMask=2
acAltMask = 4
So to retrieve particular key state you may use bitwise logical operators AND and OR.

e.g.

if Shift AND acShiftMask = 1 Then 'Shift pressed
if Shift AND acCtrlMask = 0 Then 'Ctrl not pressed
if Shift AND (acCtrlMask OR acAltMask) = 1 Then MsgBox "Press DEL and go drink beer"
Sep 19 '07 #5

FishVal
Expert 2.5K+
P: 2,653
Wow. very cool.

but what scenarios can we use that Shift in the Click() event?
Hmm.

There is no Click event which passes keyboard state to event handler.
This requires some play around.

The following code distinguish between Click and Ctrl+Click on Listbox.
MouseUp event handler checks whether Ctrl key is pressed, so far ListBox.Value is not yet set to item clicked. Then on Click event when ListBox.Value is set properly, Click event handler checks whether Ctrl key was pressed when mouse button was released. ;)

The form mentioned contains two controls:
Text2: unbound Textbox
List0: Listbox with RowSourceType = ValueList

Expand|Select|Wrap|Line Numbers
  1. Private blnCtrlPressed As Boolean
  2.  
  3. Private Sub CheckKeyboardState(Shift As Integer)
  4.     If (Shift And acCtrlMask) <> 0 Then
  5.         blnCtrlPressed = True
  6.     Else
  7.         blnCtrlPressed = False
  8.     End If
  9. End Sub
  10.  
  11.  
  12. Private Sub List0_Click()
  13.     With Me
  14.         If blnCtrlPressed Then
  15.             .Text2 = "Ctrl + " & .List0
  16.         Else
  17.             .Text2 = .List0
  18.         End If
  19.     End With
  20.  
  21. End Sub
  22.  
  23. Private Sub List0_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  24.     CheckKeyboardState (Shift)
  25. End Sub
  26.  
Sep 20 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.