If Then evaluates incorrectly. (1 Viewer)

foshizzle

Registered User.
Local time
Today, 09:40
Joined
Nov 27, 2013
Messages
277
Hey guys - Can anyone help me figure out whats wrong with the IF statement below? For some reason, the else statement is evaluating whether or not If ext1 = jpg Or png Or bmp Or jpeg Or tiff Then is true.

I get a Runtime Error 2220 at line Me.cmdAttach1.Picture = fileType1

Code:
Dim fdialog1 As Object
    Dim strfile1 As String
    Dim sourceDir1 As String
    Dim varItem1 As Variant
    Dim fileType1 As String
    Dim ext1 As String
    
    ' Open File Picker
    Set fdialog1 = Application.FileDialog(3)
    fdialog1.allowMultiSelect = False
    If fdialog1.show Then
    For Each varItem1 In fdialog1.selectedItems
    strfile1 = Dir(varItem1)

    ' Get source directory from selected file and set to sourceDir1
    sourceDir1 = Left(varItem1, Len(varItem1) - Len(strfile1))
    
    ' Assign txtAttachment1 object to source folder & file name
    Me.txtAttachment1 = sourceDir1 + strfile1

    ' Get extension from selected item
    ext1 = Right(varItem1, Len(varItem1) - InStrRev(varItem1, "."))
    MsgBox "The ext1 value is " + ext1
    
    ' Check for compatible image for thumbnail
    If ext1 = jpg Or png Or bmp Or jpeg Or tiff Then

        ' Display image thumbnail
        Me.cmdAttach1.Picture = Me.txtAttachment1.Value
    Else        
        ' Set thumbnail icon for non-image files
        fileType1 = "B:\icons\" & Mid(varItem1, InStrRev(varItem1, ".") + 1) & ".png"
    
        ' Display icon for non-image files
        Me.cmdAttach1.Picture = fileType1

    End If
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:40
Joined
May 7, 2009
Messages
19,169
you should revised to:

If ext1= "jpg" Or ext1 = "bmp" Or ext1 = "jpeg" Or ext1 = "tiff" Then
...



Or

If Instr("jpg/bmp/jpeg/tiff", ext1) <> 0 Then
...
 

foshizzle

Registered User.
Local time
Today, 09:40
Joined
Nov 27, 2013
Messages
277
Ah, yes. Thank you. That makes sense.
 

Minty

AWF VIP
Local time
Today, 13:40
Joined
Jul 26, 2013
Messages
10,354
You could also use a Select Case flow which may be better suited and/or easier to follow;

Code:
    Select Case ext1

        Case "jpg", "png", "bmp", "jpeg", "tiff"
         [COLOR="Green"]   ' Display image thumbnail[/COLOR]
            Me.cmdAttach1.Picture = Me.txtAttachment1.Value
        Case Else
         [COLOR="green"]   ' Set thumbnail icon for non-image files[/COLOR]
            fileType1 = "B:\icons\" & Mid(varItem1, InStrRev(varItem1, ".") + 1) & ".png"

         [COLOR="green"]   ' Display icon for non-image files[/COLOR]
            Me.cmdAttach1.Picture = fileType1

    End Select
 

Users who are viewing this thread

Top Bottom