Solved

Progess bar in a file loading process

Posted on 2000-03-30
6
153 Views
Last Modified: 2012-05-07
VB6:

Hello experts. I posted a question about loading big files. Here's another related one, which I choose to make its own question of.

I'm making a program which loads (sometimes) big textfiles into a textbox. Now I want to add a progressbar to the form, to make users feel better when loading big files.

The question is: How can add a progressbar to the file loading? That is, what parameter should the bar be connected to, and what is the maximum value?

Here's the code I use for importing the files:

    CommonDialog1.ShowOpen
    sFile = CommonDialog1.FileName
    If sFile <> "" Then
        iFn = FreeFile
        Open sFile For Input As #iFn
        Do While Not EOF(iFn)
            Line Input #iFn, sLine
            txtInput = txtInput & sLine & vbCrLf
        Loop
        Close #iFn
    End If
0
Comment
Question by:liljegren
6 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2671553
if youre reading the lines in then all you need is to find out how many lines you have and divide it by how many you have read in thus far and get ther percentage complete to display in a progress bar.  This code will tell you how many lines are in your file:

Function Count_Lines_In_File (ByVal strFilePath As String) As Integer
                    'delcare variables
                    Dim fileFile As Integer
                    Dim intLinesReadCount As Integer
                    intLinesReadCount = 0
                    'open file
                    fileFile = FreeFile


                    If (File_Exists(strFilePath)) Then
                        Open strFilePath For Input As fileFile
                    Else
                        'file doesn't exist
                        MsgBox "File: " & strFilePath & " hasn't been downloaded yet. Preprocessing is being aborted.", MB_OK, "File Does Not Exist"
                        Count_Lines_In_File = -1
                        Exit Function
                    End If
                    'loop through file
                    Dim strBuffer As String


                    Do While Not EOF(fileFile)
                        'read line
                        Input #fileFile, strBuffer
                        'update count
                        intLinesReadCount = intLinesReadCount + 1
                    Loop
                    'close file
                    Close fileFile
                    'return value
                    Count_Lines_In_File = intLinesReadCount
                End Function

0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 200 total points
ID: 2671557
Here is a simple solution:

Private Sub Command1_Click()
Dim iFN As Integer
Dim sFile As String
Dim sLine As String

    txtInput = ""
    CommonDialog1.ShowOpen
    sFile = CommonDialog1.FileName
    If sFile <> "" Then
        iFN = FreeFile
        Open sFile For Input As #iFN
        ProgressBar1.Min = 0
        ProgressBar1.Max = LOF(iFN)
        Do While Not EOF(iFN)
            Line Input #iFN, sLine
            txtInput = txtInput & sLine & vbCrLf
            ProgressBar1.Value = Len(txtInput)
        Loop
        Close #iFN
    End If

End Sub
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2671644
If you pull the entire file into the text box in one go you'll not be able to show a progress bar as you're only performing one INPUT statement and all of delay is occurring there.

Me, I'd opt for one big file pull and just pop a "Please wait, loading". Message while things run.

M
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:liljegren
ID: 2671689
I was afraid of something like that too, mark2150, but fortunately you are wrong in this case. :-) emoreau's solution was the best answer I could get. Thank you all.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2671706
Emoreau:
ProgressBar1.Value = Len(txtInput)
...
ProgressBar1.Value = Len(txtInput) _
     + ProgressBar1.Value
...
<smile> and a <wink>



       
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2673080
Doing it as one big load will be faster than doing a series of Line Input statements, esp. with a *big* file. For me, I'd rather have a static message displayed for a shorter period of time than a dynamic message displayed for a longer time.

Also, be sure to make the textbox.visible = false to keep the system from attempting to redraw the box as the data comes in. This too, will greatly slow the process down.

Since the two approaches are easy to code, why not write it both ways and *time* them loading the same large file. I think you may be surprised at the time difference.

M
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now