brohjoe
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.A CE.OLEDB.1 2.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
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.A
"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
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?
"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?
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
http://msdn.microsoft.com/en-us/library/ms179856%28SQL.90%29.aspx
for sql 2005, the DisallowAdhocAccess does not apply, actually
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
* yes => errors?
+ yes => which ones?
+ no => OK
* no => well, please try
ASKER
I used your code:
strSQL = "SELECT * INTO SalesOrders FROM OPENDATASOURCE('Microsoft. ACE.OLEDB. 12.0'," & _
"'Data Source=C:\Workbook.xlsx;Ex tended Properties=Excel 12.0')...[Sales Orders]"
Got the same error.
strSQL = "SELECT * INTO SalesOrders FROM OPENDATASOURCE('Microsoft.
"'Data Source=C:\Workbook.xlsx;Ex
Got the same error.
the same "incorrect syntax near Microsoft ... " ?
that's not possible.-..
that's not possible.-..
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.
Let me know.
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.A C ...
I have:
OPENDATASOURCE('Microsoft. AC ...
there are more quotes around ...
see, you had:
OPENDATASOURCE(Microsoft.A
I have:
OPENDATASOURCE('Microsoft.
there are more quotes around ...
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;Ex tended 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".
strSQL = "SELECT * INTO SalesOrders FROM OPENDATASOURCE('Microsoft.
"'Data Source=C:\Workbook.xlsx;Ex
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".
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?
"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','Dat a Source=C:\Workbook.xlsx;Ex tended 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;Ex tended Properties=Excel 12.0'
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.
and there, you have inside the opendatasource 2 arguemtns, actually:
'Microsoft.ACE.OLEDB.12.0'
'Data Source=C:\Workbook.xlsx;Ex
ASKER
Ok got it. Any ideas about the "Unspecified error."?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, well thanks anyway for your time.
ASKER
Question not answered, but I got some understanding of some syntax issues.
Open in new window