Solved

Access query via VBA problem with quotes

Posted on 2013-01-03
17
440 Views
Last Modified: 2013-01-03
Hi!

I've got an access query (which contains a dlookup) that I'm trying to run via VBA, and it's giving me an error. I think it's a double/single quote problem, but I can't figure it out.

Here's the query:
CurrentDb.Execute "INSERT INTO TPLPaymentDetailT ( PaymentID, PaymentType, Amount, StatementNo ) " & _
    "SELECT " & co2id & " AS PaymentID, 'Payment Based on Audit' AS PaymentType, IIf([partiallypaid]=False, " & _
    "[AmountPaid],[amountpaid]- DLookUp('amount','tplpaymentdetailt','statementno=' & [statementno] & _
    "' AND type = 'Pay Based on Percentage'')) AS Amount, TPLTotalAmountPaidPerCompanyQ.promptstatement AS StatementNo " & _
    "FROM TPLStatementT RIGHT JOIN TPLTotalAmountPaidPerCompanyQ ON TPLStatementT.StatementNo = " & _
    "TPLTotalAmountPaidPerCompanyQ.PromptStatement " & _
    "WHERE (((TPLTotalAmountPaidPerCompanyQ.Company)=2));"

Open in new window

On the fourth line, it gives me an error "Expected: End of Statement"

Can anyone help me fix this? Thanks!
0
Comment
Question by:etech0
  • 8
  • 4
  • 3
  • +1
17 Comments
 
LVL 39

Expert Comment

by:als315
Comment Utility
It is not so simple to debug so complex query. Assign query to variable. In this case you can debug it without problems.
SQL = "INSERT INTO TPLPaymentDetailT ( PaymentID, PaymentType, Amount, StatementNo ) "
SQL = SQL & "SELECT " & co2id & " AS PaymentID, 'Payment Based on Audit' AS PaymentType, IIf([partiallypaid]=False, "
.....
....

debug.print SQL

Open in new window

You will see errors.
0
 
LVL 10

Author Comment

by:etech0
Comment Utility
Okay, I did that. Now the whole SQL= section turned red, and I can't debug it because the code won't run - syntax error.

Any ideas? My guess is that it may be the two single quotes on line 4, but I don't know how else to write that dlookup.
0
 
LVL 39

Expert Comment

by:als315
Comment Utility
Place result here.
0
 
LVL 10

Author Comment

by:etech0
Comment Utility
Can't do debug.print sql, because I can't get that subroutine running due to syntax error.

sql =  "INSERT INTO TPLPaymentDetailT ( PaymentID, PaymentType, Amount, StatementNo ) " & _
    "SELECT " & co2id & " AS PaymentID, 'Payment Based on Audit' AS PaymentType, IIf([partiallypaid]=False, " & _
    "[AmountPaid],[amountpaid]- DLookUp('amount','tplpaymentdetailt','statementno=' & [statementno] & _
    "' AND type = 'Pay Based on Percentage'')) AS Amount, TPLTotalAmountPaidPerCompanyQ.promptstatement AS StatementNo " & _
    "FROM TPLStatementT RIGHT JOIN TPLTotalAmountPaidPerCompanyQ ON TPLStatementT.StatementNo = " & _
    "TPLTotalAmountPaidPerCompanyQ.PromptStatement " & _
    "WHERE (((TPLTotalAmountPaidPerCompanyQ.Company)=2));"

Open in new window

0
 
LVL 39

Expert Comment

by:als315
Comment Utility
Divide string as I've suggested:
SQL = "first line"
SQL = SQL & "Second line"
You will be able to find line with error. Divide this line to small parts and find problem part.
If you need quote in string, you can use chr(34) or double "". For example:
SQL = SQL & "- DLookUp(" & Chr(34) & "amount" & Chr(34) & ",""tplpaymentdetailt"",""statementno="" & [statementno]"

Open in new window

0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
First, rather then get the value of the dlookup in the statement, fetch the value first into a variable, then use the variable in the SQL statement.

That makes it easier.

Second, why are you using a Dlookup() in a SQL statement?  The domain functions represent SQL statements and are meant to be used in places where SQL is not.  Here you can.

 The problem with them in this context is that the query parser cannot optimize them and you will always get poor performance.

