How to create a folder with query conditions (1 Viewer)

habiler

Registered User.
Local time
Today, 04:08
Joined
Aug 10, 2014
Messages
70
Hey the Community,
My code blocks at the line level "MkDir ToPath"
He don't find the ToPath directory


Code:
Option Compare Database

Function Copy_Some_files()
    Dim db                    As DAO.Database
    Dim rs                    As DAO.Recordset
    Dim sSQL                  As String
    Dim i                     As Long
 
    Set db = CurrentDb
    
    sSQL = " SELECT Numero, Matr" & _
           " FROM Decisions" & _
           " Where Numero > '999'"
          Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)
    With rs
        If rs.RecordCount = 0 Then
            MsgBox "No files to transfer"
        Else
            Do While Not .EOF
                FromPath = "C:\Decisions\"
                ToPath = "C:\Pers\"
                FileExt = ".pdf"
                FromPath = FromPath & !Numero & FileExt
                If Dir(FromPath) = "" Then
                    MsgBox FromPath & " doesn't exist"
                Else
                    ToPath = ToPath & !Matr & "\"
                    If Dir(ToPath) = "" Then
                        MkDir ToPath
                    End If
                    FileCopy FromPath, ToPath & !Numero & FileExt
                    .MoveNext
                End If
            Loop
            MsgBox "Files transferred from C:\Decisions to C:\Pers"
        End If
    End With
    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Function
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:08
Joined
Oct 29, 2018
Messages
21,449
Hi. Try doing a Debug.Print ToPath to see the name of the folder you're trying to create. Make sure it doesn't have any invalid/illegal characters.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:08
Joined
May 7, 2009
Messages
19,228
Code:
…
…
               Else
                    [COLOR="Blue"]ToPath = ToPath & !Matr[/COLOR]
                    If Dir(ToPath[COLOR="blue"], vbDirectory[/COLOR]) = "" Then
                        MkDir ToPath
                    End If
                    [COLOR="blue"]ToPath = ToPath & "\"[/COLOR]
 …
…
 

habiler

Registered User.
Local time
Today, 04:08
Joined
Aug 10, 2014
Messages
70
Now µI adjusted my code but he doesn t copy the file.pdf into the right directory.
" FileCopy FromPath, ToPath & !Numero" does not work i think

Code:
Option Compare Database

Sub Copy_Certain_Files_In_Folder()

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String
    Dim db                    As DAO.Database
    Dim rs                    As DAO.Recordset
    Dim sSQL                  As String
    Dim Fnom As String
  
    Set db = CurrentDb
    
    sSQL = " SELECT Numero, Matr " & _
           " FROM Decisions" & _
           " Where VAL(Numero) < 999"
      Fnom = Numero
      
      ' Numero = number1,2,3,4
      ' Matr = Name1,2,3,4
      
    Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)
       With rs
        If rs.RecordCount = 0 Then
            MsgBox "No files to transfer"
        Else
            Do While Not .EOF
                FromPath = "C:\Decisions\"
                ToPath = "C:\Personnel\"
                FromPath = FromPath & !Numero

                    ToPath = ToPath & !Matr
                    If Dir(ToPath, vbDirectory) = "" Then
                    Debug.Print ToPath
                        MkDir ToPath
                    End If
                    ToPath = ToPath & "\"
                    FileCopy FromPath, ToPath & !Numero
                .MoveNext
      
            Loop
            MsgBox "Files transferred from C:\Doc to C:\Pers"
        End If
    End With

End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:08
Joined
May 7, 2009
Messages
19,228
does numero has ".pdf" extension on it.
if it doesn't, your need to change your code:

..
FromPath = FromPath & !Numero & ".pdf"
..
..
FileCopy FromPath, ToPath & !Numero & ".pdf"
..
 

habiler

Registered User.
Local time
Today, 04:08
Joined
Aug 10, 2014
Messages
70
OK Its ok but now If Dir(FromPath, vbDirectory) = "" Then my code blocks.

I added . MoveNext but it sblocks too.

Code:
       With rs
        If rs.RecordCount = 0 Then
            MsgBox "No files to transfer"
        Else
            Do While Not .EOF
                FromPath = "C:\Decisions\"
                ToPath = "C:\Personnel\"
                FromPath = FromPath & !Numero & ".pdf"
             If Dir(FromPath, vbDirectory) = "" Then
                  MsgBox FromPath & " Not present"
                 .MoveNext
           Else
                    ToPath = ToPath & !Matr & "\"
                    If Dir(ToPath, vbDirectory) = "" Then
                        MkDir ToPath
                    End If
                    FileCopy FromPath, ToPath & !Numero & ".pdf"
                    .MoveNext
               End If
            Loop
            MsgBox "Files transferred from C:\Doc to C:\Pers"
        End If
    End With
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:08
Joined
May 7, 2009
Messages
19,228
If Dir(FromPath) = "" Then
 

habiler

Registered User.
Local time
Today, 04:08
Joined
Aug 10, 2014
Messages
70
The code has been rewritten. Sub directory is created but the file . pdf is not copied


Code:
Private Sub btncopy_Click()
    Dim fso As Object
    Dim fromPath As String, toPath As String, strPath As String
    Dim fileExt As String, source As String, destination As String, strSql As String
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
 
    fromPath = "C:\Decisions"  '<< Changed
    toPath = "C:\Personnel"    '<< Changed
    fileExt = ".pdf"  '<< Changed

    'You can use *.* for all files or *.doc for Word files
 
    Set dbs = CurrentDb
    Set fso = CreateObject("Scripting.FileSystemObject")
 
    strSql = " SELECT Dec.Numero, Dec.Matr, Dec.NomName, Dec.NomNameMatr,Dec.debut, Dec.fin " & _
             " From DEC " & _
             " Where VAL(Numero) > 4000"
    Set rst = dbs.OpenRecordset(strSql, dbOpenSnapshot)
    With rst
        If .RecordCount <> 0 Then
            Do While Not .EOF
            i = i + 1
 
            strPath = toPath & "\" & !NomNameMatr
            If Len(Dir(strPath, vbDirectory)) = 0 Then
                MkDir strPath
            End If
            strPath = toPath & "\" & !NomNameMatr & "\Decisions\"
            If Len(Dir(strPath, vbDirectory)) = 0 Then
                MkDir strPath
            End If
 
            If Dir(fromPath & "\" & !Numero) & fileExt <> "" Then
            source = fromPath & "\" & !Numero & fileExt
            destination = toPath & "\" & !NomNameMatr & "\Decisions\"
            overwrite = True 'True = ecraser si le fichier existe deja
            Call fso.CopyFile(source, destination, overwrite)
            End If
            .MoveNext
            Loop
        End If
    End With
    rst.Close
    Set fso = Nothing
    Set rst = Nothing
    Set dbs = Nothing
End Sub
 

Users who are viewing this thread

Top Bottom