If with . Attachement

kitty77

Registered User.
Local time
Today, 17:45
Joined
May 27, 2019
Messages
715
I'm using the following...

.Attachments.Add "\\Web\bass\pdf\" & [Msample] & ".pdf"

I would like to create an if statement for the attachment line.

If IsNull([Msample]) Then .Attachments.Add "\\Web\bass1\pdf\" & [Msample] & ".pdf" Else .Attachments.Add "\\Web\bass2\pdf\" & [Msample] & ".pdf"

Do I put the IF before the .Attachement or after or like I have it?
 
Code:
If IsNull([Msample]) Then
    .Attachments.Add "\\Web\bass1\pdf\" & [Msample] & ".pdf"
Else
    .Attachments.Add "\\Web\bass2\pdf\" & [Msample] & ".pdf"
End If
 
Thanks.
 
Or use IIF()
Code:
.Attachments.Add "\\Web\bass" & IIf(IsNull([Msample]), "1", "2") & "\pdf\" & [Msample] & ".pdf"
 
I don't get it. If Msample is null, did you want the name of the pdf file to be simply ".pdf?" (No filename?)
 
No, if Msample is null then xxx.pdf else zzz.pdf
 
No, if Msample is null then xxx.pdf else zzz.pdf
And xxx.pdf still needs to be on base1 and zzz.pdf on base2 folders?

Edit: What values are in Msample? Where would xxx and zzz come from?
 
The value for Msample is at the beginning of my code. That part works fine. I'm going to try below.

If IsNull([Msample]) Then .Attachments.Add "\\Web\bass1\pdf\" & [Msample] & ".pdf" Else .Attachments.Add "\\Web\bass2\pdf\" & [Msample] & ".pdf"
 
The value for Msample is at the beginning of my code. That part works fine. I'm going to try below.

If IsNull([Msample]) Then .Attachments.Add "\\Web\bass1\pdf\" & [Msample] & ".pdf" Else .Attachments.Add "\\Web\bass2\pdf\" & [Msample] & ".pdf"
If you try that, make sure you try it with Msample is null and let us know how it goes. Good luck!
 
You can take Larry's suggestion with some changes...
Code:
Dim strFilePath as String
If IsNull([Msample]) Then
    strFilePath = "\\Web\bass1\pdf\" & [Msample] & ".pdf"
Else
    strFilePath = "\\Web\bass2\pdf\" & [Msample] & ".pdf"
End If
.Attachments.Add strFilePath

And before the .Attachment.Add, you need to check if the file really exists with FileSystemObjects' FileExists method.
 
What DBguy is saying is that if Msample is Null then Msample & ".pdf" will be ".pdf" which is an invalid file name
 

Users who are viewing this thread

Back
Top Bottom