Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Leuven, Vlaanderen, Belgium
    Posts
    322
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Recalling Dir fails in loop with extra code inside (97)

    Hello, (this should/had to be an easy one...)
    I want to process a number of files in a directory, using a loop with the old Dir function. BUT...

    1. It renders all files while testing with an 'empty' loop, like:

    Dim strDir as String
    Dim strFile as String
    strDir = CurrentDBDir '(1)
    strFile = Dir(strDir)
    Do Until strFile = ""
    MsgBox strFile
    strFile = Dir
    Loop
    ' (1) MVPS custom function returning database folder path

    2. It renders only *one* result (i.e. the first file found?) after having added some extra code:
    ...
    Do Until strFile = ""
    MsgBox strFile
    'Only handle the suitable files
    If Right(strFile, 6) = "E1.doc" Then
    '... 'copy & rename file
    '... 'use Word automation to open renamed file & perform actions
    ElseIf Left(Dir$(CurrentDb.Name), Len(Dir$(CurrentDb.Name)) - 4) = Left(strFile, Len(strFile) - 4) Then
    'do nothing
    Else
    '... 'register
    End If
    strFile = Dir
    Loop


    Does anyone have any idea what's happening here? The only workaround I can imagine is using the 'empty' Dir loop first to fill a recordset/array filled with all 'suitable' files info and process the files in this list lateron, but it's hard to accept this would be the best practice...

    Hasse
    p.s. Even with only the If... then... else... lines activated & all other lines deactivated (as comment lines), the loop fails.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Recalling Dir fails in loop with extra code inside (97)

    Hello Hasse,

    You should not use Dir (or the equivalent Dir$) within a loop more than once. A new call to Dir with an argument will cancel the previous search. You must use another method to find the filename of the current database.

  3. #3
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Leuven, Vlaanderen, Belgium
    Posts
    322
    Thanks
    9
    Thanked 0 Times in 0 Posts

    SOLVED - Re: Recalling Dir fails in loop with e...

    Aii... should have seen that <img src=/S/hushmouth.gif border=0 alt=hushmouth width=16 height=16>.
    FYI: I replaced the Dir$(CurrentDb.Name) inside the loop with strCurrentDbName, which I declare and equal to the dir$... at the beginning of the procedure, so before the loop. And... case solved... once again... Hans... thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •