peskywinnets
Registered User.
- Local time
- Today, 10:42
- Joined
- Feb 4, 2014
- Messages
- 578
So, having experienced a problem, I've established that Access deletes the contents of a complex SQL query, leaving it empty :-( (it happens after ever run of the query)
So I seek a way of recreating the query via VBA, here's the query's loooooong SQL code...
Bearing in mind the length of it, what's the best route here? I found this simple bit of code for recreating a query...
(ref http://www.geeksengine.com/article/access-error-query-destination-field.html)
but dropping my SQL text into where his was, sees the VBA compiler start projectile vomiting!
Any top tips?
So I seek a way of recreating the query via VBA, here's the query's loooooong SQL code...
Code:
SELECT IIf([BillingCompanyName] Is Null Or [BillingCompanyName]="",Left([ContactName],37) & " " & [Currency],Left([BillingCompanyName],37) & " " & [Currency]) AS CustomerName, Sales.OrderDateTime, IIf([Sales]![Source]="amazon FBA" Or [Sales]![Source]="amazon",[DispatchedDate],[OrderDateTime]) AS Dispatched, Sales.Subsource, IIf([Source]="Amazon" Or [Source]="Amazon FBA" Or [Source]="mywebsite.com",[ExtRef],[LWOrderID]) AS QBMemo, Sales.InvoiceNo, Sales.PaymentMethod, IIf([source]="amazon" Or [source]="amazon fba","amazon",IIf([source] Like "EBAY","ebay",IIf([source] Like "opencart","mywebsite.com'",IIf([source] Like "DIRECT","direct","unknown")))) AS QBClass, IIf([ShippingCountry] Like "Germany","Sales Receipt DE",IIf([ShippingCountry] Like "Austria","Sales Receipt DE",IIf([ShippingCountry] Like "Switzerland","Sales Receipt DE",IIf([ShippingCountry] Like "France","Sales Receipt FR",IIf([ECorNOT] Like "EC","Sales Receipt EC","Sales Receipt nonEC"))))) AS SalesReceiptTemplate, Left([BillingAddress1],41) AS QBBillingAddress1, IIf([BillingAddress2]=[BillingAddress1] Or [BillingAddress2]=[BillingCity],Null,Left([BillingAddress2],41)) AS QBBillingAddress2, IIf([BillingAddress3]=[BillingAddress2] Or [BillingAddress3]=[BillingCity],Null,[BillingAddress3]) AS QBBillingAddress3, Left([BillingCity],31) AS QBBillingCity, IIf([BillingRegion]=[BillingCity],Null,Left([BillingRegion],21)) AS QBBillingRegion, Sales.BillingPostcode, IIf(([BillingCountry]="United Kingdom"),Null,[BillingCountry]) AS QBBillingCountry, IIf([ShippingCompanyName] Is Null Or [ShippingCompanyName]="",[ShippingName],[ShippingCompanyName]) AS QBShippingName, Sales.ShippingAddress1, IIf([ShippingAddress2]=[ShippingAddress1] Or [ShippingAddress2]=[ShippingCity],Null,[ShippingAddress2]) AS QBShippingAddress2, Sales.ShippingCity, IIf([ShippingRegion]=[ShippingCity],Null,[ShippingRegion]) AS QBShippingRegion, Sales.ShippingPostcode, IIf(([ShippingCountry]="United Kingdom"),Null,[ShippingCountry]) AS QBShippingCountry, Sales.Phone, Sales.Email, Left([ContactName],32) AS QBContactName, Sales.SKU, Sales.Qty, Sales.UnitCost, Sales.Currency, Sales.ExchRate, Sales.QBExported, Sales.BillingCompanyName, Sales.PostageCost, Sales.EbayID, Sales.ExtRef, Sales.Source, Sales.ECorNOT, Sales.Discount, Sales.ShippingCompanyName, IIf([BillingCompanyName] Is Null Or [BillingCompanyName]="",[ContactName],[BillingCompanyName]) AS QBBillingName, Sales.LWOrderID, IIf([ECZeroRated]=True,"EGZ",IIf([BillingPostcode] Like "JE*","Z",IIf([BillingPostcode] Like "GY*","Z",IIf([BillingPostcode] Like "IM*","S",IIf([ECorNOT]="nonEC","Z","S"))))) AS VATCode, Sales.InvoiceEmailed, Sales.AmazonPostageChecked, Sales.Email_Later
FROM Sales LEFT JOIN QBSalesReceipt ON CStr(Sales.InvoiceNo) = QBSalesReceipt.doc_num_h
WHERE (((IIf(Sales!Source="amazon FBA" Or Sales!Source="amazon",[DispatchedDate],[OrderDateTime]))<DMin("LatestDisbursementDate","AmazonDisbursementDates")) And ((Sales.QBExported)=False) And ((Sales.ECorNOT)="EC") And ((Sales.AmazonPostageChecked)=True) And ((Sales.Email_Later)=False) And ((QBSalesReceipt.doc_num_h) Is Null))
ORDER BY IIf([Sales]![Source]="amazon FBA" Or [Sales]![Source]="amazon",[DispatchedDate],[OrderDateTime]);
Bearing in mind the length of it, what's the best route here? I found this simple bit of code for recreating a query...
(ref http://www.geeksengine.com/article/access-error-query-destination-field.html)
Code:
Dim strQueryName as String
strQueryName = "qry_2ndQuarter"
Set objDB = CurrentDb
If objDB.QueryDefs(strQueryName).SQL = "SELECT *;" & vbCrLf Then
'' Delete the query
objDB.QueryDefs.Delete(strQueryName)
'' Re-create the query
qdef = objDB.CreateQueryDef(strQueryName, "SELECT * FROM Sales WHERE quarter=2")
End If
'' Output to Excel
DoCmd.OutputTo acOutputQuery, strQueryName, acFormatXLS, "2ndQuarter.xls", True
but dropping my SQL text into where his was, sees the VBA compiler start projectile vomiting!
Any top tips?