ProgressBar

I have this project with this form, I would like to add a progress bar, to show users how much longer it is going to take to log them in. How do I do this?


Private Sub Command1_Click()
Dim oNet, szMsg

    szMsg1 = "If this is the first time you are logging onto this computer, Please wait 3 seconds before clicking the OK button."

    Set WshShell = CreateObject("Wscript.Shell")
    ReturnCode = WshShell.Run("net use * /del /y", 7)
    Set WshShell = Nothing
   
   

    Set WshNetwork = CreateObject("WScript.Network")
    PrinterPath = "\\server02\223scscbnpl347"
    WshNetwork.AddWindowsPrinterConnection PrinterPath

    Set WshNetwork = CreateObject("WScript.Network")
    PrinterPath = "\\server02\223scscmkpl0us"
    WshNetwork.AddWindowsPrinterConnection PrinterPath

    Set WshNetwork = CreateObject("WScript.Network")
    PrinterPath = "\\server02\223scscmkpl0vv"
    WshNetwork.AddWindowsPrinterConnection PrinterPath

    Set WshNetwork = CreateObject("WScript.Network")
    PrinterPath = "\\server02\223scscbnpl0ue"
    WshNetwork.AddWindowsPrinterConnection PrinterPath

    Set WshNetwork = CreateObject("WScript.Network")
    PrinterPath = "\\server02\223scscbipl526"
    WshNetwork.AddWindowsPrinterConnection PrinterPath

    Set WshNetwork = CreateObject("WScript.Network")
    PrinterPath = "\\server02\223lglgsmpl0w9"
    WshNetwork.AddWindowsPrinterConnection PrinterPath

    Set WshNetwork = CreateObject("WScript.Network")
    PrinterPath = "\\server02\223dpdpmpl0wl"
    WshNetwork.AddWindowsPrinterConnection PrinterPath

   
    szMsg2 = "Printer 223scscbnpl347 has been added" & vbCrLf & _
             "" & vbCrLf & _
             "Printer 223scscmkpl0us has been added" & vbCrLf & _
             "" & vbCrLf & _
             "Printer 223scscmkpl0vv has been added" & vbCrLf & _
             "" & vbCrLf & _
             "Printer 223scscbnpl0ue has been added" & vbCrLf & _
             "" & vbCrLf & _
             "Printer 223scscbipl526 has been added" & vbCrLf & _
             "" & vbCrLf & _
             "Printer 223lglgsmpl0w9 has been added" & vbCrLf & _
             "" & vbCrLf & _
             "Printer 223dpdpmpl0wl has been added" & vbCrLf & _
             "" & vbCrLf

    Set oNet = CreateObject("WScript.Network")

    oNet.MapNetworkDrive "M:", "\\server01\groups", False
    oNet.MapNetworkDrive "N:", "\\server02\global", False
    oNet.MapNetworkDrive "O:", "\\server02\groups", False
    oNet.MapNetworkDrive "P:", "\\server02\" & oNet.UserName & "$", False
    oNet.MapNetworkDrive "Y:", "\\server01\global", False
    oNet.MapNetworkDrive "Z:", "\\server02\archive", False
 
    szMsg3 = "Drive M = server01\groups" & vbCrLf & _
             "" & vbCrLf & _
             "Drive N = server02\global" & vbCrLf & _
             "" & vbCrLf & _
             "Drive O = server02\groups" & vbCrLf & _
             "" & vbCrLf & _
             "Drive P = server02\Username" & vbCrLf & _
             "" & vbCrLf & _
             "Drive Y = server01\global" & vbCrLf & _
             "" & vbCrLf & _
             "Drive Z = server02\archive"
   
   
   
    szMsg4 = "Please wait for the verification of the 2005 Information Assurance Program, This can take upto 5 seconds to display." & vbCrLf
   
   
   
    MsgBox szMsg1
    MsgBox szMsg2
    MsgBox szMsg3
    MsgBox szMsg4
   
    If Not IsUserCurrent(oNet.UserName) Then
       szMsg5 = "" & oNet.UserName & " HAS FAILED VERIFICATION!!, Please contact your supervisor!!" & vbCrLf
       szMsg6 = "An Email notification has been sent to your Information Assurance Officer ( MSgt Kevin Hite ). You can now Exit the program " & vbCrLf
    Else
       szMsg5 = "" & oNet.UserName & " PASSED VERIFICATION!!" 'In case you're looping anywhere
       szMsg6 = "You can now Exit the program"
    End If

    If Len(szMsg5) > 0 Then
        MsgBox szMsg5
        MsgBox szMsg6
    End If

   
   
   
    Set oNet = Nothing
   

End Sub

