Solved

the window did not close while the files are copying

Posted on 2003-11-26
5
297 Views
Last Modified: 2011-04-14
I wanted to give 50 points, but I can 't because this is my last 25 points. Your help is appreciated.

Visual Basic 5.0

form1 called form2. Form2 two then performs file copying. Everything copy. The problem is while it copy, if I click on the button Quit (stop copying), Form2 do not close but continue to copy files.

The code below is what is in Form2. Notice on the line with a label 99 and 100.

99      Unload Form_Copy_Status

I did tell the form to unload, but it won 't. It continue to copy files.

100      End

If it put this line in, it will quit the whole application. I just want to close Form2 so that the file will stop copying. I do not want to close the whole application.

=================================================
Option Explicit

'These are public variables and constant within this form and therefore,
'its value can be accessed by any function and procedure eithin this form
Const Source_File_Path_1 = "C:\WINNT\SYSTEM32\"      '"A:\WebOffice_VPN\"
Const Source_File_Path_2 = "C:\WINNT\SYSTEM32\"      '"A:\WebOffice_VPN\OCX\"

Dim Boot_Up_Drive_Letter As String           'C:\
Dim Boot_Up_Drive_Letter_And_Directory As String     'C:\winnt\system32\'


Private Sub Form_Load()
  Call Compute_Total_Files_Size
End Sub


Private Sub Compute_Total_Files_Size()
On Error GoTo ErrorHandler

    Dim File_Size As Double
    Dim Folder_Size As Double
    Dim Percent_Of_Total As Double
    Dim Total_Percentage_Completed As Double
    Dim Error_Exist As Boolean
   
    'Declare File System Variables
    Dim object_File As Scripting.File
    Dim object_FileSystem As Scripting.FileSystemObject
    Dim object_Folder As Scripting.Folder
    Dim A As String
   
    A = "C:\WebOffice_VPN\"

    'Source_File_Path_1 = "A:\WebOffice_VPN\"
    'Source_File_Path_2 = "A:\WebOffice_VPN\OCX\"

    Set object_FileSystem = New Scripting.FileSystemObject
    Set object_Folder = object_FileSystem.GetFolder(Source_File_Path_1)

    Error_Exist = False
    Folder_Size = object_Folder.Size
    Total_Percentage_Completed = 0

    Form_Copy_Status.Show
   
    ProgressBar2.Max = 100          'Set it to 100%
    ProgressBar2.Visible = True     'Make the ProgressBar2 visible
    Timer2.Interval = 1000          '1000 is equal to 1 second
    Timer2.Enabled = True           'Start the Timer2

    For Each object_File In object_Folder.Files
      Form_Copy_Status.Copy_From = Source_File_Path_1 & object_File.Name       'A:\WebOffice_VPN\
      Form_Copy_Status.Copy_To = Form1.Directory_Box & object_File.Name        'C:\WebOffice_VPN\
      object_FileSystem.CopyFile Source_File_Path_1 & object_File.Name, "C:\WebOffice_VPN\"

      'object_FileSystem.CopyFile Source_File_Path_1 & object_File.Name, Directory_Box
      Percent_Of_Total = Val(Format((object_File.Size / Folder_Size) * 100, "0.00"))
      Total_Percentage_Completed = Val(Format(Total_Percentage_Completed + Percent_Of_Total, "0.00"))
      Total_Percentage_Done.Text = Val(Format(Total_Percentage_Completed, "0.00"))
      DoEvents
    Next object_File
   
    Set object_Folder = object_FileSystem.GetFolder(Source_File_Path_2) '
    'For Each object_File In object_Folder.Files
    '  Form_Copy_Status.Copy_From = Source_File_Path_2   'A:\WebOffice_VPN\OCX\
    '  Form_Copy_Status.Copy_To = Boot_Up_Drive_Letter_And_Directory   'C:\Winnt\System32\
    '  object_FileSystem.CopyFile Source_File_Path_2 & object_File.Name, Boot_Up_Drive_Letter_And_Directory
    '  Percent_Of_Total = Val(Format((object_File.Size / Folder_Size) * 100, "0.00"))
    '  Total_Percentage_Completed = Total_Percentage_Completed + Percent_Of_Total
    '  Form_Copy_Status.Total_Percentage_Done = Total_Percentage_Completed
    'Next object_File
 
