I got a brilliant answer to my previous question from #Ryan Wildry but I thought I'd ask a different question regarding the same code: here goes.
Background Info
I have a shared (network/server) Excel template file which is both the input file and the data source (although on different sheets). Let's call that Input.xltm.
The code basically picks up a input in a range on Input Sheet, takes the first two letters and finds the closest code from Code Sheet, then populates a UserForm ListBox with the top five results.
The problem
The problem comes when users set off the UserForm and the error usually returns:
Run-time error '-2147467259'
The Microsoft Access database engine could not find the object 'C:\Users\user.name\Documents\Input1'. Make sure the object exists and that you spell its name and the path name correctly.......etc
I think it may have something to do with the fact Excel puts a number after the filename because it's a template file although I don't actually know!
The code
And here's the code:
Public MyConnection As New ADODB.Connection
Public MyRecordset As New ADODB.Recordset
Private Sub UserForm_Initialize()
Dim ColumnName As String: ColumnName = "[Variant code]"
Dim SearchStr As String: SearchStr = Left(Sheets("Input Sheet").Range("B4").Value2, 2)
Dim dbstring As String
dbstring = ThisWorkbook.FullName
Application.ScreenUpdating = False
If MyConnection.State <> adStateOpen Then
With MyConnection
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbstring & _
";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';"
.Open
End With
End If
If MyRecordset.State = adStateOpen Then MyRecordset.Close
MyRecordset.Open "Select top 5 " & ColumnName & " from [Code Sheet$] where " & ColumnName & _
" like '%" & SearchStr & "%'", MyConnection, adOpenForwardOnly, adLockReadOnly
Me.ListBox1.Clear
If Not MyRecordset.EOF Then MyRecordset.MoveFirst
Application.ScreenUpdating = True
Do Until MyRecordset.EOF
Me.ListBox1.AddItem MyRecordset.Fields(0).Value
MyRecordset.MoveNext
Loop
End Sub
I just need everyone who accesses the file through the server to be able to pick up the correct data source (which is only in the next sheet) and populate the ListBox.
I'd be thankful for any suggestions! Thanks
#UPDATE
I have checked, now if you open (and then save) the actual template file so there's no '1' after the file name, then the code works as expected. It's only when the template is opened normally and the number automatically appended that it stops working.
It seems that you do not make early-binding for MyConnection and MyRecordset first.
You can make a late-binding by
step 1.
Change
Public MyConnection As New ADODB.Connection
Public MyRecordset As New ADODB.Recordset
to
Public MyConnection As object
Public MyRecordset As object
.
step 2.
Add
Set MyConnection = createobject("adodb.connection")
Set MyRecordset = createobject("adodb.recordset")
before If MyConnection.State <> adStateOpen Then
Related
UPDATE 4
Posted solution.
UPDATE 3
Finally managed to find a solution to my problem, will post the answer shortly within the next few days.
UPDATE 2!
Put updates at the top for better readability.
So apparently it's not possible to use a named range from excel directly in the vba code when exporting to access.
After doing some more research I found there is a way around this: changing the dynamic range to a static range first and then using the entirety of the string as a variable.
However my current code down below says it cannot find the range even though I'm sure the syntax is correct, is this because I haven't set the reference to the excel file correctly?
Sub ExportAccess()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strQuery As String
myAddress = Replace(ThisWorkbook.Names("ExportData").RefersToRange.Address, "$", "")
myAddress = "[GWU$" & myAddress & "]"
' [Excel 8.0;HDR=NO;DATABASE=C:\Users\Public\test.xls] < unused code snippet
strQuery = "INSERT INTO Table1" & vbCrLf & _
"SELECT * FROM " & myAddress & ""
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "C:\Users\Public\Database.accdb"
.Open
End With
rs.Open strQuery, cn
cn.Close
rs.Close
Set rs = Nothing
Set cn = Nothing
End Sub
UPDATE!
After racking my brain for a few hours and checking the links I posted for some much needed reference, I came up with the following:
Sub ExportAccess()
Dim cn As ADODB.Connection
Dim strQuery As String
strQuery = "INSERT INTO Table1" & vbCrLf & _
"SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=C:\Users\Public\test.xls].ExportData"
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "C:\Users\Public\Database1.accdb"
.Open
.Execute strQuery
End With
With cn
.Close
End With
End Sub
I haven't been able to test this yet (will do this first thing tomorrow)
However I'm worried about the following code snippets:
"SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=C:\Users\Public\test.xls].ExportData"
This should technically select the 'ExportData' range from the test worksheet based on a different question here on Stackoverflow, I'm just not 100% sure if it will work
.Open
.Execute strQuery
End With
With cn
.Close
End With
Will this actually perform the INSERT INTO? and is closing the the connection really required? (I'd assume so, just would like to know for sure)
Kind regards,
I've been trying to get what I have in my head working for a while now, but to be honest I don't have the required expertise right now. I'm still fairly new to programming so setting up my own variables and arrays that I can actually use is still very hard for me.
Hopefully you can help me out.
The situation:
So in an Excel read-only file I basically have a 10 row by 14 column range where users put their own data into, I want to only export the rows that have actual data that was filled in by these users to an Access 2010 database.
What I tried before: At first I wanted as simple of a solution as possible, I tried doing this by connecting the Excel worksheet to the Access database and then by doing a SQL append query on the rows where data is NOT NULL. (checks if entries in last column are NOT NULL)
However since many people will use the file at the same time I feared that the Access database/Connection to the Excel worksheet might get confused and start doing things it wasn't supposed to. When I tried to Google find out whether this was actually a problem or not my search results came up empty, so I was unsure if I should continue in this direction and ultimately abandoned this solution.
What I'm trying to do now: Basically I figured that I could do the same thing in an Excel macro before exporting, setting up a named range (via name manager) and then cutting out the rows that have no data in them with a simple macro before appending the remaining rows in the named range to the access database.
There are a few examples of people that have tried to do the same after a few google searches (unfortunately I can't post more than 2 links OR post linkbu.ch links):
Search results
However all of these examples seem to use a static range, not a dynamic range. How do I insert a Range from the name manager in VBA code? If anyone has any ideas on a different solution that would also be appreciated.
Kind regards,
FSDT
Use the macro recorder. Simply turn it on and manually go through the steps to export your named range. Then look at the VBA code you generated. The macro recorder is the greatest tool ever bestowed upon mankind. Try it, you'll like it.
All right everyone, as stated before this is the solution I came up with after trial and error:
Sub ExportAccess()
Dim cn As ADODB.Connection
Dim strQuery As String
Dim dbFilepath As String
Dim xlFilepath As String
dbFilepath = "C:\Users\Public\Database1.accdb"
xlFilepath = Application.ActiveWorkbook.FullName
xlFilepath = "[Excel 8.0;HDR=Yes;DATABASE=" & xlFilepath & ";IMEX=1]"
myAddress = Replace(ThisWorkbook.Names("ExportData").RefersToRange.Address, "$", "")
myAddress = "[Sheet1$" & myAddress & "]"
myAddress = "" & xlFilepath & "." & myAddress & ""
STRcn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & dbFilepath & ";"
strQuery = "INSERT INTO Table1" & vbCrLf & _
"SELECT * FROM " & myAddress & ""
Set cn = CreateObject("ADODB.Connection")
cn.Open STRcn
cn.Execute strQuery
cn.Close
Set cn = Nothing
End Sub
The following code works perfectly fine on my computer. I cannot understand why it's not working on another persons computer. Can someone lead me towards ideas that may help trouble shoot this?
The error (with on error commented out) is that the array isn't being loaded with data. Get an error like "either bof or eof is true for the current record"
Also I can tell you if the on error GoTo NEXTT is left on.. it goes there and tries to erase the array and returns a wrong data type error. Makes me think the array is empty.
I checked Conn.State and I can confirm that the connection is open.
Function sbADO(ban_id, upc_id, div_id)
'Must add Activex data objects libaray 2.8
Dim sSQLSting As String
On Error GoTo nextt
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
'You can provide the full path of your external file as shown below
sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath _
& ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Conn.Open sconnect
sSQLSting = "SELECT * From [data$] where BANNER_ID IN (" & ban_id & ") AND UPC_ID IN (" & upc_id & ") AND DIVISION_ID IN (" & div_id & ") " 'Your SQL Statemnt (Table Name= Sheet Name=[DataSheet$])
mrs.Open sSQLSting, Conn
'=>Load the Data into an array
ReturnArray = mrs.GetRows
''OR''
'=>Paste the data into a sheet
'Sheets("Sheet1").Range("A2").CopyFromRecordset mrs
'Close Recordset
mrs.Close
'Close Connection
Conn.Close
'
Exit Function
nextt:
Erase ReturnArray
'mrs.Close
Conn.Close
'MsgBox (ReturnArray(6, 2)) '(feild, row)
End Function
The problem is because
DBPath = ThisWorkbook.FullName <-- the workbook is set to read only.
Removing read only status fixes the issue.
It seems like you're using ADO to query data in the same workbook? That might be for the purposes of simplifying this problem for SO, but it would be easier and faster to get the array you need by referring to the range directly. But in this answer, I'll assume that you really do want to use ADO.
You're trying to use ADO to open the workbook, but you're not specifying the ConnectionString or Open methods with any read-only arguments. Try making these adjustments:
'Add Mode=Read
sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath & _
";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";Mode=Read;"
'Add adLockReadOnly
mrs.Open sSQLSting, Conn, , adLockReadOnly
I am trying to use the ADO to read in a series of text files into a worksheet. I am running into problems when the majority of the data in a specific column are integers. It will give null values (blank cells) when it reaches a String.
According to microsoft support (Ado mixed data tyes) this is a common thing and the solution is to set the IMEX = 1. I tried this however it didn't work.
I have been searching others threads looking for the answer and came across this answer (other thread) where the author says to change TypeGuessRows to "get the Jet to detect whether a mixed types situation exists and trick the Jet into detecting a certain data type." However, this hasn't worked either.
Below is my VBA code. Any help would be appreciated
Sub query_text_file(WorkingSheet As String, Col As String, Row As Integer, fileName As String, firstOrLast As Integer)
Dim strPath As String
Dim ws As Worksheet
strToolWkbk = fileName
strPath = ThisWorkbook.Path & "\Excel_Barcode_Files"
Set ws = Worksheets(WorkingSheet)
'Need to reference the:
' Microsoft ActiveX Data Objects 2.5 Library
Dim s_rst As ADODB.Recordset
Dim s_cnn As ADODB.Connection 's for sub connection
Dim intRow As Integer
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Set s_cnn = New ADODB.Connection
s_cnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";" _
& "Extended Properties=""text;HDR=Yes;IMEX=1;TypeGuessRows=12;FMT=Delimited"";"
s_cnn.Open
Set s_rst = New ADODB.Recordset
strSQL = "SELECT * FROM " & strToolWkbk
s_rst.Open strSQL, _
s_cnn, adOpenStatic, adLockOptimistic, adCmdText
intRow = Row
s_rst.MoveFirst
Do Until s_rst.EOF
ws.Range(Col & intRow) = s_rst(0)
ws.Range(Chr(Asc(Col) + 1) & intRow) = s_rst(1)
intRow = intRow + 1
s_rst.MoveNext
Loop
s_rst.Close
s_cnn.Close
Set s_rst = Nothing
Set s_cnn = Nothing
End Sub
Here is a sample text file. The code reads in everything except the "P"
test test
P,0
1,1
5,2
6,3
Basically, don't rely on the registry entries as explained here on MSDN.
You need to create a Schema.ini file and put it in the same folder as all your text files. In the Schema.ini you specify the type for all columns you may have in your text files - it's just a much safer option to do that explicitly rather than have the driver work out the correct types for columns...
Say you have some txt files on your desktop, open Notepad and copy paste the below - make sure you adjust the [test.txt] part to match the name of your actual txt file and save it as: Schema.ini
[test.txt]
Format=CSVDelimited
Col1=Column1 Text
Col2=Column2 Text
Make sure you add another slash at the end of the parth in the strPath (also indicated in the article)
strPath = ThisWorkbook.Path & "\Excel_Barcode_Files\"
*Keep in mind that I am working in a different location to yours - I am using my Desktop for this example and my text file is named test.txt
Now, that you have a Schema.ini you can modify the connection string and take out some parameters which are not required because they exists in the Schema.ini
So bascially an SSCCE based on the above assumptions would be:
Sub Main()
Cells.ClearContents
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim thePath As String
thePath = "C:\Users\" & Environ("USERNAME") & "\Desktop\"
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & thePath & ";" _
& "Extended Properties=""text;HDR=No;"""
cn.Open
Dim sql As String
sql = "SELECT * FROM test.txt"
' populate the recordset
rs.Open sql, cn, adOpenStatic, adLockOptimistic, &H1
' copy the recordset starting at Range("A1") - assuming there are no headers - see HDR = No;
Range("A1").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Now after running this you should see all the values including the missing P:
I've successfully used ExcelADO for reading and importing Excel data into MSAccess during long time reading the entire SpreadSheet.
This time I need to import some table objects that can coexist with other table objects in the same spreadsheet.
According to the documentation http://support.microsoft.com/kb/278973, the only thing that needs to be changed is the From clause in the SQL string:
oRS.Open "Select * from Table1", oConn, adOpenStatic
However this fails and shows the error number mentioned in the title of this thread that essentially says that the object Table1 does not exists in that spreadsheet.
The complete code I'm using is this:
Private Sub Command0_Click()
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim dbs As DAO.Database
Set dbs = CurrentDb
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\MyPath\MyFile.xlsx;" & _
"Extended Properties= 'Excel 12.0;HDR=Yes';"
rst.Open "Select * From Table1", _
cnn, adOpenStatic, adLockOptimistic, adCmdText
With rst
If Not .EOF And Not .BOF Then
Do Until .EOF
Debug.Print .Fields(0), .Fields(1), .Fields(2)
.MoveNext
Loop
End If
End With
Set rst = Nothing
Set cnn = Nothing
Set dbs = Nothing
End Sub
As I said I can retrieve the whole content of the spreadsheet using:
.Open "Select * From [Sheet1$]", oConn, adOpenStatic
Also I can get the contents of a specified range:
.Open "Select * From [Sheet1$A1:B10]", oConn, adOpenStatic
I double-checked for the existence of that object using VBA, and it's there:
Private Sub Command2_Click()
Dim xlAp As Excel.Application
Dim xlWb As Excel.Workbook
Dim i As Long
Set xlAp = New Excel.Application
Set xlWb = xlAp.Workbooks.Open("C:\MyPath\MyFile.xlsx")
For i = 1 To xlWb.ActiveSheet.ListObjects.Count
Debug.Print xlWb.ActiveSheet.ListObjects(i).Name, _
xlWb.ActiveSheet.ListObjects(i).DisplayName
Next i
Set xlWb = Nothing
Set xlAp = Nothing
End Sub
Does anybody know how to solve this issue?
Thanks in advance,
Diego
Edit:
Well, the error is right in the sense that my object Table1 is not present in the schema as evaluated by using:
Set rs = cnn.OpenSchema(adSchemaTables)
With rs
If Not .EOF And Not .BOF Then
Do Until .EOF
Debug.Print rs.Fields(0), _
rs.Fields(1), _
rs.Fields(2), _
rs.Fields(3)
.MoveNext
Loop
End If
End With
Set rs = Nothing
nor in the tables catalog:
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Set cat = New ADOX.Catalog
cat.ActiveConnection = cnn
For Each tbl In cat.Tables
Debug.Print tbl.Name, tbl.Type
Next tbl
Set cat = Nothing
That's strange considering that the documentation explicitly says:
Named ranges are treated as "Tables" and worksheets are treated as "System Tables,"
So my guess is that Excel 2013 x64 stores named ranges in a different way than before and for accessing them via ExcelADO the syntax needs to be modified in the best scenario or they cannot accessed this way anymore.
As I previously said, the named ranges exist in my spreadsheet and I can loop through them using the range object via automation.
Hopefully someone has an answer to this.
All the best,
Diego
Environ: Windows 7 x64, Access 2013 x64, Excel 2013 x64.
Norie from utteraccess put me in the right path:
In Excel ListObjects aren't named ranges, and what you have is a
ListObject so it's not going to appear in the schema.
So this has nothing to do with the change in the object model; listobjects and named ranges are just different objects even if they appear almost the same for the user.
Didn't find an answer yet for dealing with ListObjects via ExcelAdo, but since that's another question I leave it here: ExcelADO: Fill recordset with ListObject
I have facing the same issue too. I have named range on another sheet that I can query using the named range.
When I open my Name Manager, there is sheets name that have single quote on the name, then query using named range on this sheet will not working.
Then I rename my sheet, then try query again and it's working.
I think my sheets name before conflict with excels default names, which is my sheets name on name manager have single quote
Backed myself in a corner....
Used a piece of code I found on the web and can't figure out how to close this connection. The returned OleDbcommand objCommand remains open after processing. I need to it close so I can delete the file after I have pulled the data from it. (Don't want them hanging around on the server.)
This has to be easier than the 100 of lines of code that I have tried to do this. This function opens the connection.
Protected Function ExcelConnection() As OleDbCommand
Dim fileName As String = Session("newUploadedFile")
' Connect to the Excel Spreadsheet
Dim xConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Server.MapPath(String.Format("~/Upload/{0}", fileName)) & ";" & _
"Extended Properties=Excel 8.0;"
' create your excel connection object using the connection string
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
' use a SQL Select command to retrieve the data from the Excel Spreadsheet
' the "table name" is the name of the worksheet within the spreadsheet
' in this case, the worksheet name is "Sheet1" and is expressed as: [Sheet1$]
Dim objCommand As New OleDbCommand("SELECT Name FROM [Sheet1$]", objXConn)
Return objCommand
End Function
I have tried...
ExcelConnection.connection.close()
along with about 40 other attempts to recreate the Command and then close it.
Could really use some help on this one.
This is probably not the best way to do this, but if you really must do it this way consider defining and opening the connection in the calling routine and passing it into this routine as a parameter. It can then be closed in the calling routing, thus...
Sub Main()
Dim xConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Server.MapPath(String.Format("~/Upload/{0}", fileName)) & ";" & _
"Extended Properties=Excel 8.0;"
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
Dim ObjCommand As New OleDbCommand = ExcelConnection(objXConn)
'Whatever other operations you want to do with your returned OleDbCommand
...
objXConn.Close()
End Sub
Function ExcelConnection(PassedConnection As OleDbConnection) As OleDbCommand
Dim fileName As String = Session("newUploadedFile")
Dim objCommand As New OleDbCommand("SELECT Name FROM [Sheet1$]", PassedConnection)
Return objCommand
End Function
I posted something similar to this here... Best fastest way to read an Excel sheet