Solved

MS Access VBA SQL where statement

Posted on 2013-06-03
10
575 Views
Last Modified: 2013-06-04
Hello Experts,
Please help me to include one more criteria in the MS Access VBA SQL where statement.   The code below works but I need to include where TblEmailPayments.Attachment = “Workbook” but every one of my syntax attempts is incorrect.

Thanks
Private Sub EmailWorkbook_Click()
DoCmd.SetWarnings False
Dim TblEmailPayments As Recordset
Dim strSql As String
Dim EmailAddress As String
Dim Contact As String
Dim Attachment As String
Dim FirstName As String
Dim FileName As String
Dim mess_body As String
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
     Set appOutLook = CreateObject("Outlook.Application")
     Set MailOutLook = appOutLook.CreateItem(olMailItem)
Dim ContactEM As String
Dim FirstnameC As String
Dim SourceTable As String
    Set db = CurrentDb
    Set rstTables = db.OpenRecordset("TblEmailPayments")

Dim oApp, unzipfile, pathname, location

rstTables.MoveFirst

Do While Not rstTables.EOF
DoCmd.OpenQuery "QdelTblEmailPayTemp", acViewNormal, acEdit
     strSql = "INSERT INTO TblEmailPayTemp([Contact],[EmailAddress],[FirstName], [Attachment])" & _
     "SELECT TblEmailPayments.Contact, TblEmailPayments.EmailAddress, TblEmailPayments.FirstName, TblEmailPayments.Attachment FROM TblEmailPayments WHERE TblEmailPayments.Contact = " & Chr$(34) & rstTables.Contact & Chr$(34) & ";"

    Set oApp = CreateObject("Shell.Application")
    pathname = "H:\PDF\"
   DoCmd.RunSQL strSql
   
   FirstnameC = DLookup("Firstname", "TblEmailPayTemp")
            Set appOutLook = CreateObject("Outlook.Application")
            Set MailOutLook = appOutLook.CreateItem(olMailItem)
            With MailOutLook
            .BodyFormat = olFormatHTML
            .To = DLookup("EmailAddress", "TblEmailPayTemp")
            .Subject = "Financial Closing Details"
            .HTMLBody = (FirstnameC) & ",   " & "<BR>" & "<BR>" & _
            vbLf & "Attached is the detail Financial Closing report. " & "<BR>" & "<BR>" & _
            "If you have any questions, please mail " & _
            "Mail Stop 01-660"
            .Attachments.Add pathname & "MasterReport.xls"
            .Close 0 '0 = olSave
            End With
            rstTables.MoveNext
        Set MailOutLook = Nothing
          Loop
              
    Set oApp = Nothing
DoCmd.SetWarnings True
    Beep
    MsgBox "The Financial Report was emailed to all Accounts and is in your Drafts folder!!!"


End Sub

Open in new window

0
Comment
Question by:CFMI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 20

Expert Comment

by:dsacker
ID: 39216931
You should be able to code the single quotes and not have to use Chr$(34), too:

Either of these should work:

DoCmd.OpenQuery "QdelTblEmailPayTemp", acViewNormal, acEdit
     strSql = "INSERT INTO TblEmailPayTemp([Contact],[EmailAddress],[FirstName], [Attachment])" & _
     "SELECT TblEmailPayments.Contact, TblEmailPayments.EmailAddress, TblEmailPayments.FirstName, TblEmailPayments.Attachment FROM TblEmailPayments WHERE TblEmailPayments.Contact = " & Chr$(34) & rstTables.Contact & Chr$(34) & " AND TblEmailPayments.Attachment = " & Chr$(34) & "Workbook" & Chr$34) & ";"

Open in new window

OR