End Sub


Private Sub Timer2_Timer()
  Static integer_Time
 
  If IsEmpty(integer_Time) Then
    integer_Time = 1
    Time_Text_Box = integer_Time
  End If
 
  'Total_Percentage_Done is the percentage of the total number of bytes
  'of all files that was copied. This number is always from 0 to 100
  ProgressBar2.Value = Total_Percentage_Done.Text

  If ProgressBar2.Value = ProgressBar2.Max Then
    Timer1.Enabled = False
    ProgressBar2.Visible = False
    Time_Text_Box = integer_Time
    ProgressBar2.Value = ProgressBar2.Min
  Else
    integer_Time = integer_Time + 1
    Time_Text_Box = integer_Time
  End If
End Sub


Private Sub Quit_Click()
    Dim Msg, Style, Title, Response, MyString
    Msg = "Do you want to stop installing the program ?"   ' Define message.
    Style = vbYesNo + vbInformation + vbDefaultButton2     ' Define buttons.
    Title = "MsgBox Stop installing the program"           ' Define title.

    Response = MsgBox(Msg, Style, Title)
    If Response = vbYes Then  
      Timer2.Enabled = False
99      Unload Form_Copy_Status                      
100      End
    End If
End Sub
0
Comment
Question by:lin100
  • 3
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
The form won't unload until all subs have completed.  You need to add a check in your copy loop to check for the form closing.

Create a boolean flag on your form2 called quitting.  Set it to false on Form_Load, and true on Form_Unload.  Then add a check in your For loop to check for it and quit the loop if it becomes true.

Dim quitting As Boolean

Private Sub Form_Load()
    quitting = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
    quitting = True
End Sub

In your For Each Loop....

For Each object_File In object_Folder.Files
      Form_Copy_Status.Copy_From = Source_File_Path_1 & object_File.Name       'A:\WebOffice_VPN\
      Form_Copy_Status.Copy_To = Form1.Directory_Box & object_File.Name        'C:\WebOffice_VPN\
      object_FileSystem.CopyFile Source_File_Path_1 & object_File.Name, "C:\WebOffice_VPN\"

      'object_FileSystem.CopyFile Source_File_Path_1 & object_File.Name, Directory_Box
      Percent_Of_Total = Val(Format((object_File.Size / Folder_Size) * 100, "0.00"))
      Total_Percentage_Completed = Val(Format(Total_Percentage_Completed + Percent_Of_Total, "0.00"))
      Total_Percentage_Done.Text = Val(Format(Total_Percentage_Completed, "0.00"))
      DoEvents
      If quitting Then
           Exit For ' or use Exit Sub
      End If
Next object_File

Once that sub is exited, the form will completely unload.

Regards,

Idle_Mind
0
 

Author Comment

by:lin100
Comment Utility
Hi Idle_Mind. Thank for helping me.

