Solved

MS Access VBA SQL where statement

Posted on 2013-06-03
10
586 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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