Function IsUserCurrent(strUser)
    IsUserCurrent = False
    Dim xlApp, xlWB, xlWS
    Dim Row, User, UserDate, Temp
    Set xlApp = CreateObject("Excel.Application")
    Set xlWB = xlApp.Workbooks.Open("\\server02\ia$\223all.xls")  'Path to your Excel file
    Set xlWS = xlWB.Sheets("IA") 'whatever name of your sheet is
    Row = 1
    Do Until xlWS.Cells(Row, 1) = ""
       User = xlWS.Cells(Row, 1)
       If User = strUser Then
            Temp = xlWS.Cells(Row, 2)
            UserDate = DateSerial(Mid(Temp, 1, 4), Mid(Temp, 5, 2), Mid(Temp, 7, 2))
            If DateDiff("d", UserDate, Date) <= 365 Then
                IsUserCurrent = True
            End If

       End If
       Row = Row + 1


    Loop
    Set xlWS = Nothing
    xlWB.Close
    Set xlWB = Nothing
    xlApp.Quit
    Set xlApp = Nothing

End Function

Private Sub Command2_Click()
  Unload Me
End Sub

Private Sub ProgressBar1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

End Sub
CMILLERAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gtalley10Commented:
This site has a fairly easy to follow example of how to use a progress bar: http://www.codeguru.com/vb/controls/vb_activex/activex/article.php/c7121/

Basically, you add your progress bar to your form, then set the initial values for min, max, & value and run the value as a counter up to your max value to fill the as your loop runs.

'Set initial parameters.
ProgressBar1.Min=0
ProgressBar1.Max=  [max possible iterations of your loop, ie. max value of array, recordcount of a recordset, etc.]
ProgressBar1.Value=0

Do While ProgressBar1.Value<=ProgressBar1.Max
  'Insert code here for process to be followed with progress bar.
  ProgressBar1.Value=ProgressBar1.Value + 1
Loop

CMILLERAuthor Commented:
I looked over the web site, I cut and pasted your code in my project, one of the things it looks like I need to change, just not sure what to have the MAX set to? or am I missing something else?
CMILLERAuthor Commented:
Also, is this what it should be?

Private Sub ProgressBar1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
'Set initial parameters.
ProgressBar1.Min = 0
ProgressBar1.Max = [max possible iterations of your loop, ie. max value of array, recordcount of a recordset, etc.]
ProgressBar1.Value = 0

Do While ProgressBar1.Value <= ProgressBar1.Max
  'Insert code here for process to be followed with progress bar.
  ProgressBar1.Value = ProgressBar1.Value + 1
Loop

End Sub
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

JesterTooCommented:
Since there isn't really any way for your script to calculate how much work is to be performed, nor measure how much has been completed at any point, you will need to make those calculations yourself.  There appears to be several points in the code where you might want to update the percentage done... make a reasonable guess as to what percentage you think each point represents and just hardcode those values into a call to the ProgressBar routine.  During testing, observe how smoothly the bar progresses and refine any percentage estimates that are obviously out of whack.  Be aware that the same "task" won't always require the same amount of time depending on server and network activity.

CMILLERAuthor Commented:
In that case can I just measure the mapping of the printers and network drives?
gtalley10Commented:
For your mapping example you could just have max be the total number of printers/drives mapped and increment value up 1 after each one is mapped for a rough progress estimate. If a certain drive or printer generally takes a significantly different amount of time you could sandbag it's value to higher a increment and percentage of the total.
CMILLERAuthor Commented:
so in this code just change the max to say 13, is that all I need to change.
JesterTooCommented:
Yeah, give that a try and see how it works.  If the motion looks pretty "jerkey" try smoothing (what gtalley10 referred to as "sandbag") some of the values until you get a reasonable display.
CMILLERAuthor Commented:
it looks like it doesnt matter what I set the max to, the progress bar doesnt change at any point in the script

ProgressBar1.Min = 0
ProgressBar1.Max = 13
ProgressBar1.Value = 0
CMILLERAuthor Commented:
for the number 13, is it counting in the code haw many times something happens.

JesterTooCommented:
Tiyr code may be running so fast it doesn't give VB time to update the display... try inserting a DoEvents statement just before the Loop statement.
CMILLERAuthor Commented:
Do you mean like this?

Private Sub ProgressBar1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
'Set initial parameters.
ProgressBar1.Min = 0
ProgressBar1.Max = 13
ProgressBar1.Value = 0

Do While ProgressBar1.Value <= ProgressBar1.Max
  'Insert code here for process to be followed with progress bar.
  ProgressBar1.Value = ProgressBar1.Value + 1
DoEvents
Loop

End Sub
CMILLERAuthor Commented:
I have to leave for the weekend, I will pick this back up on Monday.

Thanks,
-Chris
jimbobmcgeeCommented:
>>  it looks like it doesnt matter what I set the max to, the progress bar doesnt change at any point in the script

At what point in your script do you do the following:

    ProgressBar1.Value = ProgressBar1.Value + 1

That's what you need in order to make the bar go up...

