Progress bar in MS Access
Asked Answered
A

7

28

I have a query running in Microsoft Access 2010 and it takes over 30 minutes to run normally. I would like to present the end user with some status of the query. A progress bar would be nice but not required. Access seems to be poorly threaded and locks up tight during the execution of the query, negating any updates I try. While I'd rather whip out VS and write my own app to do this, I'm forced to use Access.

I used to run this from a batch script which populated the database but I'd like to have it all self-contained in Access. To be specific, the "query" is really a VBA script that pings a series of hosts. So I'm not too concerned about optimizing the time per se but simply about letting the end user know it hasn't locked up.

Afield answered 14/8, 2012 at 16:35 Comment(5)
As I recall Access starts to have issues with mid sized databases (~100,000 records) or so.Moulden
There is a chance that the query could be speeded up if you post the sql. 30 mins is unusual.Unrealizable
@Moulden You are thinking of the far past. Any database has issues if it can't make use of indexes, for example. Access is fine with a mere 100,000 records, unless they are very large records indeed.Unrealizable
Yes, Remou. Please show us your query Menefee!Firewater
You can't add a progress bar to the process of running a single query, because it's an "atomic" action in Access. BTW, optimizing can sometimes also be done by splitting a big query into some smaller queries, which may in sum need less memory.Inelastic
F
38

I often do something like this

Dim n As Long, db As DAO.Database, rs As DAO.Recordset

'Show the hour glass
DoCmd.Hourglass True

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT ...")

rs.MoveLast 'Needed to get the accurate number of records

'Show the progress bar
SysCmd acSysCmdInitMeter, "working...", rs.RecordCount

rs.MoveFirst
Do Until rs.EOF
    'Do the work here ...

    'Update the progress bar
    n = n + 1
    SysCmd acSysCmdUpdateMeter, n

    'Keep the application responding (optional)
    DoEvents

    rs.MoveNext
Loop
rs.Close: Set rs = Nothing
db.Close: Set db = Nothing

'Remove the progress bar
SysCmd acSysCmdRemoveMeter

'Show the normal cursor again
DoCmd.Hourglass False

Note: Of course you must do the work programmatically for this to work. You cannot watch a runnging query in code or the like in Access. Possibly you could split the work of your slow query into smaller pieces, in order to get the chance of updating a progress bar. But you can always show the hour glass; this tells the user that something is happening.

Firewater answered 14/8, 2012 at 16:52 Comment(3)
And I guess that is precisely the problem. I'd like to know the progress of the current query but the JET DB engine doesn't update the parent thread so it seems impossible. Hoping I'm wrong here...Afield
No, unfortunately you are right. Maybe someone can improve your query, if you show the SQL.Firewater
If the query is affecting a large number of rows then you can improve this method by using a transaction buffer and committing the recordset changes just once at the end.Noreen
I
26

In case others might find this useful, here is a class I wrote for this purpose. I use it all the time in my Access development projects. Just drop it into your project in a class module called clsLblProg, and use it like this:

enter image description here

This produces a nice little progress bar:

enter image description here

On your form, all you need is three labels. Set the back label to the desired size and make the other two hidden. The class does the rest.

enter image description here

And here is the code for clsLblProg:

Option Compare Database
Option Explicit

' By Adam Waller
' Last Modified:  12/16/05

'Private Const sngOffset As Single = 1.5    ' For Excel
Private Const sngOffset As Single = 15      ' For Access

Private mdblMax As Double   ' max value of progress bar
Private mdblVal As Double   ' current value of progress bar
Private mdblFullWidth As Double ' width of front label at 100%
Private mdblIncSize As Double
Private mblnHideCap As Boolean  ' display percent complete
Private mobjParent As Object    ' parent of back label
Private mlblBack As Access.Label     ' existing label for back
Private mlblFront As Access.Label   ' label created for front
Private mlblCaption As Access.Label ' progress bar caption
Private mdteLastUpdate As Date      ' Time last updated
Private mblnNotSmooth As Boolean    ' Display smooth bar by doevents after every update.

' This class displays a progress bar created
' from 3 labels.
' to use, just add a label to your form,
' and use this back label to position the
' progress bar.

