Can I escape both ' and " ? (1 Viewer)

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
Trying to build a select query where criteria may contain ' and " chars.
What is a good solution ? Thanks.
 

MarkK

bit cruncher
Local time
Today, 10:56
Joined
Mar 17, 2004
Messages
8,181
Use a parameterized QueryDef.
Cheers,
Mark
 

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
Thanks Mark, but it's not so easy when you don't know what a parameterized QueryDef is !
It's good to know there's a solution. Could you show an example please?
 

MarkK

bit cruncher
Local time
Today, 10:56
Joined
Mar 17, 2004
Messages
8,181
How about you describe your problem. Then I'll describe a very simple and robust solution, to that problem.
hth
Mark
 

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
OK, thanks.. I have "Select * From tbl2000 where LC = '" & dat(0) & "' Order by tbl2000.Date"

Dat(0) and Col LC may have the ' and " chars and thus give run time error 3075.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:56
Joined
May 7, 2009
Messages
19,242
Try using AnySql() search it on the forum.
 

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
I find my own question and something about Truncating tables. I searched Google too but no working solution. Maybe you have a link to AnySql() ? I don't want to spend too much time on this as lots to do (once this is sorted).
 

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
Sorry arnelgp, that's not something I can follow. Would have tried it but I could not work out what ParamArray p() as Variant should be.
I have succeeded though by If Then looking at whether my criteria contains ' or " and using the sql that works.
This query def parameter is that a parameter the user inputs manually by prompt?
 

MarkK

bit cruncher
Local time
Today, 10:56
Joined
Mar 17, 2004
Messages
8,181
Kirk,
Code:
Sub Teste012374901247()
    Const SQL As String = _
        "SELECT t.* " & _
        "FROM tbl2000 As t " & _
        "WHERE t.LC = p0 " & _
        "ORDER BY t.Date"
    
    With CurrentDb.CreateQueryDef("", SQL)
        .Parameters("p0") = dat(0)
        With .OpenRecordset
[COLOR="Green"]            'do something with the recordset[/COLOR]
            .Close
        End With
        .Close
    End With
End Sub
See what's happening there?
Mark
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:56
Joined
May 7, 2009
Messages
19,242
If paramarray is new to you google it. You cant prigram with just a basic tools in your hand.

Dim rs as dao.recordset
Set rs= fnAnySql("Select * From tbl2000 where LC = @1 Order by tbl2000.Date", dat(0))
 

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
No, Mark but it's very intriguing ! What is p0 ?
The sql prints as SELECT t.* FROM tbl2000 As t WHERE t.LC = p0 ORDER BY t.Date

Running it shows Dat(0) as not defined, so I passed in a value for Dat() and got a record count of 1 (which is right). There's no recordset name so I couldn't figure out how to see the field contents.
 

MarkK

bit cruncher
Local time
Today, 10:56
Joined
Mar 17, 2004
Messages
8,181
I got dat(0) from you in post #5. This is why I say describe your problem completely. If you only describe your problem partially, I can only give you a partial solution.
Mark
 

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
I can't think how to describe the problem any better. Dat(0) is theWhere criteria and shouldn't matter what it is, well from my point of view !
Shall we continue or bale out. I don't know if this is getting anywhere.
 

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
Mark, I'm not sure what it should do. My results in Msg 12.
 

MarkK

bit cruncher
Local time
Today, 10:56
Joined
Mar 17, 2004
Messages
8,181
I'm not sure what it should do.
I'm not sure what it should do either. This is all you've given us to go on...
"Select * From tbl2000 where LC = '" & dat(0) & "' Order by tbl2000.Date"
Presumably you want to be able to replace dat(0) with text that may contain delimiters, and I have shown how I would do that.
I don't know what else you need it to do.
Mark
 

kirkm

Registered User.
Local time
Tomorrow, 05:56
Joined
Oct 30, 2008
Messages
1,257
Mark, you guys know much more than me, and I assumed that bit of sql would be self-describing. I want a recordset with all fields from tbl2000 where (field) LC = (variable) Dat(0) and Ordered by (field) Date.
My question was about Dat(0) where it contains ' & " chars that cause errors and how to escape, or otherwise, get around that. You said to use a parametized query def, but I haven't been able to do that yet. And I've tried but not had any success with the code in an earlier message.
 

MarkK

bit cruncher
Local time
Today, 10:56
Joined
Mar 17, 2004
Messages
8,181
You could re-write the code as a function...
Code:
Function GetRecordset(LC As <whattypeisLC?>) As DAO.Recordset
    Const SQL As String = _
        "SELECT t.* " & _
        "FROM tbl2000 As t " & _
        "WHERE t.LC = p0 " & _
        "ORDER BY t.Date"
    
    With CurrentDb.CreateQueryDef("", SQL)
        .Parameters("p0") = LC
        Set GetRecordset = .OpenRecordset
        .Close
    End With
End Sub
So you pass in the value of LC (not sure what type it is) and then it returns the recordset. Does that make sense? Again, if you say more about what you need to do with the recordset or where the value LC comes from, I could do more about hooking this up to your actual problem, but maybe this helps,
Mark
 

Mark_

Longboard on the internet
Local time
Today, 10:56
Joined
Sep 12, 2017
Messages
2,111
@MarkK,

As I understand, Kirk is using a variable to hold the contents of a where clause. As such Dat(0) should have a value of
MyDateField <= "#4/5/2018#" AND MyOtherField = "MyValueHere"

Kirk, please let me know if this is correct.
 

Users who are viewing this thread

Top Bottom