Add and view images from a folder (1 Viewer)

shamal

Registered User.
Local time
Today, 14:25
Joined
Sep 28, 2013
Messages
77
Welcome..
How to display images and documents depicted in the example with different extensions.

Some images with the *.jpg extension are displayed, while others with the *.png extension are not displayed.
 

Attachments

  • the documents.zip
    540.4 KB · Views: 22

arnelgp

..forever waiting... waiting for jellybean!
Local time
, 05:25
Joined
May 7, 2009
Messages
19,247
try:
Code:
Private Sub Form_Current()
    Dim fileName As String
    Dim path As String
    
    path = CurrentProject.path & "\picto\"
    fileName = Dir$(path & "\" & ID & ".*")
    If Len(fileName) <> 0 Then
        Me.pic.Picture = path & fileName
        
    Else
        Me.pic.Picture = ""
        
    End If

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:25
Joined
Sep 21, 2011
Messages
14,334
Make sure you have an application for that extension.
 

moke123

AWF VIP
Local time
Today, 17:25
Joined
Jan 11, 2013
Messages
3,925
Similiar to Arnels code using filesystem object and using an image control.


Code:
Private Sub Form_Current()

    If Not Me.NewRecord Then
        sShowPicture
    Else
        Me.Image2.Picture = ""
    End If

End Sub

Private Sub sShowPicture()

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

Dim fol As Object, fil As Object, FN As String

Set fol = fso.GetFolder(CurrentProject.Path & "\picto")

For Each fil In fol.files

FN = Mid(fil.Name, 1, InStrRev(fil.Name, ".") - 1)

If FN = Me.ID Then Me.Image2.Picture = fil.Path

Next

End Sub

note that you will have issues if you have 2 files like 1.jpg and 1.png
 

Attachments

  • example.mdb
    944 KB · Views: 17
Last edited:

shamal

Registered User.
Local time
Today, 14:25
Joined
Sep 28, 2013
Messages
77
try:
Code:
Private Sub Form_Current()
    Dim fileName As String
    Dim path As String
   
    path = CurrentProject.path & "\picto\"
    fileName = Dir$(path & "\" & ID & ".*")
    If Len(fileName) <> 0 Then
        Me.pic.Picture = path & fileName
       
    Else
        Me.pic.Picture = ""
       
    End If

End Sub
Thanks for your wonderful answer!!
 

shamal

Registered User.
Local time
Today, 14:25
Joined
Sep 28, 2013
Messages
77
How can I retrieve images if they are in more than one folder?
As in the attachment.
 

Attachments

  • picto.zip
    1.3 MB · Views: 18

shamal

Registered User.
Local time
Today, 14:25
Joined
Sep 28, 2013
Messages
77
Similiar to Arnels code using filesystem object and using an image control.


Code:
Private Sub Form_Current()

    If Not Me.NewRecord Then
        sShowPicture
    Else
        Me.Image2.Picture = ""
    End If

End Sub

Private Sub sShowPicture()

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

Dim fol As Object, fil As Object, FN As String

Set fol = fso.GetFolder(CurrentProject.Path & "\picto")

For Each fil In fol.files

FN = Mid(fil.Name, 1, InStrRev(fil.Name, ".") - 1)

If FN = Me.ID Then Me.Image2.Picture = fil.Path

Next

End Sub

note that you will have issues if you have 2 files like 1.jpg and 1.png
Thank you for your answer!
 

moke123

AWF VIP
Local time
Today, 17:25
Joined
Jan 11, 2013
Messages
3,925
How can I retrieve images if they are in more than one folder?
As in the attachment.
You could use a recursive procedure to iterate through all the folders but you'd need to organize the folders inside a main folder.

I would consider using another naming convention than using the PKey. Also declare option explicit in every module.

if you put all the picto folders into a folder "AllPicto" you can iterate the subfolders

Code:
Private Sub Command2_Click()

sGetallPicto CurrentProject.path & "\AllPicto\"

End Sub

Private Sub sGetallPicto(pth As String)

    ' requires a reference to microsoft scripting runtime.
    Dim fso As New FileSystemObject
    Dim fol As Folder, fil As file, sfol As Folder

    Set fol = fso.GetFolder(pth)

    For Each fil In fol.Files

        Debug.Print fil.path

    Next

        For Each sfol In fol.SubFolders
            sGetallPicto sfol.path
        Next

End Sub
 

shamal

Registered User.
Local time
Today, 14:25
Joined
Sep 28, 2013
Messages
77
You could use a recursive procedure to iterate through all the folders but you'd need to organize the folders inside a main folder.

I would consider using another naming convention than using the PKey. Also declare option explicit in every module.

if you put all the picto folders into a folder "AllPicto" you can iterate the subfolders

Code:
Private Sub Command2_Click()

sGetallPicto CurrentProject.path & "\AllPicto\"

End Sub

Private Sub sGetallPicto(pth As String)

    ' requires a reference to microsoft scripting runtime.
    Dim fso As New FileSystemObject
    Dim fol As Folder, fil As file, sfol As Folder

    Set fol = fso.GetFolder(pth)

    For Each fil In fol.Files

        Debug.Print fil.path

    Next

        For Each sfol In fol.SubFolders
            sGetallPicto sfol.path
        Next

End Sub
Can you apply it to the example I sent?
 

moke123

AWF VIP
Local time
Today, 17:25
Joined
Jan 11, 2013
Messages
3,925
I get errors on your example. not sure if it's a language issue or corruption.
 

Users who are viewing this thread

Top Bottom