J.
jimbobmcgeeCommented:
Try these refinements to your printers routine:

    Set WshNetwork = CreateObject("WScript.Network")
    a_szPrinters = Array("\\server02\223scscbnpl347", "\\server02\223scscmkpl0us", _
                                  "\\server02\223scscmkpl0vv", "\\server02\223scscbnpl0ue", _
                                  "\\server02\223scscbipl526", "\\server02\223lglgsmpl0w9", _
                                  "\\server02\223dpdpmpl0wl")

    ProgressBar1.Min = LBound(a_szPrinters())
    ProgressBar1.Max = UBound(a_szPrinters())
    ProgressBar1.Value = 0

    For Each szPrinter In a_szPrinters
        oWshNetwork.AddWindowsPrinterConnection szPrinter
        ProgressBar1.Value = ProgressBar1.Value + 1
    Next

That should give you a progress bar for your printer connections.  It should be fairly similar to manage the drives, too...

HTH

J.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CMILLERAuthor Commented:
Do I need to change anything on this

I get a runtime 9 - subscript out of range

>>>ProgressBar1.Min = LBound(a_szPrinters())
ProgressBar1.Max = UBound(a_szPrinters())
CMILLERAuthor Commented:
I change the values and now I get an error here:

runtime error 424, object required

oWshNetwork.AddWindowsPrinterConnection szPrinter

***************************************************

Private Sub Command1_Click()
Dim oNet, szMsg

    szMsg1 = "If this is the first time you are logging onto this computer, Please wait 3 seconds before clicking the OK button."

    Set WshShell = CreateObject("Wscript.Shell")
    ReturnCode = WshShell.Run("net use * /del /y", 7)
    Set WshShell = Nothing
   
    Set WshNetwork = CreateObject("WScript.Network")
    a_szPrinters = Array("\\server01\223scscbnpl347", "\\server01\223scscmkpl0us", _
                                  "\\server01\223scscmkpl0vv", "\\server01\223scscbnpl0ue", _
                                  "\\server01\223scscbipl526", "\\server01\223lglgsmpl0w9", _
                                  "\\server01\223dpdpmpl0wl")

    ProgressBar1.Min = 1
    ProgressBar1.Max = 7
    ProgressBar1.Value = 3

    For Each szPrinter In a_szPrinters
        oWshNetwork.AddWindowsPrinterConnection szPrinter
        ProgressBar1.Value = ProgressBar1.Value + 1
    Next
CMILLERAuthor Commented:
I changed this "  oWshNetwork.AddWindowsPrinterConnection szPrinter "

to

" WshNetwork.AddWindowsPrinterConnection szPrinter "
CMILLERAuthor Commented:
Now I get an error at " ProgressBar1.Value = ProgressBar1.Value + 1 "



Private Sub Command1_Click()
Dim oNet, szMsg

    szMsg1 = "If this is the first time you are logging onto this computer, Please wait 3 seconds before clicking the OK button."

    Set WshShell = CreateObject("Wscript.Shell")
    ReturnCode = WshShell.Run("net use * /del /y", 7)
    Set WshShell = Nothing
   
    Set WshNetwork = CreateObject("WScript.Network")
    a_szPrinters = Array("\\223cbcs-fs-01\223scscbnpl347", "\\223cbcs-fs-01\223scscmkpl0us", _
                                  "\\223cbcs-fs-01\223scscmkpl0vv", "\\223cbcs-fs-01\223scscbnpl0ue", _
                                  "\\223cbcs-fs-01\223scscbipl526", "\\223cbcs-fs-01\223lglgsmpl0w9", _
                                  "\\223cbcs-fs-01\223dpdpmpl0wl")

    ProgressBar1.Min = 1
    ProgressBar1.Max = 7
    ProgressBar1.Value = 2

    For Each szPrinter In a_szPrinters
        WshNetwork.AddWindowsPrinterConnection szPrinter
        ProgressBar1.Value = ProgressBar1.Value + 1
    Next
jimbobmcgeeCommented:
>>  Now I get an error at " ProgressBar1.Value = ProgressBar1.Value + 1 "

My guess because .Value has exceeded .Max

If you're going to force .Min = 1 and .Value = 2 to start, .Max must be 8.

J.
CMILLERAuthor Commented:
Everything seems to be working well, thanks to all

jimbobmcgee,
on the array, does this just make the script work faster? what is the purpose of the array?
jimbobmcgeeCommented:
The array just gives you an enumerable list of printers -- it doesn't work faster, as such, just reduces the number of times that you have to type:

    Set WshNetwork = CreateObject("WScript.Network")
    PrinterPath = "\\server02\223scscbipl526"
    WshNetwork.AddWindowsPrinterConnection PrinterPath

You just add more entries to the array, if you want to add another printer.

Specificall, the iterative nature of the array usage means that you can increment your ProgressBar as you progress through the array.

J.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.