Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 01-22-2018, 03:47 AM   #1
helmerr
Newly Registered User
 
Join Date: Nov 2013
Posts: 140
Thanks: 33
Thanked 0 Times in 0 Posts
helmerr is on a distinguished road
If Then evaluates incorrectly.

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

helmerr is offline   Reply With Quote
Old 01-22-2018, 03:52 AM   #2
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: If Then evaluates incorrectly.

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
...
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
The Following User Says Thank You to arnelgp For This Useful Post:
helmerr (01-22-2018)
Old 01-22-2018, 05:03 AM   #3
helmerr
Newly Registered User
 
Join Date: Nov 2013
Posts: 140
Thanks: 33
Thanked 0 Times in 0 Posts
helmerr is on a distinguished road
Re: If Then evaluates incorrectly.

Ah, yes. Thank you. That makes sense.

helmerr is offline   Reply With Quote
Old 01-22-2018, 05:18 AM   #4
Minty
AWF VIP
 
Minty's Avatar
 
Join Date: Jul 2013
Location: UK - Wiltshire
Posts: 6,439
Thanks: 166
Thanked 1,738 Times in 1,707 Posts
Minty is a jewel in the rough Minty is a jewel in the rough Minty is a jewel in the rough
Re: If Then evaluates incorrectly.

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"
            ' Display image thumbnail
            Me.cmdAttach1.Picture = Me.txtAttachment1.Value
        Case 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 Select

__________________
If we have helped please add to our reputation - click the scales symbol on the left, tick 'I approve' and leave a comment.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Minty is offline   Reply With Quote
Reply

Tags
if then , runtime 2220

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
If...Then Evaluates 7 as Greater Than 10! JAB Modules & VBA 10 12-04-2008 04:18 PM
using NZ incorrectly? garethl Queries 10 02-19-2008 07:44 AM
IIf function evaluates falsepart when truepart found BWPanda Modules & VBA 11 02-06-2007 03:26 AM
IIF() evaluates incorrectly? 1eyedjack Reports 2 10-04-2006 03:10 AM
code that evaluates a control source suemack Modules & VBA 3 04-10-2003 06:46 AM




All times are GMT -8. The time now is 05:03 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World