Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Access - VBA - Création d'un dossier/vérification/et arret

6 réponses
Avatar
Job
Bonjour!
Alors voilà j'ai créer dans un formulaire Access un bouton de commande qui
normalement devrait me créer un répertoire et 7 fichiers.

Voilà mon code:

Private Sub Commande22_Click()
Dim strPath As String
Dim sEmplacementInitial As String, sEmplacementFinal As String



strPath = "\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Projet " + AFF_Affaire

If Dir(strPath, vbDirectory) <> "True" Then
MsgBox ("Le dossier projet " + AFF_Affaire + " existe déjà")
Else
Exit Sub
End If
MkDir strPath
sEmplacementInitial =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Check-List type\(1) Chiffrage xxx.xls"
sEmplacementFinal =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Projet " + AFF_Affaire + "\(1) Chiffrage " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Check-List type\(2) Contrat xxx.xls"
sEmplacementFinal =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Projet " + AFF_Affaire + "\(2) Contrat " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Check-List type\(3) Initialisation xxx.xls"
sEmplacementFinal =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Projet " + AFF_Affaire + "\(3) Initialisation " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Check-List type\(4) Conception xxx.xls"
sEmplacementFinal =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Projet " + AFF_Affaire + "\(4) Conception " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Check-List type\(5) Industrialisation xxx.xls"
sEmplacementFinal =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Projet " + AFF_Affaire + "\(5) Industrialisation " + AFF_Affaire +
".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Check-List type\(6) Série xxx.xls"
sEmplacementFinal =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Projet " + AFF_Affaire + "\(6) Série " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Check-List type\(7) Clôture xxx.xls"
sEmplacementFinal =
"\\FRBTSR03-01\network\Appli\Mapics\Engineering\Projet\Check-List
Projet\Projet " + AFF_Affaire + "\(7) Clôture " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal


MsgBox ("Le dossier du projet " + AFF_Affaire + " a été créé avec succès")

End Sub



Ces répertoires sont crées en fonction d'un code défini dans le formulaire
qui s'appelle AFF_Affaire. Le problème est que quand ce dossier existe déjà,
le programme devrait s'arreter tout simplement et m'afficher une msgbox comme
quoi le répertoire est déjà existant mais il m'affiche cette msgbox quoi
qu'il arrive et il continue quand même le programme.
Alors comment faire pour qu'il puisse tester l'existence du dossier, se
couper si le dossier existe déjà et dans le cas contraire poursuivre la
création du répertoire complet?

Merci d'avance!

6 réponses

Avatar
Eric
Bonjour,

Job a écrit :
Bonjour!
Alors voilà j'ai créer dans un formulaire Access un bouton de commande qui
normalement devrait me créer un répertoire et 7 fichiers.

Voilà mon code:

Private Sub Commande22_Click()
Dim strPath As String
Dim sEmplacementInitial As String, sEmplacementFinal As String



strPath = "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire

If Dir(strPath, vbDirectory) <> "True" Then
MsgBox ("Le dossier projet " + AFF_Affaire + " existe déjà")
Else
Exit Sub
End If
MkDir strPath
sEmplacementInitial =
"FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetCheck-List type(1) Chiffrage xxx.xls"
sEmplacementFinal =
"FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire + "(1) Chiffrage " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
...
MsgBox ("Le dossier du projet " + AFF_Affaire + " a été créé avec succès")

End Sub

Ces répertoires sont crées en fonction d'un code défini dans le formulaire
qui s'appelle AFF_Affaire. Le problème est que quand ce dossier existe déjà,
le programme devrait s'arreter tout simplement et m'afficher une msgbox comme
quoi le répertoire est déjà existant mais il m'affiche cette msgbox quoi
qu'il arrive et il continue quand même le programme.
Alors comment faire pour qu'il puisse tester l'existence du dossier, se
couper si le dossier existe déjà et dans le cas contraire poursuivre la
création du répertoire complet?

Merci d'avance!





Essaies :
If Dir(strPath, vbDirectory) <> "Projet " & AFF_Affaire Then
MsgBox ("Le dossier projet " & AFF_Affaire & " existe déjà")
' attention les fichiers ne seront pas copiés alors que cela
peut-être nécessaire bien que le rep existe.
Exit Sub
End If

Tu pourrais simplifier en mettant dans une variable le chemin partiel
commun puis concaténer (& en non +) en fonction des besoins:
Dim CheminPartiel as string
CheminPartiel =
"FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
Projet"
--
A+
Eric
http://www.mpfa.info/
Avatar
Job
Merci pour cette nouvelle technique je ne connaissais pas, mais par contre
j'ai toujours le même problème: le programme ne veut pas s'arreter à la
commande "Exit Sub" si le dossier existe déjà.






"Eric" a écrit :

Bonjour,

Job a écrit :
> Bonjour!
> Alors voilà j'ai créer dans un formulaire Access un bouton de commande qui
> normalement devrait me créer un répertoire et 7 fichiers.
>
> Voilà mon code:
>
> Private Sub Commande22_Click()
> Dim strPath As String
> Dim sEmplacementInitial As String, sEmplacementFinal As String