DoCmd.OpenQuery "QdelTblEmailPayTemp", acViewNormal, acEdit
     strSql = "INSERT INTO TblEmailPayTemp([Contact],[EmailAddress],[FirstName], [Attachment])" & _
     "SELECT TblEmailPayments.Contact, TblEmailPayments.EmailAddress, TblEmailPayments.FirstName, TblEmailPayments.Attachment FROM TblEmailPayments WHERE TblEmailPayments.Contact = '" & rstTables.Contact & "' AND TblEmailPayments.Attachment = 'Workbook';"

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 39216936
Try this:
     strSql = "INSERT INTO TblEmailPayTemp([Contact],[EmailAddress],[FirstName], [Attachment])" & _
     "SELECT TblEmailPayments.Contact, TblEmailPayments.EmailAddress, TblEmailPayments.FirstName, TblEmailPayments.Attachment FROM TblEmailPayments WHERE TblEmailPayments.Contact = " & Chr$(34) & rstTables.Contact & Chr$(34) & _
" And TblEmailPayments.Attachment = " & Chr(34) & Workbook &  & Chr(34) 

Open in new window

0
 
LVL 1

Author Comment

by:CFMI
ID: 39217002
Experts, The first and the third statement receive "Compile Error"  whereas the second statement compiles but when running the temp table has no records and the message, "Invalid use of Null" appears.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 20

Expert Comment

by:dsacker
ID: 39217025
Can you run it in debug mode and paste the contents of strSql here? I'm breaking down the query for readability, and to see if there is a syntax issue (coding just the strSql part):

    strSql = "INSERT INTO TblEmailPayTemp " & _
    "([Contact], [EmailAddress], [FirstName], [Attachment]) " & _
    "SELECT TblEmailPayments.Contact, " & _
            "TblEmailPayments.EmailAddress, " & _
            "TblEmailPayments.FirstName, " & _
            "TblEmailPayments.Attachment " & _
    "FROM TblEmailPayments " & _
    "WHERE TblEmailPayments.Contact = '" & rstTables.Contact & "' " & _
    "AND TblEmailPayments.Attachment = 'Workbook'"

Open in new window

Already I found a needed space at the end of the INSERT INTO line.
0
 
LVL 1

Author Comment

by:CFMI
ID: 39217115
It fails when comparing contact (rstTables.Contact has a name but TblEmailPayments.Contact  does not):
"WHERE TblEmailPayments.Contact = '" & rstTables.Contact & "' " & _

But this statement by itself works well and I have been using this for years:
WHERE TblEmailPayments.Contact = " & Chr$(34) & rstTables.Contact & Chr$(34) & ";"
0
 
LVL 20

Expert Comment

by:dsacker
ID: 39217124
If it works with a Chr$(34), which is a double-quote, perhaps the Workbooks criteria will work with it as well. Try this:

strSql = "INSERT INTO TblEmailPayTemp " & _
    "([Contact], [EmailAddress], [FirstName], [Attachment]) " & _
    "SELECT TblEmailPayments.Contact, " & _
            "TblEmailPayments.EmailAddress, " & _
            "TblEmailPayments.FirstName, " & _
            "TblEmailPayments.Attachment " & _
    "FROM TblEmailPayments " & _
    "WHERE TblEmailPayments.Contact = " & Chr$(34) & rstTables.Contact & Chr$(34) & " " & _
    "AND TblEmailPayments.Attachment = " & Chr$(34) & "Workbook" & Chr$(34)

Open in new window

0
 
LVL 20

Expert Comment

by:dsacker
ID: 39217126
Just curious: When you say that "rstTables.Contact has a name but TblEmailPayments.Contact  does not", do you mean that if you do a SELECT * FROM TblEmailPayments, your Contact column has no data in it (is NULL)?
0
 
LVL 1

Author Comment

by:CFMI
ID: 39217152
TblEmailPayments has 6 records with populated fields but inside VBA when I type:
    ?rstTables.Contact - displays a name
    ?TblEmailPayments.Contact -  Null value

Is it possible that the 2 different criteria fields in the where statement are different?
0
 
LVL 20

Accepted Solution

by:
dsacker earned 500 total points
ID: 39217160
Perhaps you did not fist run your query to bring back the rstTables data before the strSQL query?

I didn't look closely at your logic flow, but you'd have to first run that and have opened that recordset before the DoCmd.
0
 
LVL 1

Author Closing Comment

by:CFMI
ID: 39218616
Correction of the logic sequence solved the problem - Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question