If with . Attachement (1 Viewer)

kitty77

Registered User.
Local time
Today, 10:40
Joined
May 27, 2019
Messages
712
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?
 

LarryE

Active member
Local time
Today, 07:40
Joined
Aug 18, 2021
Messages
591
Code:
If IsNull([Msample]) Then
    .Attachments.Add "\\Web\bass1\pdf\" & [Msample] & ".pdf"
Else
    .Attachments.Add "\\Web\bass2\pdf\" & [Msample] & ".pdf"
End If
 

kitty77

Registered User.
Local time
Today, 10:40
Joined
May 27, 2019
Messages
712
Thanks.
 

cheekybuddha

AWF VIP
Local time
Today, 15:40
Joined
Jul 21, 2014
Messages
2,280
Or use IIF()
Code:
.Attachments.Add "\\Web\bass" & IIf(IsNull([Msample]), "1", "2") & "\pdf\" & [Msample] & ".pdf"
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:40
Joined
Oct 29, 2018
Messages
21,473
I don't get it. If Msample is null, did you want the name of the pdf file to be simply ".pdf?" (No filename?)
 

kitty77

Registered User.
Local time
Today, 10:40
Joined
May 27, 2019
Messages
712
No, if Msample is null then xxx.pdf else zzz.pdf
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:40
Joined
Oct 29, 2018
Messages
21,473
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?
 

kitty77

Registered User.
Local time
Today, 10:40
Joined
May 27, 2019
Messages
712
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"
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:40
Joined
Oct 29, 2018
Messages
21,473
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!
 

Neros

New member
Local time
Today, 11:40
Joined
Oct 14, 2023
Messages
6
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.
 

Cronk

Registered User.
Local time
Tomorrow, 00:40
Joined
Jul 4, 2013
Messages
2,772
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

Top Bottom