Results 1 to 6 of 6
  1. #1
    ser01 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Feb 2010
    Posts
    64

    Open pop up form at cursor.

    Hello everybody!
    I have a small pop up form, that I would like to open at cursor. To open the form, the user would press a button on a different form. I know that there is a windows API for getting the cursor position. I'm just not sure how to use that to move the opened form to where the cursor is. Any suggestions would be very much appreciated.
    Thank you very much in advance.
    Last edited by ser01; 04-13-2012 at 08:09 AM.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Google: VBA set form position.

    Here is one http://bytes.com/topic/access/answer...-form-position
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ser01 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Feb 2010
    Posts
    64
    Quote Originally Posted by June7 View Post
    Google: VBA set form position.

    Here is one http://bytes.com/topic/access/answer...-form-position
    Hi June7,
    Thank you for your reply. I did google that before posting my questions. I am familiar with how you can move a form with VBA. My problem is that I need to move the form to where the cursor is, at the moment the form opens. I don't know how to get the cursor position in VBA, or how to use that position to move my form to.

  4. #4
    ser01 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Feb 2010
    Posts
    64
    After a lot of research and trial and error, I was finally able to figure it out. I'm gonna post my code, in case someone else will find it useful. Please keep in mind that I am not a programmer by any means. Everything I know, I learned on my own through trial and error, reading and researching. Having said that, this may not be the "proper way" of accomplishing this. It works well however.
    The reason why I wanted this done in the first place, is because I have a form that is populated with data. The data is coming from a customers table. For each data point, I have a button called actions. When the user clicks on the button, a small pop up form opens that has nothing but 7 buttons on it. The form allows the user to perform different actions for each customer (ex. Edit contact info, print usage reports, print order guides, print statements, etc.). I wanted the form opened at cursor, because once opened, I did not want my users go "hunting" for it on the screen. So it's basically like a context menu. Here is the code:

    This needs to be put in a module:

    Code:
    Option Compare Database
    Option Explicit
    
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Type RECT_Type
        left As Long
        top As Long
        right As Long
        bottom As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Declare Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hWnd As Long, lpRect As RECT_Type) As Long
    
    Public Function GetXCursorPos() As Long
        Dim pt As POINTAPI
        GetCursorPos pt
        GetXCursorPos = pt.x
    End Function
    
    Public Function GetYCursorPos() As Long
        Dim pt As POINTAPI
        GetCursorPos pt
        GetYCursorPos = pt.y
    End Function
    The following code, goes in the OnClick event for the button that opens the form:

    Code:
    Option Compare Database
    Option Explicit
    
    '---------------------------------------------------------------------------------------
    ' Procedure : cmdActions_Click
    ' Author    : Sergiu Plesca
    ' Date      : 4/13/2012
    ' Purpose   : Opens the actions form, moves it to cursor, and sets the TempVars
    '---------------------------------------------------------------------------------------
    '
    Private Sub cmdActions_Click()
        Dim FLeft As Long
        Dim FTop As Long
        Dim FormRect As RECT_Type
        Dim MDIClient As RECT_Type
        Dim MDIClientLeft As Long
        Dim MDIClientTop As Long
        Dim x As Integer
        Dim y As Integer
    
       On Error GoTo cmdActions_Click_Error
    
    'Determine the Left and Top coordinates inside Access MDIClient window
        apiGetWindowRect Me.hWnd, FormRect
        FLeft = FormRect.left
        FTop = FormRect.top
        MDIClientLeft = MDIClient.left
        MDIClientTop = MDIClient.top
        FLeft = FLeft - MDIClientLeft
        FTop = FTop - MDIClientTop
    
        x = (GetXCursorPos() * 15 - FLeft * 15)
        y = (GetYCursorPos() * 15 - FTop * 15)
    
        TempVars.Remove "customer_code_TempVar"
        TempVars.Add "customer_code_TempVar", Me.Customer_Code.Value
        DoCmd.OpenForm "frmCustomersHomeActions"
        [Form_frmCustomersHomeActions].Move WindowLeft + x, WindowTop + y
    
    ' *** ERROR HANDLER *** ' ' *** ERROR HANDLER *** ' ' *** ERROR HANDLER *** '
       On Error GoTo 0
       Exit Sub
    
    cmdActions_Click_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdActions_Click of VBA Document Form_frmCustomerMainMenu"
    End Sub

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Glad you figured it out. When I read your OP it looked like you had the API cursor code done but did not know how to set the form position properties.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    JamesAnderson1984 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    5

    Suggested modification

    I realize that this thread was started over a year ago, but I would like to make a suggested modification to this code. This is a great code and I am currently using it in applications that require me to open a form in the mouse position. You don't have to make this a private sub within a form. You can make this a Public Sub or Public Function in a module. Instead of Me.hWnd you can declare a few variables. My example of the modification is below. You need to declare a variable 'As Form' and then set the variable to 'Screen.ActiveForm'. Change 'Me.hWnd' to '[variable name].hWnd'. In my example it is simply 'f.hWnd'. You may need to play around with these lines (FLeft = FormRect.left, FTop = FormRect.Top) to adjust this code so the form shows up in the exact location you click (This requires adding +/- and a number as shown in the example below). Hope this helps!


    Public Function CursorPosition()
    '---------------------------------------------------------------------------------------
    ' Procedure : cmdActions_Click
    ' Author : Sergiu Plesca
    ' Date : 4/13/2012
    ' Modified by: James Anderson
    ' Mod Date: 8/5/2013
    ' Purpose : Opens the actions form, moves it to cursor, and sets the TempVars
    '---------------------------------------------------------------------------------------

    Dim FLeft As Long
    Dim FTop As Long
    Dim FormRect As RECT_Type
    Dim MDIClient As RECT_Type
    Dim MDIClientLeft As Long
    Dim MDIClientTop As Long
    Dim X As Integer
    Dim Y As Integer

    'Added so Form1 can be opened from any form without having
    'to put this function in every event on every form.
    Dim f As Form
    Set f = Screen.ActiveForm

    'Determine the Left and Top coordinates inside Access MDIClient window
    apiGetWindowRect f.hwnd, FormRect
    FLeft = FormRect.left + 54
    FTop = FormRect.Top + 35
    MDIClientLeft = MDIClient.left
    MDIClientTop = MDIClient.Top
    FLeft = FLeft - MDIClientLeft
    FTop = FTop - MDIClientTop

    X = (GetXCursorPos() * 15 - FLeft * 15)
    Y = (GetYCursorPos() * 15 - FTop * 15)

    DoCmd.OpenForm "Form1"
    Forms!Form1.Move f.WindowLeft + X, f.WindowTop + Y

    End Function
    Last edited by JamesAnderson1984; 08-07-2013 at 09:25 AM.

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

Similar Threads

  1. Changing the cursor
    By BernardKane in forum Forms
    Replies: 4
    Last Post: 10-24-2022, 09:59 AM
  2. Replies: 2
    Last Post: 02-17-2012, 04:09 AM
  3. T-SQL Cursor within a cursor
    By allenjasonbrown@gmail.com in forum Programming
    Replies: 6
    Last Post: 04-14-2011, 12:31 PM
  4. Bold Field by Cursor Position
    By Rawb in forum Forms
    Replies: 4
    Last Post: 02-08-2011, 08:03 AM
  5. Cursor issue when no records
    By shenry16 in forum Forms
    Replies: 4
    Last Post: 01-20-2010, 01:22 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