$ symbol in select query - sql

why are we giving $ symbol in select query?
("select * from [Sheet1$]", connection);
I tried retrive data without $ symbol, but it showed error.
so any one clarify me what is the need for $ in Sheet1$

SQL syntax for ODBC and Ole DB is slightly different
•For ODBC: SELECT "Column Name" FROM "Sheet One$". I.e. excel worksheet name followed by a "$" and wrapped in double quotes.
•For Ole DB: SELECT [Column Name] FROM [Sheet One$]. I.e. excel worksheet name followed by a "$" and wrapped in "[" "]" brackets.
Odbc connection strings:
For Excel 2.0-2003
DRIVER={Microsoft Excel Driver (*.xls)};IMEX=1;MaxScanRows=16;DBQ=C:\Invoice.xls;
For Excel 2007
DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};IMEX=1;MaxScanRows=16;DBQ=C:\Invoice.xls;
Both Advanced ETL Processor and Visual Import ETL use odbc to connect to Excel
OLE DB connection strings:
For Excel 2.0-2003
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";
For Excel 2007
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
Source

Related

How to resolve a connection problem when connecting SQL from VBA XLS " ; VBA error message= 'Not able to find ISAM file'

when setting and running ADODB connection to SQL from VBA xls, I got a error message "Not able to find the ISAM file", I writting the following connection in a VBA Module :
"Provider= Microsoft.ACE.OLEDB.12.0
"Data Source=" & PWRQRYFilePath
Im usiging xls 2019 , 32 bits. Referencing to Microsoft ActiveX Data Objects 6.1 Library.,
check any thing but not able to create and open the connection would you please help me.
Im trying to create and open the connection so I can run SQL queries from VBA xls
Following is a simple query i want to run to obtain a join inner from two spread sheets
But Actually the Query is sending me the error message.
What is the content of the variable PWRQRYFilePath?
For the sake of completeness:
If you want to go the long way, or better spoken, if you need the query more times, as you know, you can put the code lines also in a function an pass the path to the file to it.... here its represented by the Workbook-Property ThisWorkbook.Fullname
Dim rs As Object 'or AS ADODB.Recordset for EarlyBinding/IntelliSense
With rs
.Open "SELECT * FROM [worksheetname$] WHERE [column]='value'", _
";Provider=Microsoft.Ace.OLEDB.12.0" & _
";Extended Properties""Excel 12.0 xml""" & _
";Data Source=" & ThisWorkbook.FullName
'*** do your stuff with recordsetresult
.Close
End With
If you need the query just once, you can use a anonym function call as well:
With CreateObject("ADODB.Recordset")
.OPEN ...
'*** do your stuff ...
.CLOSE
End With

Connect to SQL Server Database using MS Access VBA

