ImageFrame to open as a picture using wildcard (1 Viewer)

johnkrytus

Registered User.
Local time
Yesterday, 19:51
Joined
Mar 7, 2013
Messages
91
I want to my ImageFrame to open as a picture. My problem is that my picture could be either a .jpg or a .png (or whatever). The code below works if I hard code profile_pic.png or profile_pic.jpg. Is there a way to use the wildcard here? (Its not working as written now)

Code:
    Dim vFolderPath As String, strFile As String, dirFile As String
    
    vFolderPath = DLookup("FolderName", "tblCodes-FolderControl", "FolderKey = '" & "Profile" & "'")
    dirFile = Dir(vFolderPath & ctrl_people_id & " *", vbDirectory)
    strFile = "\profile_pic." + "*"
    
    If dirFile <> "" Then
        Me![ctrl_ImageFrame].Picture = vFolderPath & dirFile & strFile
    Else
        MsgBox "No directory for this person"
    End If
 

pr2-eugin

Super Moderator
Local time
Today, 00:51
Joined
Nov 30, 2011
Messages
8,494
Why is the code not working for you?
Code:
    Dim vFolderPath As String, strFile As String, dirFile As String
    
    vFolderPath = [COLOR=Red][B]Nz([/B][/COLOR]DLookup("FolderName", "tblCodes-FolderControl", "FolderKey = [COLOR=Red][B]'" & Profile & "'"[/B][/COLOR])[COLOR=Red][B], "Invalid")[/B][/COLOR]
    dirFile = Dir(vFolderPath & ctrl_people_id & "*", vbDirectory)
    strFile = "\profile_pic[COLOR=Red][B].*[/B][/COLOR]"
    
    If dirFile <> [COLOR=Red][B]vbNullString[/B][/COLOR] Then
        [COLOR=Red][B]Me.[/B][/COLOR][ctrl_ImageFrame].Picture = vFolderPath & dirFile & strFile
    Else
        MsgBox "No directory for this person"
    End If
 

johnkrytus

Registered User.
Local time
Yesterday, 19:51
Joined
Mar 7, 2013
Messages
91
I get an error saying: can't open the file 'X:\11484 Charles Prue\profile_pic.*' If I take out the * and put in the appropriate extension, then it works
 

johnkrytus

Registered User.
Local time
Yesterday, 19:51
Joined
Mar 7, 2013
Messages
91
Re: ImageFrame won't display while using wildcard to call it

pr2-eugin, I just now saw your suggested improvements. Thank you.
However, it still doesn't work. It just will not read the file unless I specify the type.

also..the word Profile is a value in a table so still needs to be in quotes. So..if the FolderKey is "Profile" then the value of FolderName is a particular mapped path.

also, also..I can see why vbNullString would perhaps be the proper way to do this but when I use "" the code is very fast. vbNullString makes it churn.

Thoughts?
 
Last edited:

pr2-eugin

Super Moderator
Local time
Today, 00:51
Joined
Nov 30, 2011
Messages
8,494
Okay I think I found the problem, try this,
Code:
    Dim vFolderPath As String, strFile As String, dirFile As String
    
    vFolderPath = Nz(DLookup("FolderName", "tblCodes-FolderControl", "FolderKey = '" & Profile & "'"), "Invalid")
    dirFile = vFolderPath & "\" & Dir(vFolderPath & ctrl_people_id & "*", vbDirectory)
    dirFile = dirFile & "\profile_pic.*"
    
    If Dir(dirFile) <> vbNullString Then
        Me.[ctrl_ImageFrame].Picture = dirFile
    Else
        MsgBox "No directory for this person"
    End If
 

johnkrytus

Registered User.
Local time
Yesterday, 19:51
Joined
Mar 7, 2013
Messages
91
Wow my code was really screwed up (that's what I get for copy/pasting).
Here is what I have now....same exact result.
With the extension coded in it works perfectly. Without I get the following output --->>> X:\19873 Oasis Williams\profile_pic.*

Shouldn't my output be the actual extension and not the *?

Code:
    Dim vFolderPath As String, dirFile As String
    
    vFolderPath = Nz(DLookup("FolderName", "tblCodes-FolderControl", "FolderKey = '" & "Profile" & "'"))
    dirFile = vFolderPath & Dir(vFolderPath & ctrl_people_id & " *", vbDirectory)
    dirFile = dirFile & "\profile_pic.*"
    
    Debug.Print dirFile
    
    On Error Resume Next
    If dirFile <> "" Then
        Me.[ctrl_ImageFrame].Picture = dirFile
    Else
        MsgBox "No directory for this person"
    End If
 

pr2-eugin

Super Moderator
Local time
Today, 00:51
Joined
Nov 30, 2011
Messages
8,494
Ah ! I am so sorry :eek:
Code:
    Dim vFolderPath As String, strFile As String, dirFile As String
    
    vFolderPath = Nz(DLookup("FolderName", "tblCodes-FolderControl", "FolderKey = '" & Profile & "'"), "Invalid")
    dirFile = vFolderPath & "\" & Dir(vFolderPath & ctrl_people_id & "*", vbDirectory)
    [COLOR=Red][B]strFile = dirFile & "\profile_pic.*"[/B][/COLOR]
    
    If [COLOR=Red][B]Dir(strFile)[/B][/COLOR] <> vbNullString Then
        Me.[ctrl_ImageFrame].Picture = [COLOR=Red][B]dirFile & "\" & Dir(strFile)[/B][/COLOR]
    Else
        MsgBox "No directory for this person"
    End If
 

johnkrytus

Registered User.
Local time
Yesterday, 19:51
Joined
Mar 7, 2013
Messages
91
Beautiful!

Here is the final version that I put on the On Current event of the form. When the form opens, if the person has a profile_pic in their file, then it will display. If not then it will display a placeholder.

My next hurdle will be to check for a max size because I dont want to slow the form down.

Thank you pr2-eugin for all of your help!

Code:
    Dim vFolderPath As String, dirFile As String, strFile As String
    vFolderPath = Nz(DLookup("FolderName", "tblCodes-FolderControl", "FolderKey = '" & "Profile" & "'"))
    dirFile = vFolderPath & Dir(vFolderPath & ctrl_people_id & " *", vbDirectory)
    strFile = dirFile & "\profile_pic.*"
    
    On Error Resume Next
    If Dir(strFile) <> vbNullString Then
        Me.[ctrl_ImageFrame].Picture = dirFile & "\" & Dir(strFile)
    Else
        Me!ctrl_ImageFrame.Picture = "X:\~stuff\profile_icon.png"
    End If
 

Users who are viewing this thread

Top Bottom