Stavm
Stavm

Reputation: 8121

VB6 - ADO - LIKE Paramaterized Query - Access DB

Using ADO via VB6, i'm having a hard time using the LIKE command on my access file paramaterized query.

    Dim strSQL As String
    strSQL = "SELECT * FROM [MY_TABLE] WHERE [MY_TEXT_COLUMN_NAME] LIKE %?%"

Dim conn As ADODB.Connection
Set conn = New ADODB.Connection

    conn.ConnectionString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DATABASE_PATH & ";Persist Security Info=False;"

    conn.Open

Dim adoCommand As ADODB.Command
Set adoCommand = New ADODB.Command

    With adoCommand
        .ActiveConnection = conn
        .CommandType = adCmdText
        .CommandText = strSQL
        .Prepared = True
        .Parameters.Append .CreateParameter(, adVarChar, adParamInput, 255, strMYTEXT)

        Dim rs As ADODB.Recordset
        Set rs = .Execute
    End With

returns an empty record set not sure the wildcards are needed here, but i just couldn't find the right way to place them.

Upvotes: 1

Views: 352

Answers (1)

Stavm
Stavm

Reputation: 8121

Found it.

strSQL = "SELECT * FROM [MY_TABLE] WHERE [MY_TEXT_COLUMN_NAME] LIKE %?%"

should actually be

strSQL = "SELECT * FROM [MY_TABLE] WHERE [MY_TEXT_COLUMN_NAME] LIKE '%' + ? + '%'"

that solved it.

Upvotes: 1

Related Questions