> strPath = "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire
>
> If Dir(strPath, vbDirectory) <> "True" Then
> MsgBox ("Le dossier projet " + AFF_Affaire + " existe déjà")
> Else
> Exit Sub
> End If
> MkDir strPath
> sEmplacementInitial =
> "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetCheck-List type(1) Chiffrage xxx.xls"
> sEmplacementFinal =
> "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire + "(1) Chiffrage " + AFF_Affaire + ".xls"
> FileCopy sEmplacementInitial, sEmplacementFinal
> ...
> MsgBox ("Le dossier du projet " + AFF_Affaire + " a été créé avec succès")
>
> End Sub
>
> Ces répertoires sont crées en fonction d'un code défini dans le formulaire
> qui s'appelle AFF_Affaire. Le problème est que quand ce dossier existe déjà,
> le programme devrait s'arreter tout simplement et m'afficher une msgbox comme
> quoi le répertoire est déjà existant mais il m'affiche cette msgbox quoi
> qu'il arrive et il continue quand même le programme.
> Alors comment faire pour qu'il puisse tester l'existence du dossier, se
> couper si le dossier existe déjà et dans le cas contraire poursuivre la
> création du répertoire complet?
>
> Merci d'avance!
>


Essaies :
If Dir(strPath, vbDirectory) <> "Projet " & AFF_Affaire Then
MsgBox ("Le dossier projet " & AFF_Affaire & " existe déjà")
' attention les fichiers ne seront pas copiés alors que cela
peut-être nécessaire bien que le rep existe.
Exit Sub
End If

Tu pourrais simplifier en mettant dans une variable le chemin partiel
commun puis concaténer (& en non +) en fonction des besoins:
Dim CheminPartiel as string
CheminPartiel =
"FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
Projet"
--
A+
Eric
http://www.mpfa.info/



Avatar
bayosky
Bonjour,


Dir ne retourne pas la chaine de caractère "True"

Tente plutôt avec
:

(...)

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(strPath) then
MsgBox ("Le dossier projet " + AFF_Affaire + " existe déjà.")
Exit Sub
End If
MkDir strPath
(...)
End Sub


A+

HB
Avatar
Job
Voilà en fait pour être clair maintenant lorsque je clique sur le bouton soit:
- Si le dossier n'est pas encore existant: il va m'afficher les deux msgbox,
celle qui dit que le dossier existe alors qu'il n'est pas encore créer et
celle qui dit que le dossier à été créer avec succès. Comment n'afficher que
la deuxième msgbox dans ce cas là??

- Si le dossier existe déjà : msgbox du dossier qui existe et ensuite il me
met une erreur comme quoi le fichier est introuvable alors que si le dossier
existe déjà il va tout de même réussir à copier les fichiers excel (voir le
programme). Comment faire stopper le programme lorsqu'il a détecter que le
dossier existe déjà??

Merci d'avance, car c'est une solution que je n'arrive vraiment pas à
résoudre même en cherchant partout sur le net.
Avatar
RCR
Hello,
je n'ai pas vraiment eu le temps de regarder mais de mémoire je dirrirait
que l'objet FileSystemObject doit avoir ce qu'il faut.

trois coup de "F1" sur FileSystemObject devrais règler l'affaire.

Un petit rappel pour les groupes :

Vos messages sont une mine de rensegniement pour les pirates. en écrivant
FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
vous leurs donné presque la clé de la maison !!!

faites attention au info donné et au besoin remplacez les par autre choses
MaMachineMonréseau...nomdefichier

Meilleurs salutations.
@RAPH.


"Job" a écrit dans le message de news:

Bonjour!
Alors voilà j'ai créer dans un formulaire Access un bouton de commande qui
normalement devrait me créer un répertoire et 7 fichiers.

Voilà mon code:

Private Sub Commande22_Click()
Dim strPath As String
Dim sEmplacementInitial As String, sEmplacementFinal As String



strPath =
"FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire

If Dir(strPath, vbDirectory) <> "True" Then
MsgBox ("Le dossier projet " + AFF_Affaire + " existe déjà")
Else
Exit Sub
End If
MkDir strPath
sEmplacementInitial > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetCheck-List type(1) Chiffrage xxx.xls"
sEmplacementFinal > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire + "(1) Chiffrage " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetCheck-List type(2) Contrat xxx.xls"
sEmplacementFinal > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire + "(2) Contrat " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetCheck-List type(3) Initialisation xxx.xls"
sEmplacementFinal > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire + "(3) Initialisation " + AFF_Affaire +
".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetCheck-List type(4) Conception xxx.xls"
sEmplacementFinal > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire + "(4) Conception " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetCheck-List type(5) Industrialisation xxx.xls"
sEmplacementFinal > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire + "(5) Industrialisation " + AFF_Affaire +
".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetCheck-List type(6) Série xxx.xls"
sEmplacementFinal > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire + "(6) Série " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal
sEmplacementInitial > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetCheck-List type(7) Clôture xxx.xls"
sEmplacementFinal > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
ProjetProjet " + AFF_Affaire + "(7) Clôture " + AFF_Affaire + ".xls"
FileCopy sEmplacementInitial, sEmplacementFinal