I am trying to mass import a lot of tables from a SQL Server Database into MS Access. I have been able to use this code to import a single table, but I want to find a way to import many (not all) tables.
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;Driver={SQL Server};Server=[serverName];Database=[dbName]" _
& ";Trusted_Connection=Yes", acTable, "tableName", "tableName"
I'm thinking if I can connect to the database, I can iterate through all the tables that match my criteria and loop through the code above, replacing tableName with each table. Here is what I have tried so far:
Dim c As ADODB.connection
Dim r As ADODB.Recordset
Dim connStr As String
connStr = "DRIVER={SQL Server};Server=[serverName];Database=[dbName];Trusted_Connection=yes;"
c.Open connStr
This is the error I get.
"Object variable or With block variable not set"
Anyone have any suggestions on how to approach this?
Also on a side note, I have been trying to transfer tables using DSN to connect, but I am not sure how. This is what I have.
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DSN=[DSN File];LANGUAGE=us_english;" _
& "DATABASE=[dbName]", acTable, "tableName", "tableName"
[DSN File] is a DSN file that I have saved in the same folder as my MS Access file. I think I need to add more info to specify the DSN, since I'm not sure how the code will know where to look for the DSN file. This is the error I am getting. Thanks for any help in advance.
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (#0)
Edit: Everything works when I connect without using the DSN. But I'm curious, how would I do it using a DSN file? Thanks

Sql query vba ADO

The sheet name is DATA on excel, using F4 properties in VBA instead of sheet1 I have changed it to sData.
My aql query is not returning any result.I need to extract a column from the Data Sheet of the same workbook in VBA.
My code is :
Sql = "Select productNumber from [shtData$] where produvtDesc = "&pDes&" "
'pDesc is a string variable that stores description.
Is this how we put the sheet name in sql query? I tried [Data$] as well but it doesn't work.
Please help.
Since you didn't add any code concerning the i am assuming that you set up the connection correctly.
use the sheet name you see in excel, not the F4 code (name). Use it with a trailing $ and square brackets like you did
i assume pDes doesn't contain a string in quotes. So, add quotes around your where-criterium
look here on how to set up an ado connection to excel
alternatively, this example of an sql onto an Excel is working even if your actual ado connection refers to another database:
SELECT * FROM [data$A1:E1000] IN 'C:\path\MyFile.xlsx' [Excel 12.0;HDR=YES;IMEX=0]
Edit: For an xls you need different properties, as described in the link above.
i assume your file is called data.xls and that it contains a sheet that is called data
this works if stored in a string and then used as connection string:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\data.xls;
Extended Properties="Excel 8.0;HDR=YES";
and paired with this sql
Sql = "Select productNumber from [data$] where produvtDesc = '" & pDes & "'"
since you want to use a dynamic path try
myCon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filepath & "\data.xls;Extended Properties=""Excel 8.0;HDR=YES"";"
They key is to escape the double quotes " with a 2nd "
I think there is a typo on your pDesc variable. In the note you've called it 'pDesc' but in the SQL command string its called 'pDes'.
Also, Is it possible that you have whatever DBMS it is that you are using to be case sensitive when referring to table names (so should therefore be [DATA$])? I know most DBMS's default to case in-sensitive, but I believe it is possible to configure it to be case sensitive during the set up process...

Excel VBA 2007 connection to access 2010 accdb, run-time error "Unrecognized database format"

When deploying a small Excel VBA code to access a .accdb 2010 database to my colleague, who is running Excel 2007. I get the following error
run-time error '-2147467259 (80004005)':
Unrecognized database format '\dbname.accdb'
I'm pretty stuck on this bug as it works in Excel 2010, but not in excel 2007? My DB connection code is below:
' Declarations
Dim conn As Object
Dim rs As Object
Dim strConnect As String
Dim strng As String
' Open Database
Set conn = CreateObject("ADODB.Connection")
' Define the connection String
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & _
Glo_Database_Path & ";"
' Open Connection
conn.Open strConnect
Any ideas how to connect to an accdb 2010 from Excel 2007 64bit?
I believe accdb 2010 is not supported by Excel 2007 (but Excel 2010 supports it)
Save as the accdb 2010 to mdb format and try your connection.

MS Access queries with inline functions can't be exposed

I have an MS Access 2003 database that contains the following query:
SELECT Replace(Trim(TABLE_A.Field_01), "XXX", "YYY") AS FLD01 FROM TABLE_A
If I do an "Import Data" with Excel from this Access database, I can't find the name of this query that is defined in the database.
If I change the query by removing the Trim function, then I can see the query in Excel.
SELECT RTrim(LTrim(TABLE_A.Field_01)) AS FLD01 FROM TABLE_A
Has anyone had a similar experience? I think there's a limitation on what kind of function one can apply to a query in MS Access.
It looks like there is a problem with MS Jet SQL, which doesn't support the Replace() function - searching the key words "Jet Sql Replace Function" in google gives a lot of references with various issues with the same root cause, but I haven't found a decent solution yet...
Trim() function is not a part of SQL (resides in VBA.Strings library) so couldn't be called outside MS Access.
So you can use any SQL function but none of "external".
For what it's worth, the Replace() function is supported by the Access Database Engine 2010 (a.k.a. "ACE", the successor to "Jet"), available here. To verify that I created a table named [SomeTable] in an Access 2003 database file:
ID s
-- ----------------------------
1 Everybody loves tofu!
2 Nobody really liked Raymond.
...and I created a saved query named [stockReplaceQuery]:
SELECT ID, Replace([s],"tofu","bacon") AS s1
FROM SomeTable;
When I tested a Jet ODBC connection using the following VBScript
Option Explicit
Dim con, rst
Set con = CreateObject("ADODB.Connection")
con.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\Users\Public\2003test.mdb;"
Set rst = CreateObject("ADODB.Recordset")
rst.Open "SELECT s1 FROM stockReplaceQuery WHERE ID = 1", con
WScript.Echo rst(0).Value
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing
...I got
C:\__tmp>cscript /nologo repl.vbs
C:\__tmp\repl.vbs(6, 1) Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC Microsoft Access Driver] Undefined function 'Replace' in expression.
When I tested an ACE ODBC connection by changing the con.Open line to
con.Open "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Public\2003test.mdb;"
...I got
C:\__tmp>cscript /nologo repl.vbs
Everybody loves bacon!
It might be worth a try to install the ACE engine/drivers and see if that helps any.