ushiwasasuke
Messages postés13Date d'inscriptionvendredi 3 mars 2006StatutMembreDernière intervention 1 octobre 2008
-
18 oct. 2006 à 16:26
ushiwasasuke
Messages postés13Date d'inscriptionvendredi 3 mars 2006StatutMembreDernière intervention 1 octobre 2008
-
23 oct. 2006 à 14:56
Bonjour,
J'ai un problème avec une listbox..
Voici un descriptif : l'utilisateur choisit le répértoire ou il faut lister les .doc ensuite
j'aimerai pouvoir tout lister dans une listBox mais je sais aps trop
comment faire...
Voici mon code :
ce qu'il faut déclarer en haut
Option Compare Database
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Public Enum NetWork
WithNetworkFolders = 0
WithoutNetworkFolders = 2
End Enum
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long
If IsNull(edit_repertoire) Or Len(edit_repertoire) = 0 Then
btn_lister.Visible = False
Else
btn_lister.Visible = True
End If
End Sub
la fonction SelectFolder :
Public Function SelectFolder(Optional Folder As String = "" _
, Optional NetWorkFolders As NetWork = WithNetworkFolders _
) As String
Dim X As Long, bi As BROWSEINFO, dwIList As Long
Dim szPath As String, wPos As Integer If Folder "" Then Folder CurrentProject.Path
With bi
.hOwner = hWndAccessApp
.lpszTitle = "Sélectionnez votre dossier et cliquez sur OK"
.ulFlags = BIF_RETURNONLYFSDIRS _
Or BIF_USENEWUI _
Or NetWorkFolders
End With
dwIList = SHBrowseForFolder(bi)
szPath = Folder & Space$(512 - Len(Folder))
X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
If X Then
wPos = InStr(szPath, Chr(0))
SelectFolder = Left$(szPath, wPos - 1)
Else
SelectFolder = ""
End If
End Function
je rend visible mon bouton lister :
Private Sub edit_repertoire_Change()
If IsNull(edit_repertoire.Text) Or Len(edit_repertoire.Text) = 0 Then
btn_lister.Visible = False
Else
btn_lister.Visible = True
End If
End Sub
fonction de mon bouton lister (c'est la ou je bloque :S)
Private Sub btn_lister_Click()
Dim ext As String
Dim Ctr As Integer
Const myRep = "edit_repertoire.Text" ' Voir s'il ne faut pas definir repertoire...
ext = Dir(myRep & "*.doc")
List_rep.Visible = True
List_rep.DefaultValue = ""
Do While ext <> ""
List_rep.ControlSource = ext
ext = Dir
Loop
List_rep.ListStyle = fmListStyleOption
Next
End Sub
J'ai essayé de changé ma procédure btn_list click par :
Private Sub btn_lister_Click()
Dim ext As String
Const myRep = "edit_repertoire.Text" ' Voir s'il ne faut pas definir repertoire...
ext = Dir(myRep & "*.doc")
List_rep.Visible = True
List_rep.RowSource = ""
Do While ext <> ""
List_rep.RowSource = List_rep.RowSource & ext & ";"
ext = Dir
Loop
'List_rep.ListStyle = fmListStyleOption
End Sub
Mais sans résultat...Si qqun pourrait m'aider ca serait vraiment sympa...
TomIlliev
Messages postés86Date d'inscriptionvendredi 27 décembre 2002StatutMembreDernière intervention13 octobre 20162 20 oct. 2006 à 04:50
Salut
Voici une sub pour lister les fichiers d'un dossier dans une liste
Sub LoadFileName(Lst as ListBox, Byval Path as String)
with lst
.visible = true
.clear
path=Dir(Path)
do while not path=""
.additem Path
path=dir
loop
end with
end sub
LoadFileName List1, "edit_repertoire.Text\*.Doc"
Il faut faire Attention à bien définir le chemin de début
A+
TomIlliev
mortalino
Messages postés6786Date d'inscriptionvendredi 16 décembre 2005StatutMembreDernière intervention21 décembre 201118 20 oct. 2006 à 05:37
Salut,
il y a aussi la possibilité d'utiliser la fonction de PCPT (sur codyx) qui gère les fichiers cachés (et option filtre, de l'extension) :
Function GetFilesPathFromDirectory(ByVal sDir As String, ByRef aRet() As String, Optional ByVal sFilter As String = "*.txt") As Long
' GetFilesPathFromDirectory retourne -1 si aucun fichier trouvé
' sinon retourne la dimension du tableau, donc NB fichiers -1 (tableau commence à l'indice 0)
' init les résultats
GetFilesPathFromDirectory = -1
Erase aRet
If RightB$(sDir, 2) <> "" Then sDir = sDir & ""
' formate le chemin
Dim sFile As String, lIndex As Long
sFile = Dir(sDir & sFilter, vbHidden Or vbSystem)
' redimentionne après vérif premier fichier
If sFile <> vbNullString Then
lIndex = 0
ReDim aRet(lIndex)
aRet(lIndex) = sDir & sFile
sFile = Dir
' boucle sur tous les fichiers
Do While sFile <> vbNullString
lIndex = UBound(aRet) + 1
ReDim Preserve aRet(lIndex)
aRet(lIndex) = sDir & sFile
sFile = Dir
Loop
' retour
GetFilesPathFromDirectory = lIndex
End If
End Function
' EXEMPLE D'UTILISATION
Private Sub Command1_Click()
Dim aResultat() As String
Dim lRet As Long
Dim i As Long
lRet = GetFilesPathFromDirectory("C:\Windows", aResultat())
If lRet <> -1 Then
For i = 0 To lRet
Debug.Print "Fichier " & i + 1 & " = " & aResultat(i)
Next i
End If
End Sub
mortalino
Messages postés6786Date d'inscriptionvendredi 16 décembre 2005StatutMembreDernière intervention21 décembre 201118 23 oct. 2006 à 10:17
Rien de compliquer, c'est l'avantage d'une fonction sortant de Codyx, elle est [presque] toute prête :
' EXEMPLE D'UTILISATION
' ouvre un nouveau projet (projet exe), appuie sur Ctrl + G (fenêtre execution s'affiche)
Private Sub Form_Load()
Dim aResultat() As String
Dim lRet As Long
Dim i As Long
lRet = GetFilesPathFromDirectory("C:\Windows", aResultat())
' Tu n'as que C:\Windows à remplacer, par le bon chemin du dossier
If lRet <> -1 Then
For i = 0 To lRet
Debug.Print "Fichier " & i + 1 & " = " & aResultat(i)
Next i
End If
Function GetFilesPathFromDirectory(ByVal sDir As String, ByRef aRet() As String, Optional ByVal sFilter As String = "*.txt") As Long
GetFilesPathFromDirectory = -1
Erase aRet
If RightB$(sDir, 2) <> "" Then sDir = sDir & ""
Dim sFile As String, lIndex As Long
sFile = Dir(sDir & sFilter, vbHidden Or vbSystem)
If sFile <> vbNullString Then
lIndex = 0
ReDim aRet(lIndex)
aRet(lIndex) = sDir & sFile
sFile = Dir
Do While sFile <> vbNullString
lIndex = UBound(aRet) + 1
ReDim Preserve aRet(lIndex)
aRet(lIndex) = sDir & sFile
sFile = Dir
Loop
GetFilesPathFromDirectory = lIndex
End If
End Function
Colle ces codes, la fonction, il ne faut pas y toucher, les seuls choses à faire sont mis en rouge.
Une fois tout cela collé, appuie sur F5 et tu verras le résultat s'afficher dans la fenêtre d'execution.
@++
<hr width="100%" size="2" />
--Mortalino--
Le mystérieux chevalier, "Provençal, le Gaulois"
/DIV>
mortalino
Messages postés6786Date d'inscriptionvendredi 16 décembre 2005StatutMembreDernière intervention21 décembre 201118 23 oct. 2006 à 11:48
Si edit_repertoire contient le chemin du dossier à lire, oui !
La foncion est à coller après toutes tes procédures (sub)
Dans le code suivant :
If lRet <> -1 Then
For i = 0 To lRet
Debug.Print "Fichier " & i + 1 & " = " & aResultat(i)
Next i
End If
lRet contient le nombre total de fichier (d'où son utilisation dans la boucle)
aResultat(i) contient le nom du fichier i, i étant de 1 au nombre de total de fichiers (donc lRet).
Je t'avais dis d'afficher la fenêtre d'exécution, car debug.print veut dire "insrit dans la fenêtre d'exécution tel résultat", en l'occurence, les noms de ces fichiers.
A toi de remplacer le debug.Print par ce que tu veux faire, par exemple, si tu veux placer les données dans une ListBox :
If lRet <> -1 Then
For i = 0 To lRet
List1.AddItem "Fichier " & i + 1 & " = " & aResultat(i)
Next i
End If
bonne prog.
@++
<hr width="100%" size="2" />
--Mortalino--
Le mystérieux chevalier, "Provençal, le Gaulois"
/DIV>
mortalino
Messages postés6786Date d'inscriptionvendredi 16 décembre 2005StatutMembreDernière intervention21 décembre 201118 23 oct. 2006 à 11:50
Ah et quand je disais "La fonction est à coller après toutes tes procédures (sub)"
par contre tout ce qui dans Form_Load, c'est ce code là que tu dois placer dans l'évènement Click de ton bouton btn_lister
@++
<hr width="100%" size="2" />
--Mortalino--
Le mystérieux chevalier, "Provençal, le Gaulois"
/DIV>
ushiwasasuke
Messages postés13Date d'inscriptionvendredi 3 mars 2006StatutMembreDernière intervention 1 octobre 2008 23 oct. 2006 à 14:52
je récapitule...
dans la procédure forme load :
Private Sub Form_Load()
Dim aResultat() As String
Dim lRet As Long
Dim i As Long
lRet = GetFilesPathFromDirectory("C:\Windows", aResultat())
' Tu n'as que C:\Windows à remplacer, par le bon chemin du dossier
If lRet <> -1 Then
For i = 0 To lRet
Debug.Print "Fichier " & i + 1 & " = " & aResultat(i)
Next i
End If
Function GetFilesPathFromDirectory(ByVal sDir As String, ByRef aRet() As String, Optional ByVal sFilter As String = "*.txt") As Long
GetFilesPathFromDirectory = -1
Erase aRet
If RightB$(sDir, 2) <> "" Then sDir = sDir & ""
Dim sFile As String, lIndex As Long
sFile = Dir(sDir & sFilter, vbHidden Or vbSystem)
If sFile <> vbNullString Then
lIndex = 0
ReDim aRet(lIndex)
aRet(lIndex) = sDir & sFile
sFile = Dir
Do While sFile <> vbNullString
lIndex = UBound(aRet) + 1
ReDim Preserve aRet(lIndex)
aRet(lIndex) = sDir & sFile
sFile = Dir
Loop
GetFilesPathFromDirectory = lIndex
End If
End Function
Ensuite dans la procédure btn_lister
If lRet <> -1 Then
For i = 0 To lRet
List1.AddItem "Fichier " & i + 1 & " = " & aResultat(i)
Next i
End If
Ensuite dans la procédure btn_lister Pkoi chez moi la propriété AddItem est inconnue ??? Y'a-t-il une libraire ou un dll à installer ? Je suis sous Access 2000
ushiwasasuke
Messages postés13Date d'inscriptionvendredi 3 mars 2006StatutMembreDernière intervention 1 octobre 2008 23 oct. 2006 à 14:56
Ah oui mais enfait la procédure Forme Load est juste pour rechercher le path c'est bien ca ???
Si oui je n'en ai pas besoin étant donner que je le récupère déjà avec la procédure SelectFolder :
Public Function SelectFolder(Optional Folder As String = "" _
, Optional NetWorkFolders As NetWork = WithNetworkFolders _
) As String
Dim X As Long, bi As BROWSEINFO, dwIList As Long
Dim szPath As String, wPos As Integer If Folder "" Then Folder CurrentProject.Path
With bi
.hOwner = hWndAccessApp
.lpszTitle = "Sélectionnez votre dossier et cliquez sur OK"
.ulFlags = BIF_RETURNONLYFSDIRS _
Or BIF_USENEWUI _
Or NetWorkFolders
End With
dwIList = SHBrowseForFolder(bi)
szPath = Folder & Space$(512 - Len(Folder))
X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
If X Then
wPos = InStr(szPath, Chr(0))
SelectFolder = Left$(szPath, wPos - 1)
Else
SelectFolder = ""
End If
End Function