Moving The Cursor By Code

Discussion in 'Microsoft Access Form Coding' started by Stewart Berman, May 21, 2010.

  1. Access 2007

    I want to be able to move the cursor using VBA code. I put the following in
    a module:

    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,
    lpRect As RECT) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) As
    Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
    ByVal y As Long) As Long
    Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
    Dim lReturn As Long
    Dim pnt As POINT
    Dim rec As RECT
    lReturn = GetWindowRect(vlHwnd, rec)
    Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
    rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
    lReturn = GetCursorPos(pnt)
    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    y: " & pnt.y
    lReturn = SetCursorPos(rec.Left, rec.Top)
    Debug.Print "SetCursorPos: " & lReturn
    lReturn = GetCursorPos(pnt)
    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    y: " & pnt.y
    MoveMouseToTopLeftCorner = (0 <> lReturn)
    End Function

    In the click event of a button on a form I put:
    MoveMouseToTopLeftCorner Me.hwnd

    I then opened the form and clicked on the button. The cursor changed from a
    hand to an arrow but it did not move.

    The Debug statements produced:
    GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    GetCursorPos - Return: 1, x: 824, y: 657
    SetCursorPos: 1
    GetCursorPos - Return: 1, x: 242, y: 379

    This would seem to indicate that the cursor moved but it didn't. If I
    clicked on the button and then clicked on it again without moving the mouse
    the debug statements produced:
    GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    GetCursorPos - Return: 1, x: 804, y: 654
    SetCursorPos: 1
    GetCursorPos - Return: 1, x: 242, y: 379

    GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    GetCursorPos - Return: 1, x: 804, y: 654
    SetCursorPos: 1
    GetCursorPos - Return: 1, x: 242, y: 379

    As you can see from the above the second time the button was clicked the
    system thought the cursor was back in the original position.

    What did I miss?
     
    Stewart Berman, May 21, 2010
    #1
    1. Advertisements

  2. What are you trying to achieve? What is the purpose? Perhaps if you explain
    a little more, we could advise you.
    --
    Hope this helps,

    Daniel Pineault
    http://www.cardaconsultants.com/
    For Access Tips and Examples: http://www.devhut.net
    Please rate this post using the vote buttons if it was helpful.



    "Stewart Berman" wrote:

    > Access 2007
    >
    > I want to be able to move the cursor using VBA code. I put the following in
    > a module:
    >
    > Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,
    > lpRect As RECT) As Long
    > Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) As
    > Long
    > Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
    > ByVal y As Long) As Long
    > Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
    > Dim lReturn As Long
    > Dim pnt As POINT
    > Dim rec As RECT
    > lReturn = GetWindowRect(vlHwnd, rec)
    > Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
    > rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
    > lReturn = GetCursorPos(pnt)
    > Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    > y: " & pnt.y
    > lReturn = SetCursorPos(rec.Left, rec.Top)
    > Debug.Print "SetCursorPos: " & lReturn
    > lReturn = GetCursorPos(pnt)
    > Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    > y: " & pnt.y
    > MoveMouseToTopLeftCorner = (0 <> lReturn)
    > End Function
    >
    > In the click event of a button on a form I put:
    > MoveMouseToTopLeftCorner Me.hwnd
    >
    > I then opened the form and clicked on the button. The cursor changed from a
    > hand to an arrow but it did not move.
    >
    > The Debug statements produced:
    > GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    > GetCursorPos - Return: 1, x: 824, y: 657
    > SetCursorPos: 1
    > GetCursorPos - Return: 1, x: 242, y: 379
    >
    > This would seem to indicate that the cursor moved but it didn't. If I
    > clicked on the button and then clicked on it again without moving the mouse
    > the debug statements produced:
    > GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    > GetCursorPos - Return: 1, x: 804, y: 654
    > SetCursorPos: 1
    > GetCursorPos - Return: 1, x: 242, y: 379
    >
    > GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    > GetCursorPos - Return: 1, x: 804, y: 654
    > SetCursorPos: 1
    > GetCursorPos - Return: 1, x: 242, y: 379
    >
    > As you can see from the above the second time the button was clicked the
    > system thought the cursor was back in the original position.
    >
    > What did I miss?
    > .
    >
     
    Daniel Pineault, May 21, 2010
    #2
    1. Advertisements

  3. Stewart Berman

    Jon Lewis Guest

    This works fine for me and sets the cursor position to the top left of the
    form as intended. You didn't post your RECT and POINT type declarations
    though so check these are correct. Other than that there must be something
    else going on causing the effect you're getting. Is there any other code in
    your command button?

    Jon



    "Stewart Berman" <> wrote in message
    news:...
    > Access 2007
    >
    > I want to be able to move the cursor using VBA code. I put the following
    > in
    > a module:
    >
    > Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As
    > Long,
    > lpRect As RECT) As Long
    > Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT)
    > As
    > Long
    > Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
    > ByVal y As Long) As Long
    > Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
    > Dim lReturn As Long
    > Dim pnt As POINT
    > Dim rec As RECT
    > lReturn = GetWindowRect(vlHwnd, rec)
    > Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
    > rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
    > lReturn = GetCursorPos(pnt)
    > Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    > y: " & pnt.y
    > lReturn = SetCursorPos(rec.Left, rec.Top)
    > Debug.Print "SetCursorPos: " & lReturn
    > lReturn = GetCursorPos(pnt)
    > Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    > y: " & pnt.y
    > MoveMouseToTopLeftCorner = (0 <> lReturn)
    > End Function
    >
    > In the click event of a button on a form I put:
    > MoveMouseToTopLeftCorner Me.hwnd
    >
    > I then opened the form and clicked on the button. The cursor changed from
    > a
    > hand to an arrow but it did not move.
    >
    > The Debug statements produced:
    > GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    > GetCursorPos - Return: 1, x: 824, y: 657
    > SetCursorPos: 1
    > GetCursorPos - Return: 1, x: 242, y: 379
    >
    > This would seem to indicate that the cursor moved but it didn't. If I
    > clicked on the button and then clicked on it again without moving the
    > mouse
    > the debug statements produced:
    > GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    > GetCursorPos - Return: 1, x: 804, y: 654
    > SetCursorPos: 1
    > GetCursorPos - Return: 1, x: 242, y: 379
    >
    > GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    > GetCursorPos - Return: 1, x: 804, y: 654
    > SetCursorPos: 1
    > GetCursorPos - Return: 1, x: 242, y: 379
    >
    > As you can see from the above the second time the button was clicked the
    > system thought the cursor was back in the original position.
    >
    > What did I miss?
     
    Jon Lewis, May 21, 2010
    #3
  4. The definitions are:
    Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type

    Private Type POINT
    x As Long
    y As Long
    End Type

    If it works for you then it is probably my environment that is causing the
    problem:
    Windows Ultimate X64 running in a VirtualBox virtual machine.

    I will pursue it with Oracle.

    "Jon Lewis" <> wrote:

    >This works fine for me and sets the cursor position to the top left of the
    >form as intended. You didn't post your RECT and POINT type declarations
    >though so check these are correct. Other than that there must be something
    >else going on causing the effect you're getting. Is there any other code in
    >your command button?
    >
    >Jon
    >
    >
    >
    >"Stewart Berman" <> wrote in message
    >news:...
    >> Access 2007
    >>
    >> I want to be able to move the cursor using VBA code. I put the following
    >> in
    >> a module:
    >>
    >> Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As
    >> Long,
    >> lpRect As RECT) As Long
    >> Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT)
    >> As
    >> Long
    >> Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
    >> ByVal y As Long) As Long
    >> Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
    >> Dim lReturn As Long
    >> Dim pnt As POINT
    >> Dim rec As RECT
    >> lReturn = GetWindowRect(vlHwnd, rec)
    >> Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
    >> rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
    >> lReturn = GetCursorPos(pnt)
    >> Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    >> y: " & pnt.y
    >> lReturn = SetCursorPos(rec.Left, rec.Top)
    >> Debug.Print "SetCursorPos: " & lReturn
    >> lReturn = GetCursorPos(pnt)
    >> Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    >> y: " & pnt.y
    >> MoveMouseToTopLeftCorner = (0 <> lReturn)
    >> End Function
    >>
    >> In the click event of a button on a form I put:
    >> MoveMouseToTopLeftCorner Me.hwnd
    >>
    >> I then opened the form and clicked on the button. The cursor changed from
    >> a
    >> hand to an arrow but it did not move.
    >>
    >> The Debug statements produced:
    >> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    >> GetCursorPos - Return: 1, x: 824, y: 657
    >> SetCursorPos: 1
    >> GetCursorPos - Return: 1, x: 242, y: 379
    >>
    >> This would seem to indicate that the cursor moved but it didn't. If I
    >> clicked on the button and then clicked on it again without moving the
    >> mouse
    >> the debug statements produced:
    >> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    >> GetCursorPos - Return: 1, x: 804, y: 654
    >> SetCursorPos: 1
    >> GetCursorPos - Return: 1, x: 242, y: 379
    >>
    >> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    >> GetCursorPos - Return: 1, x: 804, y: 654
    >> SetCursorPos: 1
    >> GetCursorPos - Return: 1, x: 242, y: 379
    >>
    >> As you can see from the above the second time the button was clicked the
    >> system thought the cursor was back in the original position.
    >>
    >> What did I miss?

    >
     
    Stewart Berman, May 21, 2010
    #4
  5. It turned out to be the way the guest add-ins (integration features) work in
    VirtualBox. It is a security issue since the mouse in the VM is the same as
    the mouse on the host. This is when running in the VirtualBox GUI on the
    host. It is not an issue when using remote desktop to connect the VM and
    the code works in that environment.

    "Jon Lewis" <> wrote:

    >This works fine for me and sets the cursor position to the top left of the
    >form as intended. You didn't post your RECT and POINT type declarations
    >though so check these are correct. Other than that there must be something
    >else going on causing the effect you're getting. Is there any other code in
    >your command button?
    >
    >Jon
    >
    >
    >
    >"Stewart Berman" <> wrote in message
    >news:...
    >> Access 2007
    >>
    >> I want to be able to move the cursor using VBA code. I put the following
    >> in
    >> a module:
    >>
    >> Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As
    >> Long,
    >> lpRect As RECT) As Long
    >> Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT)
    >> As
    >> Long
    >> Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
    >> ByVal y As Long) As Long
    >> Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
    >> Dim lReturn As Long
    >> Dim pnt As POINT
    >> Dim rec As RECT
    >> lReturn = GetWindowRect(vlHwnd, rec)
    >> Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
    >> rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
    >> lReturn = GetCursorPos(pnt)
    >> Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    >> y: " & pnt.y
    >> lReturn = SetCursorPos(rec.Left, rec.Top)
    >> Debug.Print "SetCursorPos: " & lReturn
    >> lReturn = GetCursorPos(pnt)
    >> Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
    >> y: " & pnt.y
    >> MoveMouseToTopLeftCorner = (0 <> lReturn)
    >> End Function
    >>
    >> In the click event of a button on a form I put:
    >> MoveMouseToTopLeftCorner Me.hwnd
    >>
    >> I then opened the form and clicked on the button. The cursor changed from
    >> a
    >> hand to an arrow but it did not move.
    >>
    >> The Debug statements produced:
    >> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    >> GetCursorPos - Return: 1, x: 824, y: 657
    >> SetCursorPos: 1
    >> GetCursorPos - Return: 1, x: 242, y: 379
    >>
    >> This would seem to indicate that the cursor moved but it didn't. If I
    >> clicked on the button and then clicked on it again without moving the
    >> mouse
    >> the debug statements produced:
    >> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    >> GetCursorPos - Return: 1, x: 804, y: 654
    >> SetCursorPos: 1
    >> GetCursorPos - Return: 1, x: 242, y: 379
    >>
    >> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
    >> GetCursorPos - Return: 1, x: 804, y: 654
    >> SetCursorPos: 1
    >> GetCursorPos - Return: 1, x: 242, y: 379
    >>
    >> As you can see from the above the second time the button was clicked the
    >> system thought the cursor was back in the original position.
    >>
    >> What did I miss?

    >
     
    Stewart Berman, May 21, 2010
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad

    Moving Scroll Bar in VB code

    Brad, Mar 1, 2004, in forum: Microsoft Access Form Coding
    Replies:
    5
    Views:
    260
  2. Lee

    Moving cursor to beginning of textbox

    Lee, Mar 22, 2004, in forum: Microsoft Access Form Coding
    Replies:
    1
    Views:
    192
  3. Mary Bowman via AccessMonster.com

    Subform cursor not changing/moving

    Mary Bowman via AccessMonster.com, Apr 29, 2005, in forum: Microsoft Access Form Coding
    Replies:
    1
    Views:
    149
    Guest
    May 1, 2005
  4. Guest

    Moving Cursor

    Guest, May 18, 2005, in forum: Microsoft Access Form Coding
    Replies:
    3
    Views:
    151
    Guest
    May 18, 2005
  5. magmike

    Moving cursor to field on error

    magmike, Feb 3, 2006, in forum: Microsoft Access Form Coding
    Replies:
    6
    Views:
    157
    magmike
    Feb 6, 2006
Loading...

Share This Page