Hi
I have an issue. I am executing an sql query that contains IIF statement that runs fine in query editor in access but when I am running it in excel VBA, I am getting the error " The select statement contains a reserved keyword error.. Please help.
It usese the source date parameter.
Here is the query in VBA that is giving error
sSQL = "SELECT CompanyProfiles.CompanyCode, CompanyProfiles.CompanyRegisteredName, CompanyProfiles.CompanyTradingName, CompanyProfiles.ReportingSortOrder, qryCompanyAccountingPeriodsCurrent.FiscalYear, qryCompanyAccountingPeriodsCurrent.PeriodName, TransactionEntryHeader.DefaultBranchNumber, SupplierDetails.SupplierCode, SupplierDetails.Name, TransactionEntryHeader.DocumentNo, TransactionEntryHeader.DocumentDate, SupplierBranchDetails.DefaultPaymentTermsCode, PaymentTerms.PaymentTermsDescription, qryRptTransactionTotalsDateBased.Outstanding, TransactionEntryHeader.PaymentDueDate, [SourceDate] AS SourceDate, IIf(PaymentTerms.PaymentTermsCode='HELD',-1,IIf([PaymentDueDate]<[SourceDate],DateDiff('d',[paymentduedate],Now()),0)) AS OverDueDays, IIf([Overduedays]>=0 And [Overduedays]<29,[outstanding],0) AS Due, IIf([OverdueDays]>29 And [Overduedays]<60,[outstanding],0) AS 30Days, IIf([OverdueDays]>59 And [Overduedays]<90,[outstanding],0) AS 60Days, IIf([OverdueDays]>89,[outstanding],0) AS 90Days," & _
"IIf(PaymentTerms.PaymentTermsCode='HELD',[outstanding],0) AS Held, 0 AS YTDPurchases, '' AS LastPaid, PaymentTerms.PaymentTermsCode, BranchProfiles.CentralReportingNumber AS EBosStoreNumber" & _
"FROM (((((PaymentTerms INNER JOIN TransactionEntryHeader ON PaymentTerms.PaymentTermsCode = TransactionEntryHeader.PaymentTermsCode) INNER JOIN CompanyProfiles ON TransactionEntryHeader.CompanyCode = CompanyProfiles.CompanyCode) INNER JOIN qryCompanyAccountingPeriodsCurrent ON CompanyProfiles.CompanyCode = qryCompanyAccountingPeriodsCurrent.CompanyCode) INNER JOIN (SupplierDetails INNER JOIN SupplierBranchDetails ON SupplierDetails.SupplierCode = SupplierBranchDetails.SupplierCode) ON (TransactionEntryHeader.SupplierCode = SupplierDetails.SupplierCode) AND (CompanyProfiles.CompanyCode = SupplierBranchDetails.MasterBranchNumber)) INNER JOIN qryRptTransactionTotalsDateBased ON TransactionEntryHeader.TransactionID = qryRptTransactionTotalsDateBased.TransactionID) INNER JOIN BranchProfiles ON TransactionEntryHeader.DefaultBranchNumber = BranchProfiles.MasterBranchNumber" & _
" WHERE (((qryRptTransactionTotalsDateBased.Outstanding)<>0));"
and here is the query runing fine in access
[TABLE="width: 601"]
<tbody>[TR]
[TD="width: 585"] PARAMETERS SourceDate DateTime;
SELECT CompanyProfiles.CompanyCode, CompanyProfiles.CompanyRegisteredName, CompanyProfiles.CompanyTradingName, CompanyProfiles.ReportingSortOrder, qryCompanyAccountingPeriodsCurrent.FiscalYear, qryCompanyAccountingPeriodsCurrent.PeriodName, TransactionEntryHeader.DefaultBranchNumber, SupplierDetails.SupplierCode, SupplierDetails.Name, TransactionEntryHeader.DocumentNo, TransactionEntryHeader.DocumentDate, SupplierBranchDetails.DefaultPaymentTermsCode, PaymentTerms.PaymentTermsDescription, qryRptTransactionTotalsDateBased.Outstanding, TransactionEntryHeader.PaymentDueDate, [SourceDate] AS SourceDate, IIf(PaymentTerms.PaymentTermsCode='HELD',-1,IIf([PaymentDueDate]<[SourceDate],DateDiff('d',[paymentduedate],Now()),0)) AS OverDueDays, IIf([Overduedays]>=0 And [Overduedays]<29,[outstanding],0) AS Due, IIf([OverdueDays]>29 And [Overduedays]<60,[outstanding],0) AS 30Days, IIf([OverdueDays]>59 And [Overduedays]<90,[outstanding],0) AS 60Days, IIf([OverdueDays]>89,[outstanding],0) AS 90Days, IIf(PaymentTerms.PaymentTermsCode='HELD',[outstanding],0) AS Held, 0 AS YTDPurchases, "" AS LastPaid, PaymentTerms.PaymentTermsCode, BranchProfiles.CentralReportingNumber AS EBosStoreNumber
FROM (((((PaymentTerms INNER JOIN TransactionEntryHeader ON PaymentTerms.PaymentTermsCode = TransactionEntryHeader.PaymentTermsCode) INNER JOIN CompanyProfiles ON TransactionEntryHeader.CompanyCode = CompanyProfiles.CompanyCode) INNER JOIN qryCompanyAccountingPeriodsCurrent ON CompanyProfiles.CompanyCode = qryCompanyAccountingPeriodsCurrent.CompanyCode) INNER JOIN (SupplierDetails INNER JOIN SupplierBranchDetails ON SupplierDetails.SupplierCode = SupplierBranchDetails.SupplierCode) ON (TransactionEntryHeader.SupplierCode = SupplierDetails.SupplierCode) AND (CompanyProfiles.CompanyCode = SupplierBranchDetails.MasterBranchNumber)) INNER JOIN qryRptTransactionTotalsDateBased ON TransactionEntryHeader.TransactionID = qryRptTransactionTotalsDateBased.TransactionID) INNER JOIN BranchProfiles ON TransactionEntryHeader.DefaultBranchNumber = BranchProfiles.MasterBranchNumber
WHERE (((qryRptTransactionTotalsDateBased.Outstanding)<>0));
[/TD]
[/TR]
</tbody>[/TABLE]
Thanks
I have an issue. I am executing an sql query that contains IIF statement that runs fine in query editor in access but when I am running it in excel VBA, I am getting the error " The select statement contains a reserved keyword error.. Please help.
It usese the source date parameter.
Here is the query in VBA that is giving error
sSQL = "SELECT CompanyProfiles.CompanyCode, CompanyProfiles.CompanyRegisteredName, CompanyProfiles.CompanyTradingName, CompanyProfiles.ReportingSortOrder, qryCompanyAccountingPeriodsCurrent.FiscalYear, qryCompanyAccountingPeriodsCurrent.PeriodName, TransactionEntryHeader.DefaultBranchNumber, SupplierDetails.SupplierCode, SupplierDetails.Name, TransactionEntryHeader.DocumentNo, TransactionEntryHeader.DocumentDate, SupplierBranchDetails.DefaultPaymentTermsCode, PaymentTerms.PaymentTermsDescription, qryRptTransactionTotalsDateBased.Outstanding, TransactionEntryHeader.PaymentDueDate, [SourceDate] AS SourceDate, IIf(PaymentTerms.PaymentTermsCode='HELD',-1,IIf([PaymentDueDate]<[SourceDate],DateDiff('d',[paymentduedate],Now()),0)) AS OverDueDays, IIf([Overduedays]>=0 And [Overduedays]<29,[outstanding],0) AS Due, IIf([OverdueDays]>29 And [Overduedays]<60,[outstanding],0) AS 30Days, IIf([OverdueDays]>59 And [Overduedays]<90,[outstanding],0) AS 60Days, IIf([OverdueDays]>89,[outstanding],0) AS 90Days," & _
"IIf(PaymentTerms.PaymentTermsCode='HELD',[outstanding],0) AS Held, 0 AS YTDPurchases, '' AS LastPaid, PaymentTerms.PaymentTermsCode, BranchProfiles.CentralReportingNumber AS EBosStoreNumber" & _
"FROM (((((PaymentTerms INNER JOIN TransactionEntryHeader ON PaymentTerms.PaymentTermsCode = TransactionEntryHeader.PaymentTermsCode) INNER JOIN CompanyProfiles ON TransactionEntryHeader.CompanyCode = CompanyProfiles.CompanyCode) INNER JOIN qryCompanyAccountingPeriodsCurrent ON CompanyProfiles.CompanyCode = qryCompanyAccountingPeriodsCurrent.CompanyCode) INNER JOIN (SupplierDetails INNER JOIN SupplierBranchDetails ON SupplierDetails.SupplierCode = SupplierBranchDetails.SupplierCode) ON (TransactionEntryHeader.SupplierCode = SupplierDetails.SupplierCode) AND (CompanyProfiles.CompanyCode = SupplierBranchDetails.MasterBranchNumber)) INNER JOIN qryRptTransactionTotalsDateBased ON TransactionEntryHeader.TransactionID = qryRptTransactionTotalsDateBased.TransactionID) INNER JOIN BranchProfiles ON TransactionEntryHeader.DefaultBranchNumber = BranchProfiles.MasterBranchNumber" & _
" WHERE (((qryRptTransactionTotalsDateBased.Outstanding)<>0));"
and here is the query runing fine in access
[TABLE="width: 601"]
<tbody>[TR]
[TD="width: 585"] PARAMETERS SourceDate DateTime;
SELECT CompanyProfiles.CompanyCode, CompanyProfiles.CompanyRegisteredName, CompanyProfiles.CompanyTradingName, CompanyProfiles.ReportingSortOrder, qryCompanyAccountingPeriodsCurrent.FiscalYear, qryCompanyAccountingPeriodsCurrent.PeriodName, TransactionEntryHeader.DefaultBranchNumber, SupplierDetails.SupplierCode, SupplierDetails.Name, TransactionEntryHeader.DocumentNo, TransactionEntryHeader.DocumentDate, SupplierBranchDetails.DefaultPaymentTermsCode, PaymentTerms.PaymentTermsDescription, qryRptTransactionTotalsDateBased.Outstanding, TransactionEntryHeader.PaymentDueDate, [SourceDate] AS SourceDate, IIf(PaymentTerms.PaymentTermsCode='HELD',-1,IIf([PaymentDueDate]<[SourceDate],DateDiff('d',[paymentduedate],Now()),0)) AS OverDueDays, IIf([Overduedays]>=0 And [Overduedays]<29,[outstanding],0) AS Due, IIf([OverdueDays]>29 And [Overduedays]<60,[outstanding],0) AS 30Days, IIf([OverdueDays]>59 And [Overduedays]<90,[outstanding],0) AS 60Days, IIf([OverdueDays]>89,[outstanding],0) AS 90Days, IIf(PaymentTerms.PaymentTermsCode='HELD',[outstanding],0) AS Held, 0 AS YTDPurchases, "" AS LastPaid, PaymentTerms.PaymentTermsCode, BranchProfiles.CentralReportingNumber AS EBosStoreNumber
FROM (((((PaymentTerms INNER JOIN TransactionEntryHeader ON PaymentTerms.PaymentTermsCode = TransactionEntryHeader.PaymentTermsCode) INNER JOIN CompanyProfiles ON TransactionEntryHeader.CompanyCode = CompanyProfiles.CompanyCode) INNER JOIN qryCompanyAccountingPeriodsCurrent ON CompanyProfiles.CompanyCode = qryCompanyAccountingPeriodsCurrent.CompanyCode) INNER JOIN (SupplierDetails INNER JOIN SupplierBranchDetails ON SupplierDetails.SupplierCode = SupplierBranchDetails.SupplierCode) ON (TransactionEntryHeader.SupplierCode = SupplierDetails.SupplierCode) AND (CompanyProfiles.CompanyCode = SupplierBranchDetails.MasterBranchNumber)) INNER JOIN qryRptTransactionTotalsDateBased ON TransactionEntryHeader.TransactionID = qryRptTransactionTotalsDateBased.TransactionID) INNER JOIN BranchProfiles ON TransactionEntryHeader.DefaultBranchNumber = BranchProfiles.MasterBranchNumber
WHERE (((qryRptTransactionTotalsDateBased.Outstanding)<>0));
[/TD]
[/TR]
</tbody>[/TABLE]
Thanks