How to display progress of a running query


I am running a query in access database. The query takes about 1 minute to execute. during this time I want to show the user the progress of the query in a progress bar, otherwise the user thinks as the application stopped responding.

Is there any way to do this? please help. I am very new to .NET

the code I am using is

        Dim qstr, strWhere, buildings As String
        Dim reportDS As New DataTable
        Dim dbconnection As New OleDbConnection(connectionString)
        Dim Adapter As New OleDbDataAdapter

        buildings = GetBuildingSelection()
        strWhere = " WHERE (((ACCOUNT) = " + Str(account_no) + ") AND ((BUILDINGNUM) In (" + buildings + ")))"

        qstr = "SELECT * FROM qryDisposalCost" + strWhere

        Adapter.SelectCommand = New OleDbCommand(qstr, dbconnection)
        Adapter.Fill(reportDS)   'This line takes about 1 minute to finish

LVL 11
Who is Participating?
RonaldBiemansConnect With a Mentor Commented:
I don't think you can do this during a Fill, why don't you just show a small splash screen before the fill and close it after the fill.
bansidharAuthor Commented:
that i am doing.
But because the query takes about a minute or more, and it uses hevy resources the user thinks it as a system hang and often reboots the machine.

and he is not allowing me to optimize the database and querries.

Hi bansidhar,

Here is some code that I use for a splash screen with a timer. You can adjust the timer to  that one minute that you need.

Public Class frmSplash
    Inherits System.Windows.Forms.Form
    Public Shared frmMain As New frmMain
#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents StartTimer As System.Windows.Forms.Timer
    Friend WithEvents LinkLabel As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmSplash))
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.StartTimer = New System.Windows.Forms.Timer(Me.components)
        Me.LinkLabel = New System.Windows.Forms.Label
        Me.ProgressBar1.Location = New System.Drawing.Point(8, 280)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(144, 16)
        Me.ProgressBar1.TabIndex = 0
        Me.Timer1.Interval = 300
        Me.LinkLabel.BackColor = System.Drawing.Color.Transparent
        Me.LinkLabel.ForeColor = System.Drawing.Color.White
        Me.LinkLabel.Location = New System.Drawing.Point(372, 280)
        Me.LinkLabel.Name = "LinkLabel"
        Me.LinkLabel.Size = New System.Drawing.Size(156, 16)
        Me.LinkLabel.TabIndex = 1
        Me.LinkLabel.Text = "Please Wait..... Form Loading"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.White
        Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
        Me.ClientSize = New System.Drawing.Size(528, 298)
        Me.ControlBox = False
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmSplash"
        Me.Opacity = 0
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.TopMost = True

    End Sub

#End Region
    Private Sub beginLoadingComponents()
        Me.LinkLabel.Text = "Loading Main Form"
        Me.Timer1.Enabled = True
        Me.ProgressBar1.Visible = True
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.ProgressBar1.Value += Me.ProgressBar1.Maximum / 10
        If Me.ProgressBar1.Value >= 90 Then
            Me.LinkLabel.Text = "Main Form loaded"
        End If
        If Me.ProgressBar1.Value >= Me.ProgressBar1.Maximum Then
            Timer1.Enabled = False
            Me.Visible = False
            frmMain.ShowDialog()    '<--- show your main form
            Application.Exit()      '<--- exit
        End If
    End Sub

    Dim elapsed As Short = 0
    Private Sub StartTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartTimer.Tick
        elapsed += 1
        Me.Opacity = elapsed * 0.1
        If elapsed >= 10 Then
            Me.LinkLabel.Visible = True
            Me.StartTimer.Enabled = False
        End If
    End Sub

    Private Sub frmSplash_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.StartTimer.Enabled = True
    End Sub
End Class
bansidharAuthor Commented:
I think I have to run the query and the progress bar in different threads, because during the query running no other events are handled by the application.

 I may also have to calculate the query speed (network connectivity speed) of the target machine to calculate the increments and max value of the progress bar.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.