Show folder content pdf files in a listbox (1 Viewer)

Alhakeem1977

Registered User.
Local time
Tomorrow, 01:00
Joined
Jun 24, 2017
Messages
308
How can I get the file names of a folder on my desktop called 'Target': "C:\Users\Basel\Desktop\Target" in a ListBox1 on MS Access form called Form1?

I succeed to get the pdf files in a web browser control, but I prefer to have them as a text in my list box1 and can open them from the Listbox and can move them to another folder instead.

I have the below code but I am not getting any result:
Code:
Private Sub UserForm_initialize()
Dim Filename As String
Dim Form1 As Form
Filename = Dir("C:\Users\Basel\Desktop\Target" & "\*.pdf", vbNormal)
Do While Len(Filename) > 0
Form1.ListBox1.AddItem Filename

Filename = Dir()
Loop

End Sub

Your earliest response would be highly appreciated.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:00
Joined
Sep 21, 2011
Messages
14,299
Code works if folder path is correct.

I used Me.ListBox1, no need to define a form. ?

Walk through the code line by line.
 
Last edited:

Alhakeem1977

Registered User.
Local time
Tomorrow, 01:00
Joined
Jun 24, 2017
Messages
308
Code works if folder path is correct.

I used Me.ListBox1, no need to define a form. ?

Walk through the code line by line.

Thanks for your earliest response.

I have changed the folder path to:
Filename = Dir("C:\Users\redha\Desktop\Target", vbNormal)
but it still not working.:banghead:

Actually, I got the code from Youtube it's VBA MS Excel I want to have it in MS Access form I do not know how to achieve this, please see the link: https://www.youtube.com/watch?v=2tiu8Ht-0NA

it's 4 minutes video it's exactly what I want.

Thanks again!
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:00
Joined
Sep 21, 2011
Messages
14,299
Well that is a path, but it does have to exist. You do not get an error if it does not. I inadvertently used your path to start with.

You need the Me prefix !
At least that works for me?

Code:
Private Sub UserForm_initialize()
Dim Filename As String
'Dim Form1 As Form
Filename = Dir("C:\Temp\", vbNormal)
Do While Len(Filename) > 0
[COLOR="Red"]Me[/COLOR].ListBox1.AddItem Filename

Filename = Dir()
Loop

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:00
Joined
Sep 21, 2011
Messages
14,299
Just watched that video. He uses

Code:
UserForm.ListBox1.AddItem Filename
 

Alhakeem1977

Registered User.
Local time
Tomorrow, 01:00
Joined
Jun 24, 2017
Messages
308
Code:
Private Sub Form_Current()

Dim Filename As String
Filename = Dir("C:\Users\Basel\Desktop\Target" & "\*.pdf", vbNormal)
Do While Len(Filename) > 0
Me.ListBox3.AddItem Filename

Filename = Dir()
Loop

End Sub

It's working now :)

One more thing now how can I open the file when I double click on it?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:00
Joined
May 7, 2009
Messages
19,242
use application.FollowHyperlink:
on the doubleclick event of your listbox:
Code:
Private Sub Listboxname_DblClick(Cancel As Integer)
Application.FollowHyperlink "C:\Users\Basel\Desktop\Target\" & Me.Listboxname.Value
End Sub
 

Alhakeem1977

Registered User.
Local time
Tomorrow, 01:00
Joined
Jun 24, 2017
Messages
308
use application.FollowHyperlink:
on the doubleclick event of your listbox:
Code:
Private Sub Listboxname_DblClick(Cancel As Integer)
Application.FollowHyperlink "C:\Users\Basel\Desktop\Target\" & Me.Listboxname.Value
End Sub
Thank you so much it's working [emoji817]

Sent from my HUAWEI NXT-L29 using Tapatalk
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:00
Joined
May 7, 2009
Messages
19,242
youre welcome.
 

Users who are viewing this thread

Top Bottom