Re: ADO Error - ODBC Microsft Access Driver Toofew parameters. Expecte
- From: "onedaywhen" <jamiecollins@xxxxxxxxxx>
- Date: 16 Feb 2007 01:29:56 -0800
On Feb 15, 11:01 am, RoyVidar <roy_vidarNOS...@xxxxxxxx> wrote:
I think the concept of "doublequoting" is more a DAO phenomena thanADO.
Try
strSQL = "SELECT * " & _
"FROM tblNames " & _
"WHERE tblNames.Name = '" & Replace(strName, "'", "''") & "'"
That handles single quotes but what about escaping double quotes plus
any other 'illegal' characters? Is it practical/safe to try to escape
all these characters yourself?
I think the better approach is to let the system (engine, parser,
provider, driver, whatever) do the work and the best way I know of
achieving this is to use Parameter objects; ADO is my preference but
perhaps DAO can do the same. This approach has the added advantage of
making one think about converting dynamic/ad hoc SQL into engine-level
PROCEDUREs ;-)
Quick example:
Sub paramtest()
Kill Environ$("temp") & "\DropMe.mdb"
Dim cat
Set cat = CreateObject("ADOX.Catalog")
Dim value As String
value = "Double "" quote Single ' quote"
With cat
.Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
Environ$("temp") & "\DropMe.mdb"
With .ActiveConnection
.Execute _
"CREATE TABLE Test (" & _
" col1 VARCHAR(30));"
' Try the 'manual escape' approach
On Error Resume Next
.Execute _
"INSERT INTO Test (col1) VALUES (" & _
Replace(value, "'", "''") & ");"
If Err.Number <> 0 Then
MsgBox _
Err.Description, , _
"Manual escape approach"
End If
On Error GoTo 0
End With
'Try the 'parameters' approach
Dim cmd
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = .ActiveConnection
With cmd
.CommandText = _
"INSERT INTO Test (col1) VALUES (?);"
.Parameters _
.Append .CreateParameter(, 200, 1, 30, value)
Dim rowsAffected As Long
.Execute rowsAffected
MsgBox _
"Rows affected: " & CStr(rowsAffected), _
, "Parameters approach"
End With
Set .ActiveConnection = Nothing
End With
End Sub
Jamie.
--
.
- Follow-Ups:
- Prev by Date: Re: Querying in VB Code - UTC time
- Next by Date: RE: Need to convert integer MM into decimal inches
- Previous by thread: Re: Access -> SQL
- Next by thread: Re: ADO Error - ODBC Microsft Access Driver Toofew parameters. Expecte
- Index(es):
Relevant Pages
|