0

This may seem like a very odd thing to do in VBA but I'm curious so hear me out. I created a procedure that moves an image across the screen when it's clicked. This works, albeit alright, but still - it works.

Here is the code:

'Start primitive animation
Private Sub imgBean_Click()

    'Limit for movement to right
    Dim coordinates, limit As Integer
    coordinates = 0
    limit = Form.Width

    Do While coordinates < limit

        coordinates = coordinates + 5

        'Move the image
        Me.imgBean.Move coordinates

        'Needed to add this to see the image move - updates the form
        Form.Repaint

    Loop

    Debug.Print "Coordinates " & coordinates
    Debug.Print limit

    'Reset the limit
    limit = 0

End Sub

As said before the code works - but while the image is moving the screen is frozen i.e. I can't close the form or interact with other components. (This is similar to blocking the UI thread in the Android environment - something that you never do!)

So is there a way to avoid this?

Thanks

P.S. Setting the focus to the form makes the image sporadically appear and disappear.

8
  • Let's be honest there, Microsoft Access isn't especially recognize for his multitasking capabilities. I'm having the same problem with most my VBA applications (whether it's Access, Excel or Outlook). Commented May 16, 2013 at 14:01
  • @dnLL I know but thought I'd chance it :P Commented May 16, 2013 at 14:16
  • multi-threading isnt supported by excel Commented May 16, 2013 at 14:47
  • @mehow I'm not using Excel - I know it isn't supported in VBA so I'm looking into other means - came across something similar (to multithreading) which leverages VBScript, though not sure if it's appropriate for what I want - seems it's more suited to web scraping and stuff of that nature Commented May 16, 2013 at 14:54
  • 1
    Have you tried adding a DoEvents statement immediately after you enter the Do While loop? It's usually used to enable interaction with "other things" while a tight loop is being executed, but it might loosen things up in this case, too. Details here, and be sure to read the "Problems Associated with DoEvents" section near the end of that article. Commented May 16, 2013 at 15:59

1 Answer 1

0

Turns out I couldn't find a way to visually prevent interaction with the GUI once the animation has started. I said visually because it is possible to click buttons and enter input while the animation is running.

It involved the use of the DoEvents command which acts as a way of multitasking in MS Access. In mine I added the command above the Do-While loop (see below):

'Essentially allow multitasking
DoEvents

Do While coordinates < limit

    coordinates = coordinates + 5

    'Move the image
    Me.imgBean.Move coordinates
    Form.Repaint

Loop

It should be noted that it didn't work when I placed it in the Do-While loop. So, when I started the animation by clicking on the image I could still press the other buttons because I entered a value one one field and clicked a button to convert it to another - while the animation is running.

The only question is how to do this in a visual sense.

Sign up to request clarification or add additional context in comments.

1 Comment

As I've said above, DoEvents isn't really reliable, so be careful when using it and be sure the application can still work without it. If you want to give the OS a little bit more time, you could loop on DoEvents (if it really helps, because in some case it just does nothing).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.