SQL syntax error in from clause...

krice1974

Active Member
Joined
Jul 3, 2008
Messages
422
Hi all. I'm trying to query my access database from outlook vba. I wrote a very basic query in access that works, and copied in the sql to access. I get a "syntax error in from clause" on the last line below. Here's what I've got:

Code:
'Get the symbol from the database.
    str = "SELECT tblSymbolsMain.Symbol FROM tblSymbolsMain WHERE (((tblSymbolsMain.Company) " & _
        "Like '*" & Company & "*'));"
    
    'Open a recordset.
    Set rsData2 = New ADODB.Recordset
    
    rsData2.Open str, conn, adOpenKeyset, adLockOptimistic, adCmdTable
Any ideas? As always thanks in advance...

Kevin
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
does Company have a single quote in it ?

Smith and Son's Groceries
would be
Like '*Smith and Son's Groceries*'

the single quote in the company name would conflict with the single quotes surrounding the name
 
Upvote 0
do

debug.print str

'Open a recordset.
Set rsData2 = New ADODB.Recordset

and set a breakpoint on the set statement and run the code.

then when the code breaks there, look at the immediate window, copy the printed sql and paste it directly into the the access query builder and try to run it
see if it gives you a better error message or something
 
Upvote 0
Hi James,

Earlier before I posted I did just about the same thing (I think, tell me if I'm off) by doing "? str" in the immediate window right after it was assigned its value, and I pasted that into a sql window in access and it ran just fine.
 
Upvote 0
Straight up access. Could it be something in the translation between outlook vba and access vba? The ado? I guess it has to be actually since the query works in access...
 
Upvote 0
Straight up access. Could it be something in the translation between outlook vba and access vba? The ado? I guess it has to be actually since the query works in access...

Might be worth a try using another syntax for the wildcard. In fact, I vaguely recall the asterisks not working for me either once.

Try:
Like '%Cypress%'

Code:
str = "SELECT tblSymbolsMain.Symbol FROM tblSymbolsMain " _
    & "WHERE tblSymbolsMain.Company " _
    & "LIKE " & Chr(39) & Chr(37) & company & Chr(37) & Chr(39) _
    & ";"
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,462
Members
448,899
Latest member
maplemeadows

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top