R

Hi,

I am using VB.Net 2.0 and have connected to Microsoft Access but having trouble running a query as it uses * as a wildcard, but when I run it in .Net it returns nothing as needs to have %.

The solutions I can think of are:

1) If I was able to extract out the actual SQL string from the MS Access query from my code then I could do a Replace("*", "%") then would work - but don't know how to extract the SQL string.

2) If there is some way to make the query work with the * then that would also solve my problem.

The problem is that I have to run through 850 MS Databases (Student Lab Tests) so can't just change them manually.

The code below is what I have used, and works fine if you use % in the Access query, but returns nothing if you use *.

Dim con As System.Data.OleDb.OleDbConnection = Nothing

Dim cmd As System.Data.OleDb.OleDbCommand = Nothing

Dim objRS As System.Data.OleDb.OleDbDataReader = Nothing

con = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFolder & "\Access\Access.mdb;")

cmd = New System.Data.OleDb.OleDbCommand()

cmd.Connection = con

cmd.CommandType = CommandType.Text

cmd.CommandText = "SELECT * FROM qryQ01"

con.Open()

objRS = cmd.ExecuteReader

Any help would be much appreciated.

Kind Regards,

Grant.



Answer this question

R

  • Bassam Mansoob

    Sorry, confused...

    The Access online help topic "Using Wildcard Characters in String Compa
    risons" contains a nasty little sentence:

    It is important to note that the ANSI SQL wildcards (%) and (_) are only available with MicrosoftR Jet version 4.X and the Microsoft OLE DB Provider for Jet. They will be treated as literals if used through Microsoft Access or DAO.

    Hmm, trying to interpret that: queries executed in Access do NOT behave the same way as queries executed through OLE DB.

    I tried to find a workaround by executing the saved Access query from code (Command.Type = StoredProcedure) but that has the same problem, it only works if you encode the wildcard as "%". Ouch.


  • Kressilac

    Hi,

    If I put the % into the Access query then I can't run the query from Access or I will get the error you mentioned, but if I change it to % then save and close it, then run the query from my code it works

    I also just went to use another query that uses as a wildcard (1 character) in Access and the same thing - doesn't work from my code arrrgggg.

    If anyone is still checking this post, then any help would be much appreciated.

    My Queries are:

    Doesn't work from code:

    SELECT tblCustomer.*, tblCustomer.PostalCode
    FROM tblCustomer
    WHERE (((tblCustomer.PostalCode) Like "*RZ"));

    Does work from code:

    SELECT tblCustomer.*, tblCustomer.PostalCode
    FROM tblCustomer
    WHERE (((tblCustomer.PostalCode) Like "%RZ"));

    and

    SELECT tblCustomer.CustomerID, tblCustomer.CustomerName, tblCustomer.Phone
    FROM tblCustomer
    WHERE (((tblCustomer.Phone) Not Like " . . . ") AND ((tblCustomer.Country) Like "France"));

    Kind Regards,

    Grant.


  • Will Harris

    Strange. In .NET 2.0, "*" is the wildcard for select queries in Access databases as well. "%" is the wildcard for SQL server database. You can't use "%" in Access databases or you get a OleDbException ("Syntax error in query expression '%'").


  • Cynthia Joffrion

    There seems to be some confusion about the difference between a wildcard character in a string and the shortcut for requesting all fields.

    In both SQL and Access you use * to select all fields...

    Select * from MyTable

    However when you want a wildcard character in a string you use % in SQL. If you tried to use two * or two % in the following command it would never work because either the "all fields" character would be wrong or you would unintentionally be searching for a incorrect literal character in the search criteria...

    Select * from MyTable where Title Like 'VB%'

    While we're on the subject it's better to list the fields you actually want in your select clause instead of using * (even if you happen to want all the fields that are currently in the table). If you end up adding new fields to your table later you could break existing code by unintentionally selecting those new fields and even if you don't there's no sense having the overhead of selecting fields you're not using.


  • itai_sh100

    The issue is documented in the Knowledge base under the heading "Wildcards and Stored Queries". I could not google any workaround. Try posting your question in the .NET Framework Data Access and Storage Forum


  • R