Results 1 to 12 of 12
  1. #1
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175

    How to get location of object

    This is a good one!

    I have a form that contains 336 text 'caption' boxes. It is for scheduling jobs. Each box does (or can) hold a job name. The job names are entered on this form from another form.

    I have built a right click menu to perform different tasks. When I right click on one of these objects, and then select a item from the list, I get the mouse position of the item on the menu. I need the mouse position of the box that I right clicked.

    I included my code although you should not need it. Help please.

    Sub CreateNewShortcutMenu()
    Dim newButton As CommandBarControl
    Dim cmbRightClick As Office.CommandBar
    Set cmbRightClick = CommandBars.Add("newRightClick", msoBarPopup, False, False)


    Set newButton = cmbRightClick.Controls.Add(msoControlButton, 1, , , False)
    With newButton
    .Style = msoButtonIconAndCaption
    .Caption = "&Invoice" ' Menu caption which shows when right clicked
    ' .Picture = CurrentProject.path & "\Images\Invoice.png" ' Path to the picture
    .OnAction = "=Invoice()" ' name of fuction to call
    End With

    Set newButton = cmbRightClick.Controls.Add(msoControlButton, 1, , , False)
    With newButton
    .Style = msoButtonIconAndCaption
    .Caption = "&Proposal" ' Menu caption which shows when right clicked
    ' .Picture = CurrentProject.path & "\Images\Invoice.png" ' Path to the picture
    .OnAction = "=Proposal()" ' name of fuction to call
    End With
    End Sub

    Public Function Invoice()
    ' MsgBox "You clicked on Invoice"
    Get_Cursor_Pos
    End Function

    Public Function Proposal()
    ' MsgBox "You clicked on Invoice"
    Get_Cursor_Pos
    End Function

    Sub Get_Cursor_Pos()
    ' Main routine to dimension variables, retrieve cursor position,
    ' and display coordinates
    ' Dimension the variable that will hold the x and y cursor positions
    Dim Hold As POINTAPI
    ' Place the cursor positions in variable Hold
    GetCursorPos Hold
    ' Display the cursor position coordinates
    MsgBox "X Position is : " & Hold.X_Pos & Chr(10) & _
    "Y Position is : " & Hold.Y_Pos
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Does this work for you?

    Debug.Print Screen.ActiveControl.Top & " : " & Screen.ActiveControl.Left
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175
    Where do I put that code. I am doing this on the right click? I get '0 : 2460' everywhere I right click.

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Perhaps the right click isn't giving focus to the textbox? In a brief test it did. Can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175
    Well, I have a new problem now. My database is very large. I am trying to make you a sample, but for the right click, I need a reference to Access objects. The 'reference' object under tools is grayed. I don't know why. Also it is saving the database as a 'mdb' instead of 'accdb'. I am using Access 2010.

  6. #6
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175
    Ok, I have created and attached a sample database. There is one form which has no code attached. There is one module which has all the code. I need to right click on any of the 3 objects on the form and get the mouse position of that object and open the short cut menu. Getting the mouse position of the object will let me know which object was right clicked.

    The way I have written the code, I get the mouse position of the mouse when I click the menu selection. I need the mouse position when the box is right clicked.
    Attached Files Attached Files

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    on your control can't you just use something like:

    Code:
    Private Sub fld_Test_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = acRightButton Then
                 'bring up your right click menu code here
        End If
    End Sub

  8. #8
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    You used labels, which can't have focus, which is why my method didn't work. Changing them to textboxes appears to work, but not sure how that fits into what you're trying to accomplish. Chris does a lot with this type of thing, so you may find his drag and drop sample helpful:

    http://www.baldyweb.com/ChrisOSamples.htm

    I use it in a map application and grab the position of the label as it is moved.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I downloaded your database and put the mouseup code on the controls in the subform expected results here's a copy of your database back the mouseup commands seem to work for all controls in the subform whether you right click on the label or the text box itself.

    test.zip

  10. #10
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175
    I may have found a way to get it done. I am now trying to use the 'mouse move'. With that, I can get the actual object name, instead of having to determine which object based on cursor location. Thank you for trying.

  11. #11
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    my example gets the control name, 'object name' is vague. There are two components to a text box the text box itself and the label, if you are trying to get the control name regardless of whether you right click on the text box or the label, the code I gave you will work.

    If you are trying to get two different returned values based on whether the user right clicks the label are as opposed to the text box area that's a different matter but I don't know why you'd want to do anything when right clicking on the label of a text box control.

  12. #12
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175
    I am trying to get the control name. I like your way better than mine. Mouse move constantly sends the control name where mouse up does it one time. I thought mouse up only worked with mouse down. Thank you very much.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 02-06-2014, 12:55 AM
  2. Replies: 1
    Last Post: 07-02-2013, 08:41 AM
  3. Replies: 1
    Last Post: 09-03-2011, 07:01 PM
  4. Replies: 3
    Last Post: 11-02-2010, 10:14 AM
  5. Replies: 1
    Last Post: 08-05-2010, 12:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums