Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file system object, for...each

Status
Not open for further replies.

Niv3k

Programmer
Jul 11, 2001
350
US
I'm baffled. On Form1, I have the following lines of code:
Code:
Private Sub Form_Activate()
Dim fso As New FileSystemObject
Dim fld As Folder
Dim fil As File

    Set fld = fso.GetFolder("C:\Temp")
    For Each fil In fld.Files
        Me.Caption = fil.Name
        DoEvents
        
        If Not fso.FileExists("C:\Temp1\" & fil.Name) Or gbOverwrite Then
            fil.Copy "C:\Temp1\" & fil.Name, gbOverwrite
        Else
            Form2.Caption = fil.Name
            Form2.Show vbModal
            If gbOverwrite Or gbOverwriteOnce Then fil.Copy "C:\Temp1\" & fil.Name, True
        End If
        DoEvents
    Next
Unload Me
End Sub
Form2 has three buttons (Yes, Yes to All, No) which set gbOverwrite or gbOverwriteOnce to true or false, depending on whether you want to overwrite this one file or All Files.

When the file(s) are not there, it runs through perfectly. When you select "Yes to All" (setting gbOverwrite to True), it runs through fine. But if you select No or Yes, the next iteration through is still on the same file, so you will never get past the first file. I think this has something to do with the modal form, but I can't see why it would reset the for...each loop to the first object in the collection.

If anyone has any ideas on this at all, let me know.

Kevin

"There are only three types of people in the world. Those who can count, and those who can't." - Warren Buffet
 
I suggest that you check thread222-343200. It has some great information. What is happening is that when Form2 is displayed, Form1 is deactivated. when Form 2 is unloaded or deactivated, Form1 is activated again, firing the Form_Activate event again, starting over at the beginning--again.

Moving your code the the Form_Load event might solve your problem. BlackburnKL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top