Chi Is Current
asked on
Handle Apostrophes in VBA SQL - Part 2
Friends ~ The following statement works fine, AS LONG AS there are no apostrophes in field: tblDistributors.distParent Co
Co - Results in Error #3075 (Missing operator)
Would be grateful for another take on syntax here.
Thank You, Jacob
strSQL1 = "SELECT tblDepletions_Detail.detZSKU, tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblProducts_DEPL.DprodVarietal, tblDepletions_Detail.detFormat"
strSQL1 = strSQL1 & " FROM (tblDistributors INNER JOIN tblDepletions ON tblDistributors.distDistID = tblDepletions.depDistID) INNER JOIN (tblProducts_DEPL INNER JOIN tblDepletions_Detail ON tblProducts_DEPL.DprodZSKU = tblDepletions_Detail.detZSKU) ON tblDepletions.depID = tblDepletions_Detail.detDeplID"
strSQL1 = strSQL1 & " WHERE (((tblDistributors.distParentCo) ='" & [Forms]![frmRPT_YTD_pntDepl_SF]![cmbDist] & "') And ((tblDepletions.depDate)Between #" & [Forms]![frmRPT_YTD_pntDepl_SF]![BDT1] & "# And #" & [Forms]![frmRPT_YTD_pntDepl_SF]![EDT2] & "#))"
strSQL1 = strSQL1 & " GROUP BY tblDepletions_Detail.detZSKU, tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblProducts_DEPL.DprodVarietal, tblDepletions_Detail.detFormat;"
I have tried replacing apostrophes in field: tblDistributors.distParent strSQL1 = "SELECT tblDepletions_Detail.detZSKU, tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblProducts_DEPL.DprodVarietal, tblDepletions_Detail.detFormat"
strSQL1 = strSQL1 & " FROM (tblDistributors INNER JOIN tblDepletions ON tblDistributors.distDistID = tblDepletions.depDistID) INNER JOIN (tblProducts_DEPL INNER JOIN tblDepletions_Detail ON tblProducts_DEPL.DprodZSKU = tblDepletions_Detail.detZSKU) ON tblDepletions.depID = tblDepletions_Detail.detDeplID"
strSQL1 = strSQL1 & " WHERE (((Replace[tblDistributors].[distParentCo],Chr(39),Chr(39)Chr(39)))= Replace('" & [Forms]![frmRPT_YTD_pntDepl_SF]![cmbDist] & "',Chr(39),Chr(39)Chr(39))) And ((tblDepletions.depDate)Between #" & [Forms]![frmRPT_YTD_pntDepl_SF]![BDT1] & "# And #" & [Forms]![frmRPT_YTD_pntDepl_SF]![EDT2] & "#))"
strSQL1 = strSQL1 & " GROUP BY tblDepletions_Detail.detZSKU, tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblProducts_DEPL.DprodVarietal, tblDepletions_Detail.detFormat;"
Clearly, Line #3 w/ 'Replace' function is incorrect.Would be grateful for another take on syntax here.
Thank You, Jacob
ASKER
Thank you fanpages for your comment.
strSQL1 = strSQL1 & " WHERE (((Replace[tblDistributors].[distParentCo],Chr(39),Chr(39)Chr(39)))= Replace('" & [Forms]![frmRPT_YTD_pntDepl_SF]![cmbDist] & "',Chr(39),Chr(39)&Chr(39))) And ((tblDepletions.depDate)Between #" & [Forms]![frmRPT_YTD_pntDepl_SF]![BDT1] & "# And #" & [Forms]![frmRPT_YTD_pntDepl_SF]![EDT2] & "#))"
Still results in Error #3075. Closer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Still results in Error #3075. Closer.
:) I'm not surprised... I probably would not have used the approach you are attempting to incorporate within your SQL statement.
Do you specifically wish to strip single quote/apostrophe [ASCII Code 39] from the database column values, or would you actually like to store & retrieve the original value of the column (including this character)?
ASKER
Bill ~ This IS indeed MS Access SQL and your solution DOES indeed work with tblDistributors.distParent Co values containing apostrophes!
Though I do not understand why. How are the apostrophes in tblDistributors.distParent Co not in the way?
Though I do not understand why. How are the apostrophes in tblDistributors.distParent
I think I mentioned this in the "Part 1" post, but I apologize for not making it clearer.
MS Access can use either double quotes or single quotes around string literals in a SQL query. Whichever one you use in a query, the only character that will then be a problem if embeded in a text value is the type of quote you are using. So by using double quotes the single quotes that might occur between the double quotes are just treated like any other character. Maybe the examples below will be more clear...
***** VALID *****
***** INVALID *****
»bp
MS Access can use either double quotes or single quotes around string literals in a SQL query. Whichever one you use in a query, the only character that will then be a problem if embeded in a text value is the type of quote you are using. So by using double quotes the single quotes that might occur between the double quotes are just treated like any other character. Maybe the examples below will be more clear...
***** VALID *****
WHERE Name = "O'Grady"
WHERE Name = 'O"Grady'
***** INVALID *****
WHERE Name = "O"Grady"
WHERE Name = 'O'Grady'
»bp
ASKER
Understood. Thank you, Bill. Best Regards, Jacob
I guess my query will go unanswered then.
Welcome, glad that was useful.
»bp
»bp
ASKER
fanpages: My apologies.
Co field values, without changing stored values.
Best Regards, Jacob
Do you specifically wish to strip single quote/apostrophe [ASCII Code 39] from the database column values, or would you actually like to store & retrieve the original value of the column (including this character)?I was looking for a way to write a select query (in VBA) which handles apostrophes in criteria and tblDistributors.distParent
Best Regards, Jacob
Chr(39)Chr(39)
to
Chr(39)&Chr(39)
That is:
strSQL1 = strSQL1 & " WHERE (((Replace[tblDistributors