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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Recalling Dir fails in loop with extra code inside (97)

    Author
    Topic
    #412421

    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.

    Viewing 1 reply thread
    Author
    Replies
    • #901677

      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.

      • #901765

        Aii… should have seen that hushmouth.
        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!

      • #901766

        Aii… should have seen that hushmouth.
        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!

    • #901678

      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.

    Viewing 1 reply thread
    Reply To: Recalling Dir fails in loop with extra code inside (97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: