Moving command buttons for April Fools

B

B. Meincke

What I want to do for April Fools day is reprogram all the command buttons on
our attendance database switchboard such that when a teacher moves the mouse
over it, it moves. I have the following:

Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Command0.Left = Command0.Left + ((1 + Int(500 * Rnd())))
Command0.Top = Command0.Top + ((1 + Int(500 * Rnd())))
End Sub

This works beautifully, until the button reaches the bottom or right-hand
side of the form, then, of course, I get an error message as the Left or Top
value becomes invalid.

Is there any way I can revise this code so that if the button hits the wall,
so to speak, it will turn around and go the other way, or make the button
movement completely random as opposed to it moving only down and to the right?

Thanks for any help in advance.

And...shhh! <G>
 
J

John W. Vinson

What I want to do for April Fools day is reprogram all the command buttons on
our attendance database switchboard such that when a teacher moves the mouse
over it, it moves. I have the following:

Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Command0.Left = Command0.Left + ((1 + Int(500 * Rnd())))
Command0.Top = Command0.Top + ((1 + Int(500 * Rnd())))
End Sub

This works beautifully, until the button reaches the bottom or right-hand
side of the form, then, of course, I get an error message as the Left or Top
value becomes invalid.

Is there any way I can revise this code so that if the button hits the wall,
so to speak, it will turn around and go the other way, or make the button
movement completely random as opposed to it moving only down and to the right?

You're a cruel person...

First off, have it move either in a positive or negative direction: instead of
(1 + Int(500 * Rnd())) use (Int(1000 * Rnd() - 500) in both places.

To catch hitting the wall, trap the error and just retry:

Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
On Error GoTo Proc_Error
Dim iMoveX As Integer
Dim iMoveY As Integer
iMoveX = Int(1000 * Rnd()) - 500
iMoveY = Int(1000 * Rnd()) - 500
again:
Command0.Left = Command0.Left + iMoveX
Command0.Top = Command0.Top + iMoveY
Proc_Exit:
Exit Sub
Proc_Error:
Select Case Err.Number
Case 2100
iMoveX = -iMoveX
iMoveY = -iMoveY
Resume again
Case Else
MsgBox "Error " & Err.Number
Resume Proc_Exit
End Select
End Sub
 
L

Larry Linson

In every work environment where I worked in a 50+ year career, management
would have determined who created havoc in the workplace, and that person
would have been summarily dismissed. Good luck! I have a feeling you're
going to need it -- what you're considering seems a lot funnier in prospect
than people not being able to accomplish their work turns out to be.

Larry Linson
Microsoft Office Access MVP
 
B

B. Meincke

John W. Vinson said:
You're a cruel person...

LOL...

You many very well be right, but does the code below not then make you an
accessory to cruelty? <G>

Seriously:
Thank you, John. Your code works like a charm.

And I promise I'll have the database back to normal before the morning
announcements play out.
 
J

John W. Vinson

In every work environment where I worked in a 50+ year career, management
would have determined who created havoc in the workplace, and that person
would have been summarily dismissed. Good luck! I have a feeling you're
going to need it -- what you're considering seems a lot funnier in prospect
than people not being able to accomplish their work turns out to be.

Larry Linson
Microsoft Office Access MVP

<Tab> key will still work fine... maybe the users should be warned of this
though.

I'll watch out for you at the Summit, Larry... especially if you're carrying a
wet blanket! <g>
 
B

B. Meincke

Just to let you know, Larry, my "prank" was a success.

With the *invaluable help* of the folks who monitor this newsgroup, I
managed to accomplish what I wanted to do and the results were seen in the
nature of fun they were intended.

There was no loss in productivity due to searching for a culprit, as I am
the only one who manages the database programming. <G>

But thank you for your concern anyway.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top