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