Public Sub Initialize(BackLabel As Access.Label, FrontLabel As Access.Label, CaptionLabel As Access.Label)

    On Error GoTo 0    ' Debug Mode


    Dim objParent As Object ' could be a form or tab control
    Dim frm As Form

    Set mobjParent = BackLabel.Parent
    ' set private variables
    Set mlblBack = BackLabel
    Set mlblFront = FrontLabel
    Set mlblCaption = CaptionLabel

    ' set properties for back label
    With mlblBack
        .Visible = True
        .SpecialEffect = 2  ' sunken. Seems to lose when not visible.
    End With

    ' set properties for front label
    With mlblFront
        mdblFullWidth = mlblBack.Width - (sngOffset * 2)
        .Left = mlblBack.Left + sngOffset
        .Top = mlblBack.Top + sngOffset
        .Width = 0
        .Height = mlblBack.Height - (sngOffset * 2)
        .Caption = ""
        .BackColor = 8388608
        .BackStyle = 1
        .Visible = True
    End With

    ' set properties for caption label
    With mlblCaption
        .Left = mlblBack.Left + 2
        .Top = mlblBack.Top + 2
        .Width = mlblBack.Width - 4
        .Height = mlblBack.Height - 4
        .TextAlign = 2 'fmTextAlignCenter
        .BackStyle = 0 'fmBackStyleTransparent
        .Caption = "0%"
        .Visible = Not Me.HideCaption
        .ForeColor = 16777215   ' white
    End With
    'Stop

    Exit Sub

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Initialize", Erl
            Resume Next ' Resume at next line.
    End Select

End Sub

Private Sub Class_Terminate()

    On Error GoTo 0    ' Debug Mode

    On Error Resume Next
    mlblFront.Visible = False
    mlblCaption.Visible = False
    On Error GoTo 0    ' Debug Mode

    Exit Sub

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Class_Terminate", Erl
            Resume Next ' Resume at next line.
    End Select

End Sub

Public Property Get Max() As Double

    On Error GoTo 0    ' Debug Mode

    Max = mdblMax

    Exit Property

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Max", Erl
            Resume Next ' Resume at next line.
    End Select

End Property

Public Property Let Max(ByVal dblMax As Double)

    On Error GoTo 0    ' Debug Mode

    mdblMax = dblMax

    Exit Property

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Max", Erl
            Resume Next ' Resume at next line.
    End Select

End Property

Public Property Get Value() As Double

    On Error GoTo 0    ' Debug Mode

    Value = mdblVal

    Exit Property

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Value", Erl
            Resume Next ' Resume at next line.
    End Select

End Property

Public Property Let Value(ByVal dblVal As Double)

    On Error GoTo 0    ' Debug Mode

    'update only if change is => 1%
    If (CInt(dblVal * (100 / mdblMax))) > (CInt(mdblVal * (100 / mdblMax))) Then
        mdblVal = dblVal
        Update
    Else
        mdblVal = dblVal
    End If

    Exit Property

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Value", Erl
            Resume Next ' Resume at next line.
    End Select

End Property

Public Property Get IncrementSize() As Double

    On Error GoTo 0    ' Debug Mode

    IncrementSize = mdblIncSize

    Exit Property

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "IncrementSize", Erl
            Resume Next ' Resume at next line.
    End Select

End Property

Public Property Let IncrementSize(ByVal dblSize As Double)

    On Error GoTo 0    ' Debug Mode

    mdblIncSize = dblSize

    Exit Property

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "IncrementSize", Erl
            Resume Next ' Resume at next line.
    End Select

End Property

Public Property Get HideCaption() As Boolean

    On Error GoTo 0    ' Debug Mode

    HideCaption = mblnHideCap

    Exit Property

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "HideCaption", Erl
            Resume Next ' Resume at next line.
    End Select

End Property

Public Property Let HideCaption(ByVal blnHide As Boolean)

    On Error GoTo 0    ' Debug Mode

    mblnHideCap = blnHide

    Exit Property

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "HideCaption", Erl
            Resume Next ' Resume at next line.
    End Select

End Property

Private Sub Update()

    On Error GoTo 0    ' Debug Mode

    Dim intPercent As Integer
    Dim dblWidth As Double
    'On Error Resume Next
    intPercent = mdblVal * (100 / mdblMax)
    dblWidth = mdblVal * (mdblFullWidth / mdblMax)
    mlblFront.Width = dblWidth
    mlblCaption.Caption = intPercent & "%"
    'mlblFront.Parent.Repaint    ' may not be needed

    ' Use white or black, depending on progress
    If Me.Value > (Me.Max / 2) Then
        mlblCaption.ForeColor = 16777215   ' white
    Else
        mlblCaption.ForeColor = 0  ' black
    End If

    If mblnNotSmooth Then
        If mdteLastUpdate <> Now Then
            ' update every second.
            DoEvents
            mdteLastUpdate = Now
        End If
    Else
        DoEvents
    End If

    Exit Sub

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Update", Erl
            Resume Next ' Resume at next line.
    End Select

End Sub

Public Sub Increment()

    On Error GoTo 0    ' Debug Mode

    Dim dblVal As Double
    dblVal = Me.Value
    If dblVal < Me.Max Then
        Me.Value = dblVal + 1
        'Call Update
    End If

    Exit Sub

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Increment", Erl
            Resume Next ' Resume at next line.
    End Select

End Sub

Public Sub Clear()

    On Error GoTo 0    ' Debug Mode

    Call Class_Terminate

    Exit Sub