I modified the code as you have suggested. When I clicked on the button quit,
the copying stop, but the form2 did not close. This works if on label number 6 (see below for 6 --> 'Unload Me ) , I commented out the code Unload me.

If the code Unload Me was not commented, it would result in the same error as before "run-time error '364' object was unloaded".

The Form_Unload is redundant, it does not do anything. When un-commented,
it still does not change anything

The only thing the code below does was stop the copying when I clicked the Quit button. If I attempt to unload the form anywhere, it would give me a
"run-time error '364' object was unloaded".

=======================================
Option Explicit

Const Source_File_Path_1 = "C:\WINNT\SYSTEM32\"      '"A:\WebOffice_VPN\"
Const Source_File_Path_2 = "C:\WINNT\SYSTEM32

Dim Boot_Up_Drive_Letter As String                            'C:\
Dim Boot_Up_Drive_Letter_And_Directory As String     'C:\winnt\system32\'

1 --> Dim Cancel_Copy_File As Boolean


'Private Sub Form_Unload(Cancel As Integer)
--> This is redundant so the whole procedure is commemnetd '  Cancel_Copy_File = True
'End Sub


Private Sub Form_Load()
2 -->  Cancel_Copy_File = False
  Call Compute_Total_Files_Size

End Sub


Private Sub Compute_Total_Files_Size()
On Error GoTo ErrorHandler

    Dim File_Size As Double
    Dim Folder_Size As Double
    Dim Percent_Of_Total As Double
    Dim Total_Percentage_Completed As Double
    Dim Error_Exist As Boolean
   
    'Declare File System Variables
    Dim object_File As Scripting.File
    Dim object_FileSystem As Scripting.FileSystemObject
    Dim object_Folder As Scripting.Folder


    'Source_File_Path_1 = "A:\WebOffice_VPN\"
    'Source_File_Path_2 = "A:\WebOffice_VPN\OCX\"

    Set object_FileSystem = New Scripting.FileSystemObject
    Set object_Folder = object_FileSystem.GetFolder(Source_File_Path_1)

    Error_Exist = False
    'Cancel_Copy_File = False
    Folder_Size = object_Folder.Size
    Total_Percentage_Completed = 0

    Form_Copy_Status.Show
   
    ProgressBar2.Max = 100          'Set it to 100%
    ProgressBar2.Visible = True     'Make the ProgressBar2 visible
    Timer2.Interval = 1000          '1000 is equal to 1 second
    Timer2.Enabled = True           'Start the Timer2

    For Each object_File In object_Folder.Files
        Form_Copy_Status.Copy_From = Source_File_Path_1 & object_File.Name       'A:\WebOffice_VPN\
        Form_Copy_Status.Copy_To = Form1.Directory_Box & object_File.Name        'C:\WebOffice_VPN\
        object_FileSystem.CopyFile Source_File_Path_1 & object_File.Name, "C:\WebOffice_VPN\"

        'object_FileSystem.CopyFile Source_File_Path_1 & object_File.Name, Directory_Box
        Percent_Of_Total = Val(Format((object_File.Size / Folder_Size) * 100, "0.00"))
        Total_Percentage_Completed = Val(Format(Total_Percentage_Completed + Percent_Of_Total, "0.00"))
        Total_Percentage_Done.Text = Val(Format(Total_Percentage_Completed, "0.00"))
        DoEvents
3 -->        If Cancel_Copy_File Then
4 -->           Exit Sub
5 -->        End If
    Next object_File
       
End Sub



Private Sub Timer2_Timer()
  Static integer_Time
 
  If IsEmpty(integer_Time) Then
    integer_Time = 1
    Time_Text_Box = integer_Time
  End If
 
  'Total_Percentage_Done is the percentage of the total number of bytes
  'of all files that was copied. This number is always from 0 to 100
  ProgressBar2.Value = Total_Percentage_Done.Text

  If ProgressBar2.Value = ProgressBar2.Max Then
    Timer1.Enabled = False
    ProgressBar2.Visible = False
    Time_Text_Box = integer_Time
    ProgressBar2.Value = ProgressBar2.Min
  Else
    integer_Time = integer_Time + 1
    Time_Text_Box = integer_Time
  End If
End Sub

Private Sub Quit_Click()
    Dim Msg, Style, Title, Response, MyString
    Msg = "Do you want to stop installing the program ?"   ' Define message.
    Style = vbYesNo + vbInformation + vbDefaultButton2     ' Define buttons.
    Title = "MsgBox Stop installing the program"           ' Define title.

    Response = MsgBox(Msg, Style, Title)
    If Response = vbYes Then                               ' User chose Yes.
      Timer1.Enabled = False
      Timer2.Enabled = False
6 -->      Cancel_Copy_File = True
7 --> 'Unload Me           This statement will cause a run-time error  '364'        
    End If
End Sub
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 25 total points
Comment Utility
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vamsgldunloaded.asp

The copying code is being called from your Form_Load.  When you try to quit it is actually still in that sub which is basically what the site is talking about.

Try loading Form2 first, then call the Compute_Total_Files_Size() sub from your first form.  You will have to change Compute_Total_Files_Size() from Private to Public.  Put the Unload Me back in your Quit_Click.

Form2.Show
Form2.Compute_Total_Files_Size

Idle_Mind
0
 

Author Comment

by:lin100
Comment Utility
Thank you Idle_Mind. You are a genius.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
I wouldn't go that far...  =)

Glad I could help.

Happy Programming,

Idle_Mind
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

728 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

10 Experts available now in Live!

Get 1:1 Help Now