Link to home
Start Free TrialLog in
Avatar of brohjoe
brohjoeFlag for United States of America

asked on

Incorrect syntax near 'Microsoft'

Hi Experts,

I'm getting an error, "[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Microsoft.'

Here is the code:

    Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim stSQL As String
   



Public Sub loadData()
     'This was set up using Microsoft  ActiveX Data Components version 6.0.
     
   
     
     'Create ADODB connection object, open connection and construct the connection string object which is the DSN name.
     Set conn = New ADODB.Connection
     conn.ConnectionString = "sql_server"
       
    conn.Open
    'conn.Execute (strSQL)
   
    On Error GoTo ErrorHandler
   
    'Open Excel and run query to export data to SQL Server.
    strSQL = "SELECT * INTO SalesOrders FROM OPENDATASOURCE(Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=C:\Workbook.xlsx;" & _
    "Extended Properties=Excel 12.0; [Sales Orders])"

    conn.Execute (strSQL)
   
    'Error handling.
ErrorExit:
     'Reclaim memory from the cntection objects
     Set rst = Nothing
     Set conn = Nothing
     
   Exit Sub
   
ErrorHandler:
   MsgBox Err.Description, vbCritical
   Resume ErrorExit
   
   
   'clean up and reclaim memory resources.
    conn.Close
    If CBool(cnt.State And adStateOpen) Then
    Set rst = Nothing
    Set conn = Nothing
   
    End If
   
End Sub






Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

RTFM: http://msdn.microsoft.com/en-us/library/ms179856.aspx
strSQL = "SELECT * INTO SalesOrders FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0'," & _
    "'Data Source=C:\Workbook.xlsx;Extended Properties=Excel 12.0')...[Sales Orders]"

Open in new window

Avatar of brohjoe

ASKER

Ok, Thanks Angellll.  What I get from the MSDN website that I might not have done is this:

"OPENDATASOURCE can be used to access remote data from OLE DB data sources only when the DisallowAdhocAccess registry option is explicitly set to 0 for the specified provider, and the Ad Hoc Distributed Queries advanced configuration option is enabled. When these options are not set, the default behavior does not allow for ad hoc access."

How do I enable the DisallowAdhocAccess registry option and set it to zero?  Also, I didn't see 'Ad Hoc Distributed Queries' option in the Configuration Manager.   Where would I set this?
Avatar of brohjoe

ASKER

The instructions are for SQL Server.  I'm using SQL Server Management Studio Express 2005.
I see, actually, my first link was not correct:
http://msdn.microsoft.com/en-us/library/ms179856%28SQL.90%29.aspx

for sql 2005, the DisallowAdhocAccess does not apply, actually
Avatar of brohjoe

ASKER

I'm going to need a little hand holding here.  What do I do?
did you try my code?
* yes => errors?
 + yes => which ones?
 + no => OK
* no => well, please try
Avatar of brohjoe

ASKER

I used your code:

strSQL = "SELECT * INTO SalesOrders FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0'," & _
    "'Data Source=C:\Workbook.xlsx;Extended Properties=Excel 12.0')...[Sales Orders]"

Got the same error.
the same "incorrect syntax near Microsoft ... " ?
that's not possible.-..
Avatar of brohjoe

ASKER

This is what I'm trying to do.  I'm trying to export data from an Excel spreadsheet to SQL Server Management Studio 2005 on the same machine.  I have a DSN set up for the SQL Server instance.  Connectivity does not seem to be an issue.  The issue is this error.  You are welcome to test this.  I"m sure you have Excel and SQL Server on your machine.

Let me know.
Avatar of brohjoe

ASKER

You do realize that the only difference between your code and mine is the parentheses after 'Excel 12.0'.  I'm here to tell you it made no difference.
no, there are more differences, more single quotes

see, you had:
OPENDATASOURCE(Microsoft.AC ...

I have:
OPENDATASOURCE('Microsoft.AC ...

there are more quotes around ...
Avatar of brohjoe

ASKER

Ok, now I'm starting to see something different.   I put in you code exactly

strSQL = "SELECT * INTO SalesOrders FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0'," & _
    "'Data Source=C:\Workbook.xlsx;Extended Properties=Excel 12.0')...[Sales Orders]"

I get a new error now:

[Microsoft][ODBC SQL Server Driver][SQL Server]OLE DB provider "Microsoft.ACE.OLEDB.12.0 for linked server "(null): returned message "Unspecified error".
Avatar of brohjoe

ASKER

I think it may have something to do with the single quote mark.  When you put a  closing ' at the end of this string after [Sales Orders], you get an "Unclosed quotation mark after the character string."  And if you delete the single quote in from of "Data", you get

"Incorrect syntax near 'Data'.

I'm not quite understanding the rhyme and reason behind the single quotes.  Like, why is only a single quote is used in front of 'Data' , but no closing single quote on that line?
you have this:

strSQL = "  SELECT  ....  "
the double quotes are to delimit the string, which holds the sql

if you remove that part, you end up with this:
SELECT * INTO SalesOrders FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0','Data Source=C:\Workbook.xlsx;Extended Properties=Excel 12.0')...[Sales Orders]

and there, you have inside the opendatasource 2 arguemtns, actually:
'Microsoft.ACE.OLEDB.12.0'
'Data Source=C:\Workbook.xlsx;Extended Properties=Excel 12.0'



Avatar of brohjoe

ASKER

Ok got it.   Any ideas about the "Unspecified error."?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brohjoe

ASKER

Ok, well thanks anyway for your time.
Avatar of brohjoe

ASKER

Question not answered, but I got some understanding of some syntax issues.