Jim.
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
sql =  "INSERT INTO TPLPaymentDetailT ( PaymentID, PaymentType, Amount, StatementNo ) " & _
    "SELECT " & co2id & " AS PaymentID, 'Payment Based on Audit' AS PaymentType, IIf([partiallypaid]=False, " & _
    "[AmountPaid],[amountpaid]-" & DLookUp('amount','tplpaymentdetailt','statementno=' & [statementno] & _
    "' AND type = 'Pay Based on Percentage'')) & " AS Amount, TPLTotalAmountPaidPerCompanyQ.promptstatement AS StatementNo " & _
    "FROM TPLStatementT RIGHT JOIN TPLTotalAmountPaidPerCompanyQ ON TPLStatementT.StatementNo = " & _
    "TPLTotalAmountPaidPerCompanyQ.PromptStatement " & _
    "WHERE (((TPLTotalAmountPaidPerCompanyQ.Company)=2));"
0
 
LVL 10

Author Comment

by:etech0
Comment Utility
@Jim:
I'm using DLookup in SQL, because I don't know how to do that particular join.

What I need: if a certain field (PartiallyPaid) is flagged, then join with another table and pick up a matching value with criteria with another field. No clue how to do that.

I can't fetch the variable and put it into the sql, because this sql inserts multiple rows, and the target value is different for each row.

@als315: I will try using Chr(34)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 10

Author Comment

by:etech0
Comment Utility
@Jim: your code gives the same error (and the whole block of code turns red)
0
 
LVL 10

Author Comment

by:etech0
Comment Utility
@als315: replacing the quotes for the dlookup with Chr(34)  did not help. This is my new code:
sql =  "INSERT INTO TPLPaymentDetailT ( PaymentID, PaymentType, Amount, StatementNo ) " & _
    "SELECT " & co2id & " AS PaymentID, 'Payment Based on Audit' AS PaymentType, IIf([partiallypaid]=False, " & _
    "[AmountPaid],[amountpaid]- & DLookUp(" & chr(34) & "amount" & chr(34) & "," & chr(34) & "tplpaymentdetailt" & chr(34) & "," & chr(34) & "statementno=' & [statementno] & _
    "' AND type = 'Pay Based on Percentage'" & chr(34) & ")) & " AS Amount, TPLTotalAmountPaidPerCompanyQ.promptstatement AS StatementNo " & _
    "FROM TPLStatementT RIGHT JOIN TPLTotalAmountPaidPerCompanyQ ON TPLStatementT.StatementNo = " & _
    "TPLTotalAmountPaidPerCompanyQ.PromptStatement " & _
    "WHERE (((TPLTotalAmountPaidPerCompanyQ.Company)=2));"

Open in new window

0
 
LVL 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 50 total points
Comment Utility
Break it down as Anthony said:

sql =  "INSERT INTO TPLPaymentDetailT ( PaymentID, PaymentType, Amount, statementNo ) "
sql = sql &  "SELECT " & co2id & " AS PaymentID, 'Payment Based on Audit' AS PaymentType, "
sql = sql & " IIf([partiallypaid]=False,[AmountPaid],[amountpaid]-" & DLookUp('amount','tplpaymentdetailt','statementno=' & [statementno] & "' AND type = 'Pay Based on Percentage'''))
sql = sql & " AS Amount, TPLTotalAmountPaidPerCompanyQ.promptstatement AS statementNo "
sql = sql & "FROM TPLStatementT RIGHT JOIN TPLTotalAmountPaidPerCompanyQ ON TPLStatementT.StatementNo = "
sql = sql & "TPLTotalAmountPaidPerCompanyQ.PromptStatement"
sql = sql &  "WHERE (((TPLTotalAmountPaidPerCompanyQ.Company)=2));"

Think I got it that time, but if not, you get the idea of what Anthony suggested.

Jim.
0
 
LVL 39

Accepted Solution

by:
als315 earned 450 total points
Comment Utility
Test this:
SQL = "INSERT INTO TPLPaymentDetailT ( PaymentID, PaymentType, Amount, StatementNo ) "
SQL = SQL & "SELECT " & co2id & " AS PaymentID, 'Payment Based on Audit' AS PaymentType, IIf([partiallypaid]=False, "
SQL = SQL & "[AmountPaid],[amountpaid]- DLookUp(""amount"",""tplpaymentdetailt"",""statementno="" & [statementno] & "
SQL = SQL & """ AND type = 'Pay Based on Percentage'"")) AS Amount, TPLTotalAmountPaidPerCompanyQ.promptstatement AS StatementNo "
SQL = SQL & "FROM TPLStatementT RIGHT JOIN TPLTotalAmountPaidPerCompanyQ ON TPLStatementT.StatementNo = "
SQL = SQL & "TPLTotalAmountPaidPerCompanyQ.PromptStatement "
SQL = SQL & "WHERE (((TPLTotalAmountPaidPerCompanyQ.Company)=2));"

Open in new window

Place result into Query designer in SQL mode and check
0
 
LVL 10

Author Closing Comment

by:etech0
Comment Utility
alas315: That did it! I don't totally understand the double quotes and all, but if it works...
Jim: thanks for your help as well.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Just a thought, what is the relationship between tplStatementT and tplPaymentDetailT?  is this a 1-to-1, 1-to-0 or 1 (may not be a detail record for each statement), or 1-to-many?

If it is a 1-to-1 or 1-to-0 or 1, then you could use an outer join to connect those tables, but if it is 1-to-many relationship, you need to replace the DLOOKUP in your query with a DSUM(), since the DLOOKUP will only return the 1st value from that table, not all of the values.
0
 
LVL 10

Author Comment

by:etech0
Comment Utility
1 to many. However, I'd only want the join when a certain field in tplStatementT is flagged. Is that possible? If it's not, I don't want to end up with all those extra rows from tplPaymentDetailT.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
You could do a join to a sub-query which would group by the join field and sum across the Amount field in that table.  This would be quicker than using DLOOKUP or DSUM.  But you definately should consider using the DSUM() function rather than DLOOKUP(), otherwise, you will only subtract a single value, rather than the sum of all payments.
0
 
LVL 10

Author Comment

by:etech0
Comment Utility
There will only ever be one record in the tplPaymentDetailT with a type of "Pay Based on Percentage" for any particular statement record. That's why I used Dlookup.

I could try a subquery, but for now I think I'll leave it as it is. After all, it works, and it's not too slow because the query updates an average of 1-3 records. But I'll keep it in mind.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

762 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

16 Experts available now in Live!

Get 1:1 Help Now