Ok, I am not sure if there is a way to do this or not based on how Access works. I need to get a progress bar to run Asynchronously while a Query is being loaded if this is possible.
I have a macro that runs as soon as Access is opened each time, which pulls in 3 SharePoint linked lists, unions them into a temp table, deletes all records from the current union Table(since I couldn't delete it because it has relationships to the other tables), copies the temp table to the union table and then deletes the temp table.
I created a progress bar based on this: https://www.youtube.com/watch?v=0Sqc...ature=youtu.be
I adjusted my code to fit what I am doing, and I have tried putting it in the Form_Open, Form_Load and finally the Form_Current events. However, none of them work properly. What happens is the progress bar updates to 100% while the form is not showing, then it shows and then it runs the query, which takes some time.
I tried putting the query inside the progress bar loop with a RunOnce Boolean flag on it, but still no luck. Progress Bar hits 100% before it even shows on the screen, and then everything else runs.
Is there a way to get this to work properly? I want Access to open, the Progress Bar Form to fully load and be visible before starting the progress bar, and while the progress bar is running, I want the Query to be updating, hopefully finishing close together.
Here is the modified code I have:
This is on AccessOpen which is what I named the AutoExec script that runs as soon as Access is opened:
Code:
'This runs as soon as Access opens and pulls the LoggedInUser information.
Public Function OnDBOpen()
Const sSource As String = "Function OnDBOpen()"
On Error GoTo ErrorHandler
'open the Progress Bar form
DoCmd.OpenForm "Progress Bar", acNormal, , , , acWindowNormal
'Get the user
Call GetUser
'If this is not an admin, then they will be unable to view the DB instances, etc...only the Form itself---DB is set to Run-Access mode
DoCmd.OpenForm "Form2", acNormal
ErrorHandler:
If Err.Number > 0 Then
Call DisplayError(Err.Number, Err.Description, msModule, sSource)
End If
End Function
This is in the Progress Bar Form:
Code:
Option Compare Database
Option Explicit
Const msModule As String = "Form_Progress Bar"
Dim holdPercComplete As Single
Private Sub Form_Current()
Const sSource As String = "Sub Form_Current()"
Const lngTotal As Long = 120000
Dim lngItem As Long
Dim RunOnce As Boolean
On Error GoTo ErrorHandler
Call UpdateProgress(0, 0, "Idle")
Do While lngItem <= lngTotal
lngItem = lngItem + 1
Debug.Print lngItem
If lngItem < 70000 Then
Call UpdateProgress(lngItem, lngTotal, "Please Wait...Loading...")
If lngItem = 20000 And Not RunOnce Then
Call UnionTable
RunOnce = False
DoEvents
End If
ElseIf lngItem < 95000 Then
Call UpdateProgress(lngItem, lngTotal, "Just a few more seconds...")
Else
Call UpdateProgress(lngItem, lngTotal, "Almost finished...")
End If
Loop
Call UpdateProgress(lngItem, lngTotal, "Task complete")
ErrorHandler:
If Err.Number > 0 Then
Call DisplayError(Err.Number, Err.Description, msModule, sSource)
End If
End Sub
Private Sub UpdateProgress(CurrentItem As Long, TotalItems As Long, taskName As String)
Const sSource As String = "UpdateProgress(CurrentItem As Long, TotalItems As Long, taskName As String)"
Dim PercComplete As Single
Dim intWidth As Integer
Dim nameArr() As Variant
Dim valArr() As Variant
On Error GoTo ErrorHandler
Me.lblCurrentTask.Caption = taskName
nameArr = Array("CurrentItem", "TotalItems", "taskName")
valArr = Array(CurrentItem, TotalItems, taskName)
'Validate data
If CurrentItem <= 0 Or TotalItems <= 0 Then
imgProgress.Width = 0
Exit Sub
End If
'Calculate the percentage complete
PercComplete = CurrentItem / TotalItems
If Int(PercComplete * 100) = Int(holdPercComplete * 100) Then
Exit Sub
End If
'Save it for comparison
holdPercComplete = PercComplete
'Calculate how wide to make the progress bar
If (PercComplete * 100) Mod 5 = 0 Then
intWidth = (BoxProgress.Width * PercComplete)
imgProgress.Width = intWidth
DoEvents 'or Me.Repaint
End If
ErrorHandler:
If Err.Number > 0 Then
Call DisplayError(Err.Number, Err.Description, msModule, sSource, nameArr, valArr)
End If
End Sub
And here is the UnionTable Sub that calls the long running query:
Code:
Option Compare Database
Option Explicit
Const msModule As String = "UnionTables"
'This will be used to programatically drop and create a new union table each time the user opens the program
'to ensure they have the latest sharepoint data.
Public Sub UnionTable()
Const sSource As String = "Sub UnionTable()"
Dim SQL As String
On Error GoTo ErrorHandler
'Create the SQL Statement to select all records from the 3 tables, union them and combine them into a new table
'we have to create a Temp Table to do this since the UnionT table has relationships and cannot be deleted
SQL = "Proprietary Info---3 Union Commands with on a bunch of columns"
'Turn off the prompts
DoCmd.SetWarnings False
'Run the SQL Command
DoCmd.RunSQL SQL
'Delete All rows in UnionT Table
DoCmd.RunSQL "DELETE * FROM UnionT"
'Insert all rows from the TempTable into Union T
DoCmd.RunSQL "INSERT INTO UnionT SELECT * FROM TempTable"
'Delete the duplicate Temp Table
DoCmd.RunSQL "DROP TABLE TempTable"
'Reset the prompts
DoCmd.SetWarnings True
ErrorHandler:
If Err.Number > 0 Then
Call DisplayError(Err.Number, Err.Description, msModule, sSource)
End If
End Sub