I am experiencing an odd behavior when I'm exporting an Excel spreadsheet into a Access database via ADODB. The Office version is 2013 32bit running on Win7 64bit. These are the steps:
I have created a new Access file called 'test.accdb' with just one table 'orders' and one field 'OrderID'. The table is empty.
I have created a new .csv file, orders.csv. Later I show the content of this file and the end result.
I have an Excel addin with the following macro:
Public Sub updateAccess()
Dim con As New ADODB.Connection
Dim connectionString As String
Dim rs As New ADODB.Recordset
Dim sql As String
Dim Filename As String
Filename = Application.ActiveWorkbook.Path & "\test.accdb"
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" & Filename
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Filename
Call con.Open(connectionString)
sql = "DELETE FROM orders"
Call con.Execute(sql)
sql = "INSERT INTO orders " & _
"SELECT * FROM [Excel 12.0 Xml;HDR=YES;DATABASE=" & _
ActiveWorkbook.FullName & "].[" & ActiveWorkbook.Sheets(1).Name & "$]"
Set rs = con.Execute(sql)
Call con.Close
Set con = Nothing
End Sub
I then open the order.csv file using Excel, run the macro and then open test.accdb using Access. Depending on the content of the csv file, different results are output:
Case A
orders.csv:
OrderID
1
A
test.accdb, in table 'orders':
OrderID
1
A
Case B
orders.csv:
OrderID
1
A
3
test.accdb, in table 'orders'
OrderID
<blank>
1
3
Case C
orders.csv:
OrderID
1
A
3
B
C
D
test.accdb, in table 'orders'
OrderID
1
3
A
B
C
D
Why is case B failing?
I can't wrap my head around it. I tried two drivers, but no luck.
ADODB with an Excel database will guess the field types from the first 16 records. In the case
OrderID
1
A
3
it will guess numeric field type since numeric is in majority.
To avoid this, you can use the IMEX parameter within the connection string, see: https://www.connectionstrings.com/excel/
So with your code:
...
sql = "INSERT INTO orders " & _
"SELECT * FROM [Excel 12.0 Xml;HDR=YES;IMEX=1;DATABASE=" & _
ActiveWorkbook.FullName & "].[" & ActiveWorkbook.Sheets(1).Name & "$]"
Set rs = con.Execute(sql)
...
Related
I am writing a small application in Excel 2002 and I need to store numbers in some format, it can be a string.
The tables I have a 1:1 relationship and other table is just a table of one column so using access is not necesary and having to have another file is something I'd like to avoid.
So, I want to store it in separate sheets.
However, I like the benefits of SQL for querying and I need it.
I tried using some ADODB connection strings to reach this but I cannot achieve it.
I used the following code:
Dim cn As Object, rs As Object, output As String, sql As String
'---Connecting to the Data Source---
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & "Excel 8.0;HDR=Yes;IMEX=1"
.Open
End With
Also, do I have to use ODBC or should I use OLE DB? I don't know if OLE DB could be used to query in excel files.
Also, is it possible to do inserts with SQL using this ODBC or OlE DB?
I tried different providers in the connection string, and I checked the ADO references to be available.
Also, I get this error:
"Error 3706. The specified provider could not be found. It may not be installed properly."
Connection issue
First, there was an error in your Provider string, it should not contain the part with Data Source=C:\MyExcel.xls; since this is part of the connection string. So it should look like this:
.Provider = "Provider=Microsoft.Jet.OLEDB.4.0;"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & "Excel 8.0;HDR=Yes;IMEX=1"
ODBC vs OLEDB
I've never used ODBC, but based on this answer, you can't use it to query an Excel file, so OLEDB is the way to go.
Insert Statement
Once you have a working ADODB connection, insert query should work as hoped. I'm providing an example below that worked for me, but there is a few caveats:
I'm using the ACE.OLEDB.12.0 instead of JET.OLEDB.4.0 with Excel for Microsoft 365 MSO (Version 2112 Build 16.0.14706.20000) 64-bit on Windows 10.
I'd suggest to set Mode=ReadWrite in your connection string to avoid potential writting permission issues (but it might work even without it.).
Regarding the IMEX setting, I was having errors when it was set to IMEX=1, so I switched to IMEX=0 (see related question.
The example
With a workbook named Data.xls with the first sheet named Data and the following data :
Data for copy-paste
I can run the following:
Dim wb As Workbook
Set wb = Workbooks("Data.xls")
Dim ws As Worksheet
Set ws = wb.Worksheets("Data")
'Create connection
Dim conn As Object
Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0;"
.ConnectionString = "Data Source=" & wb.FullName & ";" & "Excel 8.0;HDR=Yes;IMEX=0;Mode=ReadWrite;"
.Open
End With
'Compose the INSERT statement.
Dim query As String
Const sep = ", "
query = "INSERT INTO [" & ws.Name & "$] " & _
"(Id, Name, Age) " & _
" VALUES (" & _
4 & sep & _
"'" & "Joe" & "'" & sep & _
40 & _
")"
'Execute the statement.
conn.Execute query, adCmdText
'Close the connection
conn.Close
And it should insert the data as follow:
Should you use ACE or JET?
If JET works for you, you might as well use it. Based on this article , you should also have the 32-bit version of ACE available with Windows 7 to work with Excel 2002 (32-bit), but based on your comment it seems like it's causing some problems.
See also some interesting answer about JET vs ACE.
OK. I'm sorry for wasting everyone's time. Like a DUMMY i didn't think simple solution first. The amount of data i am dealing with isn't too large and will actually work better just exporting to an excel file (i'm pretty sure). I would like to thank all that helped (June7, Parfait, and HansUp). The support you guys (everyone on this forum) give has made my job easier by far.
I'm trying to export an Excel Table from my active excel file to an Access database file.
I was getting an error at
"con.excecute sql"
"Run-time error '-2147467259 (80004005)': [Microsoft][ODBC Microsoft Access Driver] Query input must contain at least one table or query."
Sub updateAccess()
Dim con As New ADODB.Connection
Dim connectionString As String
Dim sql, newTable As String
Filename = "C:\Desktop\Quote-Size_Contacts.accdb"
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" & Filename
con.Open connectionString
' Save current table ("ContactsTbl_Data") to another table ("ContactsTbl_Data_yyyymmdd_hh_mmss")
newTable = "Quote-Size_Contacts_" & Format(Date, "yyyymmdd") & "_" & Format(Now, "hhmmss")
sql = "SELECT CODE, STORE INTO " & newTable & "FROM ContactsTbl_Data"
con.Execute sql
' Delete rows of current table ("ContactsTbl_Data")
sql = "DELETE FROM ContactsTbl_Data"
con.Execute sql
' Insert new rows into current table ("ContactsTbl_Data") from my Excel Sheet
sql = "INSERT INTO ContactsTbl_Data ([CODE], [STORE]) " & _
"SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & ThisWorkbook.FullName & "].[" & ThisWorkbook.Sheets("Sheet2").Name & "$]"
con.Execute sql
con.Close
Set con = Nothing
End Sub
EDIT::
I'm not sure standard protocol for these forums on cleaning up the code and asking more questions so i'll just put an "Edit" here.
I applied the suggestions and matched the fields it was trying to save to my access file. I now get the error: "Method 'Execute' of object '_Connection' failed"
Public Sub updateAccess()
Dim con As New ADODB.Connection
Dim connectionString As String
Dim sql, newTable As String
Filename = "C:\Desktop\Quote-Size_Contacts.accdb"
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & Filename & "'"
con.Open connectionString
' Save current table ("ContactsTbl_Data") to another table ("ContactsTbl_Data_yyyymmdd_hh_mmss")
newTable = "Quote-Size_Contacts_" & Format(Date, "yyyymmdd") & "_" & Format(Now, "hhmmss")
sql = "SELECT Company, Contact, Initials, Position, Address, AddressContd, CityStatePost, MainNo, CellNo, FaxNo, Email INTO [" & newTable & "] FROM ContactsTbl_Data"
con.Execute sql
' Delete rows of current table ("ContactsTbl_Data")
sql = "DELETE FROM ContactsTbl_Data"
con.Execute sql
' Insert new rows into current table ("ContactsTbl_Data") from my Excel Sheet
sql = "INSERT INTO ContactsTbl_Data ([Company], [Contact], [Initials], [Position], [Address], [AddressContd], [CityStatePost], [MainNo], [CellNo], [FaxNo], [Email]) " & _
"SELECT * FROM [Excel 12.0 Xml;HDR=Yes;Database=" & ThisWorkbook.FullName & "].[" & ThisWorkbook.Sheets("Sheet2").Name & "$]"
con.Execute sql
con.Close
Set con = Nothing
End Sub
See if this helps.
Table name has hyphen (-) character so use [ ] characters to delimit. Add a space in front of FROM so text doesn't run together in compiled SQL string.
sql = "SELECT CODE, STORE INTO [" & newTable & "] FROM ContactsTbl_Data"
As for connection to Access database, don't think I've ever used or seen Driver, I use Provider:
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & Filename & "'"
I have around 80 queries which I execute on a daily basis for monitoring purpose. All of them being SELECT queries, we capture the mostly the counts. This is turning out to be a boring task that's just running the query and manually capturing the output in an excel file.
For example, these are my queries with their sample respective outputs:
Query#1: SELECT count(*) from table WHERE certain_condition = 'True'
OUTPUT: 985
Query#2: SELECT count(*) from another_table WHERE yet_another_condition = 'True'
OUTPUT: 365
…
Query#80: SELECT count(*) from another_table WHERE yet_another_condition = 'True'
OUTPUT: 578
My requirement is this:
Capture the output of these 80 queries and paste them in an excel file in a certain order.
In Excel, I'll already have a heading (condition) in a cell. So I want the output of each query to be mapped to a specific cell corresponding to the heading (condition).
Is there any way of automating this boring task, or am I stuck for eternity as a bot?
PS: I am using Toad for Oracle v 12.9.0.71 database
Like Tim was saying ADO is your best bet here. Lucky for you I just had to do this myself so hopefully this should work for you.
Sub SQLQuery(sqlServer As String, strDatabase As String, strQuery As String, _
exportLocation As Variant, strUserId As String, strPassword As String)
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = Nothing
Set rs = Nothing
'create the Connection and Recordset objects
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'open the connection
conn.Open _
"Provider=SQLOLEDB;" & _
"Data Source=" & sqlServer & ";" & _
"Initial Catalog=" & strDatabase & ";" & _
"User ID=" & strUserId & ";" & _
"Password=" & strPassword & ";" & _
"Trusted_Connection=" & "True" & ";"
'execute
Set rs = conn.Execute(strQuery)
'check if data exists
If Not rs.EOF Then
'if so, copy to location
exportLocation.CopyFromRecordset rs
'close the recordset
rs.Close
End If
'clean up
conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
An example of using this subroutine:
Call SQLQuery( _
oSERVER, _
oDB, _
"SELECT count(*) from table WHERE certain_condition = 'True'", _
ThisWorkbook.Sheets("Sheet1").Cells(1, 1), _
oUSER, _
oPW)
Just for reference you will likely have to enable Microsoft ActiveX Data Objects 2.8 Library in your References for this to work.
Just to give you background of my work, i have to fetch data from MS Sql on daily basis and for that every time have to go to other server to run the query. Once the query is executed, have to paste into my common drive, which takes a lot time. ~55 mins to paste 5,00,000 row & 30 fields to common or to move file. In total 2 hours for execution & movement from one location to other.
To reduce this i would need your help to use the SQL queries through excel with the below things:
If possible,
Point1: Query will be stored in the text file in the common location
Point2: Query Parameter to be populate to get
Or
Point2:Range to be defined for parameter
If not possible above,
Query will be pasted into the code and parameter to be populated based on the above mentioned suggestion.
Connection type is windows authentication, it will work based on logged in users windows name.
This code will allow you to provide variables that you use within your SQL statement and put those into cells on a spreadsheet (In this case Cred2) and return the results on a separate sheet (Sheet2).
The first portion of the code establishes a connection with the SQL server.
The column Headers will be started in Row 2 and then the data will begin populating starting on row 3. I have used this to pull well over 100,000 records at a time and this works very quickly.
Private Sub CommandButton1_Click()
Dim cn As Object
Dim rs As Object
Dim strCon As String
Dim strSQL As String
strCon = "DRIVER=SQL Server;SERVER=ServerName;DATABASE=DBName;Trusted_Connection=True"
Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
' if not a trusted connection you could replace top line of strCon with
strCon = "DRIVER=SQL Server; Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword"
' set up where you are getting your variables to include in the SQL statement
stat = Sheets("Cred2").Range("c7").Value
barg = Sheets("Cred2").Range("c10").Value
worksite = Sheets("Cred2").Range("c11").Value
' Construct SQL statement
strSQL = "select * " _
& " FROM tableName A , table2 B " _
& "WHERE A.[field1] = B.[field1] " _
& " and field1 like '" & stat & "'" _
& "and field2 like '" & barg & "'" _
& "and field3 like '" & worksite & "'" _
& " order by Field? "
' Build Record Set
Set rs = CreateObject("ADODB.RECORDSET")
rs.ActiveConnection = cn
rs.Open strSQL
' Display Data
For intColIndex = 0 To rs.Fields.Count - 1
Sheet2.Range("A2").Offset(0, intColIndex).Value = rs.Fields(intColIndex).name
Next
Sheet2.Range("A3").CopyFromRecordset rs
' Close Database
rs.Close
cn.Close
Set cn = Nothing
end sub
I'm importing an excel file into access using vba (dao) in the following manner:
Set db = CurrentDb
query = "SELECT DISTINCT * INTO MyTable" _
& " FROM [Excel 12.0 Xml;HDR=Yes;Database=" & filePath & "].[Sheet1$];"
db.Execute (query)
[Sheet1$] is the keyword here. My excel table header starts with line 3. I want to do something like [Sheet1$A3:Lastline].
Is there a simple way to obtain the lastline? Or do I really need to create a VBA Excel Object, open the file and count?
Alternatively, can I change the header start? For instance, by using a custom import scheme instead?
Thanks in advance.
Consider a count query using a recordset and concatenate result in make-table query:
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim query As String
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT Count(*) AS RowCount" _
& " FROM [Excel 12.0 Xml;HDR=No;Database=" & filePath & "].[Sheet1$]")
query = "SELECT DISTINCT * INTO MyTable" _
& " FROM [Excel 12.0 Xml;HDR=Yes;" _
& " Database=" & filePath & "].[Sheet1$A3:A" & rst!RowCount & "];"
db.Execute (query)
rst.Close
Set rst= Nothing
Set db = Nothing