Clarifying Questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hyperpau
    Recognized Expert New Member
    • Jun 2007
    • 184

    #1

    Clarifying Questions

    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.
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    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.
    Code:
    Private Sub Text0_BeforeUpdate(Cancel As Integer)
        If IsNull(Me.Text0) Then Cancel = True
    End Sub
    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

    Comment

    • hyperpau
      Recognized Expert New Member
      • Jun 2007
      • 184

      #3
      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.


      Originally posted by FishVal
      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.
      Code:
      Private Sub Text0_BeforeUpdate(Cancel As Integer)
      If IsNull(Me.Text0) Then Cancel = True
      End Sub
      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

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Originally posted by hyperpau
        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.

        Code:
        Bit#       Set to 1 when pressed
        0            Shift
        1            Ctrl
        2            Alt
        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"

        Comment

        • hyperpau
          Recognized Expert New Member
          • Jun 2007
          • 184

          #5
          Wow. very cool.

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

          Originally posted by FishVal
          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.

          Code:
          Bit# Set to 1 when pressed
          0 Shift
          1 Ctrl
          2 Alt
          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"

          Comment

          • FishVal
            Recognized Expert Specialist
            • Jun 2007
            • 2656

            #6
            Originally posted by hyperpau
            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

            [code=vb]
            Private blnCtrlPressed As Boolean

            Private Sub CheckKeyboardSt ate(Shift As Integer)
            If (Shift And acCtrlMask) <> 0 Then
            blnCtrlPressed = True
            Else
            blnCtrlPressed = False
            End If
            End Sub


            Private Sub List0_Click()
            With Me
            If blnCtrlPressed Then
            .Text2 = "Ctrl + " & .List0
            Else
            .Text2 = .List0
            End If
            End With

            End Sub

            Private Sub List0_MouseUp(B utton As Integer, Shift As Integer, X As Single, Y As Single)
            CheckKeyboardSt ate (Shift)
            End Sub
            [/code]

            Comment

            Working...