Create a new folder and copy another folders contents into the new folder (1 Viewer)

Rob_Jones101

Member
Local time
Today, 10:06
Joined
Aug 8, 2019
Messages
41
I am creating a new database for where I work. It is creating quotes for jobs. When I click the save button its save the quote and opens a new folder which gets its name from three fields on the form. I want it to import or copy files from another folder in the directory to the newly created folder.

I have tried to use the copyfolder function and it does copy the files but to the main folder where all the quotes are held and not into the newly created folder.

On Error GoTo btnOK_Click_Error

Const strParent = "C:\Users\r.jones\Desktop\Quotes\ "
Dim Strquotenumber As String
Dim Strsite As String
Dim StrprojDesc As String
Dim strFolder As String
Dim Strspace As String

Strspace = Space(1) & "- "

Strquotenumber = Me.QuoteNumber
Strsite = Me.Txtsite
StrprojDesc = Me.Project_Description

strFolder = strParent & Strquotenumber & Strspace & Strsite & Strspace & StrprojDesc
If Dir(strFolder, vbDirectory) = "" Then MkDir strFolder


Shell "explorer.exe " & strFolder, vbNormalFocus

If Me.Dirty Then DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "Frmquotebook"
btnOK_Click_Exit: Exit Sub

btnOK_Click_Error: MsgBox "Error" & " In Attempting To Create New Folder. All Fields Must Be Filled In." & vbCrLf_ Cancel = True Resume btnOK_Click_Exit

Is it possible to do this as I have not been able to find anything on it.

Thanks for the help.
 

Rob_Jones101

Member
Local time
Today, 10:06
Joined
Aug 8, 2019
Messages
41
I want to create a folder and copy files from another folder at the same time. I cant seem to figure out how to create and folder and copy at the same time. I can put another button on the form to copy the files but would prefer it all in one button.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:06
Joined
Sep 21, 2011
Messages
14,038
So you create the folder first, then use that as the destination?, which likely should be
Code:
strFolder & "\"

You might not even need the "" ?, I've not used the function, but that link infers that you should.?

HTH
 
Last edited:

Rob_Jones101

Member
Local time
Today, 10:06
Joined
Aug 8, 2019
Messages
41
Thanks Gasman

What would I need to add to the code to get it to copy the files. I understand what you are saying but im quite new to this.

Thanks
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:06
Joined
May 7, 2009
Messages
19,169
after creating the new folder, move the files there.
are the files in "C:\Users\r.jones\Desktop\Quotes\ "?
if so:
Code:
…
…
…
Strspace = Space(1) & "- "

Strquotenumber = Me.QuoteNumber
Strsite = Me.Txtsite
StrprojDesc = Me.Project_Description

strFolder = strParent & Strquotenumber & Strspace & Strsite & Strspace & StrprojDesc
If Dir(strFolder, vbDirectory) = "" Then MkDir strFolder
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objFolder = objFSO.GetFolder(strParent)
For Each objFile In objFolder.Files
    objFSO.MoveFile objFile.Path, strFolder & "\"
Next

Shell "explorer.exe " & strFolder, vbNormalFocus
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:06
Joined
Sep 21, 2011
Messages
14,038
Thanks Gasman

What would I need to add to the code to get it to copy the files. I understand what you are saying but im quite new to this.

Thanks

Well presuming your files are in the strParent folder and you want all of them, according to that link it would be along the lines of

Code:
If Dir(strFolder, vbDirectory) = "" Then 
    MkDir strFolder
    fso.CopyFolder strParent & "*" ,strFolder & "\"
End If
where you have declared the fso object at the top of your module with

Code:
Dim fso as Object
Set fso = CreateObject("Scripting.FileSystemObject")

I would add the backslash to the folder name when created and not keep adding it to your name as I have had to do here. Much like you did for the parent.

Review that link I posted. That is what I worked from.

HTH
 

Rob_Jones101

Member
Local time
Today, 10:06
Joined
Aug 8, 2019
Messages
41
Thanks Gasman

That has worked perfectly. I cant thank you enough.

How would i go about searching for this newly created folder from another form. Or how to create a hyperlink to this folder on creation that can be stored in the table.

Sorry to keep bothering you but I have spent 3 days trying to figure it out and you have just done it in one hour. Thanks:):):)
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:06
Joined
Sep 21, 2011
Messages
14,038
Well it is easy enough to store the path in a table in a hyperlink field and then present that on a form.?

Not used them myself TBH

The experts line Arne might have a better method.?
 

Users who are viewing this thread

Top Bottom