ErrHandler:

    Select Case Err.Number
        Case Else
            LogErr Err, "clsLblProg", "Clear", Erl
            Resume Next ' Resume at next line.
    End Select

End Sub

Private Function ParentForm(ctlControl As Control) As String

    ' returns the name of the parent form
    Dim objParent As Object

    Set objParent = ctlControl

    Do While Not TypeOf objParent Is Form
       Set objParent = objParent.Parent
    Loop

    ' Now we should have the parent form
    ParentForm = objParent.Name

End Function

Public Property Get Smooth() As Boolean
    ' Display the progress bar smoothly.
    ' True by default, this property allows the call
    ' to doevents after every increment.
    ' If False, it will only update once per second.
    ' (This may increase speed for fast progresses.)
    '
    ' negative to set default to true
    Smooth = mblnNotSmooth
End Property

Public Property Let Smooth(ByVal IsSmooth As Boolean)
    mblnNotSmooth = Not IsSmooth
End Property

Private Sub LogErr(objErr, strMod, strProc, intLine)
    ' For future use.
End Sub
Imprecation answered 30/1, 2015 at 20:34 Comment(3)
Nice class... Looks a bit old school but does the job. I confirm it works in MS Access 2010 - 32bitGilmore
where do I put the process? I'm sorry, it is not obvious to me, but where do I put my process - for example DoCmd.OpenQuery("LongQuery")Cordalia
@monty327 - Usually you would use this when you are doing a loop through code. If you have a single long query, you might need to take a little different approach in order to be able to use the progress bar. Hope that helps!Imprecation
B
4

Just adding my part to the above collection for future readers.

If you are after less code and maybe cool UI. Check out my GitHub for Progressbar for VBA enter image description here

a customisable one:

enter image description here

The Dll is thought for MS-Access but should work in all VBA platform with minor changes. All codes can be found in the sample database.

This project is currently under development and not all errors are covered. So expect some!

You should be worried about 3rd party dlls and if you are, please feel free to use any trusted online antivirus before implementing the dll.

Bulger answered 8/5, 2018 at 16:41 Comment(0)
T
2

Due to problems with available control I created a home grown progress bar using 2 rectangles. A border, and solid bar that is resized as things progress. The progress rectangle in in front of the border. To use

If pbar Is Nothing Then
    Set pbar = New pBar_sub
    pbar.init Me.Progressbar_border, Me.ProgressBar_Bar
End If
pbar.value = 0
pbar.show
pbar.max = 145 ' number of interations
...
...
Do While Not recset.EOF
    count = count + 1
    pbar.value = count
'   get next 
    recset.MoveNext
Loop

One can associate a status line with the progress bar that announces what element is being processed. Like: 123. District SomeWhere, sales agent WhomEver

======== Progress Bar substitute pBar_sub ==============

Option Compare Database
Option Explicit

Dim position    As Long
Dim maximum     As Long
Dim increment   As Single
Dim border      As Object
Dim bar         As Object

Sub init(rect As Object, b As Object)
    Set border = rect
    Set bar = b
    bar.width = 0
    hide
End Sub
Sub hide()
    bar.visible = False
    border.visible = False
End Sub
Sub show()
    bar.visible = True
    border.visible = True
End Sub
Property Get Max() As Integer
    Max = maximum
End Property
Property Let Max(val As Integer)
    maximum = val
    increment = border.width / val
End Property
Property Get value() As Integer
    value = position
End Property
Property Let value(val As Integer)
    position = val
    bar.width = increment * value
End Property
Taylor answered 4/10, 2013 at 9:39 Comment(0)
B
1

Use the command DoEvents after updating the progressbar (acSysCmdUpdateMeter).

In case of a large number of records only execute DoEvents every x times since this slows down your application a little.

Baryta answered 29/4, 2014 at 9:44 Comment(0)
C
0

This is not a professional way but can be applied if you like it. If you are using a form You can have a small text box in a convenient place default with Green color.

Supposing the text box Name is TxtProcessing,
Properties can be as below.

Name : TxtProcessing
Visible : Yes
Back color : Green
Locked: Yes
Enter Key Behavior : Default

1) In your VB script you can put Me.TxtProcessing.BackColor = vbRed which will be in Red and it signifies the task in Process.
2) you can write all your set of scripts
3) At last you can put Me.TxtProcessing.BackColor = vbGreen

Me.TxtProcessing.BackColor = vbRed
Me.TxtProcessing.SetFocus
Me.Refresh

Your Code here.....

Me.TxtProcessing.BackColor = vbGreen
Me.TxtProcessing.SetFocus

:-) Funny but purpose is achieved.

Cannabin answered 18/2, 2014 at 12:2 Comment(0)
D
-2

First drag progressive bar control in MS Access form, then change the name of progressive bar like aa.

Then go to form property, on timer :write in code

me.aa.value=me.aa.value+20

time interval 300 as per your choice. Run the form you will see the progressive bar

Depressant answered 6/9, 2014 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.