Solved

MS Access VBA SQL where statement

Posted on 2013-06-03
10
539 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Get an idea of what you should include in an email disclaimer with these Top 5 email disclaimer tips.
If you don't know how to downgrade, my instructions below should be helpful.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now