MsgBox ("Le dossier du projet " + AFF_Affaire + " a été créé avec succès")

End Sub



Ces répertoires sont crées en fonction d'un code défini dans le formulaire
qui s'appelle AFF_Affaire. Le problème est que quand ce dossier existe
déjà,
le programme devrait s'arreter tout simplement et m'afficher une msgbox
comme
quoi le répertoire est déjà existant mais il m'affiche cette msgbox quoi
qu'il arrive et il continue quand même le programme.
Alors comment faire pour qu'il puisse tester l'existence du dossier, se
couper si le dossier existe déjà et dans le cas contraire poursuivre la
création du répertoire complet?

Merci d'avance!



Avatar
Job
Merci beaucoup la fonction FileSystemObject était la clef!
Encore merci
a+


"RCR" a écrit :

Hello,
je n'ai pas vraiment eu le temps de regarder mais de mémoire je dirrirait
que l'objet FileSystemObject doit avoir ce qu'il faut.

trois coup de "F1" sur FileSystemObject devrais règler l'affaire.

Un petit rappel pour les groupes :

Vos messages sont une mine de rensegniement pour les pirates. en écrivant
FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
vous leurs donné presque la clé de la maison !!!

faites attention au info donné et au besoin remplacez les par autre choses
MaMachineMonréseau...nomdefichier

Meilleurs salutations.
@RAPH.


"Job" a écrit dans le message de news:

> Bonjour!
> Alors voilà j'ai créer dans un formulaire Access un bouton de commande qui
> normalement devrait me créer un répertoire et 7 fichiers.
>
> Voilà mon code:
>
> Private Sub Commande22_Click()
> Dim strPath As String
> Dim sEmplacementInitial As String, sEmplacementFinal As String
>
>
>
> strPath =
> "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire
>
> If Dir(strPath, vbDirectory) <> "True" Then
> MsgBox ("Le dossier projet " + AFF_Affaire + " existe déjà")
> Else
> Exit Sub
> End If
> MkDir strPath
> sEmplacementInitial > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetCheck-List type(1) Chiffrage xxx.xls"
> sEmplacementFinal > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire + "(1) Chiffrage " + AFF_Affaire + ".xls"
> FileCopy sEmplacementInitial, sEmplacementFinal
> sEmplacementInitial > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetCheck-List type(2) Contrat xxx.xls"
> sEmplacementFinal > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire + "(2) Contrat " + AFF_Affaire + ".xls"
> FileCopy sEmplacementInitial, sEmplacementFinal
> sEmplacementInitial > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetCheck-List type(3) Initialisation xxx.xls"
> sEmplacementFinal > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire + "(3) Initialisation " + AFF_Affaire +
> ".xls"
> FileCopy sEmplacementInitial, sEmplacementFinal
> sEmplacementInitial > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetCheck-List type(4) Conception xxx.xls"
> sEmplacementFinal > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire + "(4) Conception " + AFF_Affaire + ".xls"
> FileCopy sEmplacementInitial, sEmplacementFinal
> sEmplacementInitial > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetCheck-List type(5) Industrialisation xxx.xls"
> sEmplacementFinal > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire + "(5) Industrialisation " + AFF_Affaire +
> ".xls"
> FileCopy sEmplacementInitial, sEmplacementFinal
> sEmplacementInitial > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetCheck-List type(6) Série xxx.xls"
> sEmplacementFinal > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire + "(6) Série " + AFF_Affaire + ".xls"
> FileCopy sEmplacementInitial, sEmplacementFinal
> sEmplacementInitial > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetCheck-List type(7) Clôture xxx.xls"
> sEmplacementFinal > > "FRBTSR03-01networkAppliMapicsEngineeringProjetCheck-List
> ProjetProjet " + AFF_Affaire + "(7) Clôture " + AFF_Affaire + ".xls"
> FileCopy sEmplacementInitial, sEmplacementFinal
>
>
> MsgBox ("Le dossier du projet " + AFF_Affaire + " a été créé avec succès")
>
> End Sub
>
>
>
> Ces répertoires sont crées en fonction d'un code défini dans le formulaire
> qui s'appelle AFF_Affaire. Le problème est que quand ce dossier existe
> déjà,
> le programme devrait s'arreter tout simplement et m'afficher une msgbox
> comme
> quoi le répertoire est déjà existant mais il m'affiche cette msgbox quoi
> qu'il arrive et il continue quand même le programme.
> Alors comment faire pour qu'il puisse tester l'existence du dossier, se
> couper si le dossier existe déjà et dans le cas contraire poursuivre la
> création du répertoire complet?
>
> Merci d'avance!
>