VBA code to rename a directory with a form field value
Solved
doublem93
Posted messages
349
Registration date
Status
Member
Last intervention
-
doublem93 Posted messages 349 Registration date Status Member Last intervention -
doublem93 Posted messages 349 Registration date Status Member Last intervention -
Hello,
I would need help for my Access database because despite my searches I can't find the solution...
My problem is the following:
I want, by clicking a button in a form, to copy and rename a directory with the value of a field.
To bypass the problem I found the code that allows me to copy my model directory and name it "New" but I can't give it the name based on the [NomRep] field.
Here is the code I have:
Sub CopyFolder(folderpath As String, destfolderpath As String) Dim fso As Object Dim fld As Object Set fso = CreateObject("Scripting.FileSystemObject") Set fld = fso.GetFolder(folderpath) fld.Copy destfolderpath End Sub Private Sub Commande50_Click() Call CopyFolder("D:\Dossiers\Dossiermodele", "D:\Dossiers\New") End Sub
I would therefore greatly appreciate your guidance if you have time to look into it...
Thank you in advance for your help
Best regards
5 answers
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention Ambassadeur 1 588
Is all of this in a form?
If you manage to retrieve the value of your [NomRep] field and put it into the variable nouveaunom, you can do:Private Sub Commande50_Click() dim nouveaumon as string nouveaunom = "" ' up to you to figure Call CopyFolder("D:\Dossiers\Dossiermodele", "D:\Dossiers\"+nouveaunom) End Sub -
Thank you
I will try on Monday and I will tell you the result
Kind regards
--
Posted from CCM Live forum for iPhone/iPad -
Hello,
I tried the proposed solution and it doesn’t work. We end up with error 76 and highlighting of the line fld.copy...
Thanks anyway.
--
Posted from CCM Live forum for iPhone/iPad -
Good evening
I’ll look into this as soon as I’m back in the office, which won’t be until Wednesday or Thursday.
Thanks again for the help.
--
Posted from CCM Live forum for iPhone/iPad -
Hello,
I just tried again and it works as soon as the folder name field is not empty.
Thank you for your help.
---
I explain that I replaced [NomRep] with [IdDossier] & " - " & [NomDossier].
These are two fields present in the form and they are the information contained in the [NomRep] field.
--- The complete code is as follows:
Sub CopyFolder(folderpath As String, destfolderpath As String)
Dim fso As Object
Dim fld As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(folderpath)
fld.Copy destfolderpath
End Sub
Private Sub Commande50_Click()
dim nouveaumon as string
nouveaunom = Me.[IdDossier] & " - " & Me.[NomDossier]
Call CopyFolder("D:\Dossiers\Dossiermodele", "D:\Dossiers\"+nouveaunom)
End Sub
`
-