damainkilla
New Member
- Joined
- May 4, 2017
- Messages
- 4
I have an Access database that is linked to csv text files, and I've written 5 simple queries (all involving an inner join and 2 where conditions). I've also created a macro that refreshes all these queries that right now are in a single Excel worksheet. The stand-alone macro works fine, but when I'm trying to incorporate it into a much bigger Excel add-in for my workplace it's giving me this error - "The text file specification '.... ' does not exist. You cannot import, export, or link using the specification." I've done some searches and I couldn't really understand what the underlying issue behind this error was about. So although I technically can use the stand-alone macro to do the job (at least for now), I don't think it's the best solution. Any simple explanation(s) to this error message or inputs to solving this problem would be super helpful!
I also experienced this error when I first "imported" the data from Access into Excel as a table. My workaround was saving and then closing the Excel workbook once I've successfully imported the data into Excel.
Here's the code to the stand-alone macro
I also experienced this error when I first "imported" the data from Access into Excel as a table. My workaround was saving and then closing the Excel workbook once I've successfully imported the data into Excel.
Here's the code to the stand-alone macro
Code:
Sub RefreshTable()
On Error GoTo ErrHandler
Dim ConnectionName As String
Dim Connection_Array() As String
Dim i As Integer
Dim MyConnection As WorkbookConnection
Dim DBSource As String
Dim ConnectionString As String
DBSource = "" 'full path to db source
For i = 1 To ActiveWorkbook.Connections.Count
ConnectionName = ActiveWorkbook.Connections(i).Name
Set MyConnection = ActiveWorkbook.Connections(i)
ConnectionString = MyConnection.OLEDBConnection.Connection
' Break the connection string into its constituent parts
Connection_Array = Split(ConnectionString, ";")
' Replace the data source
Connection_Array(3) = "Data Source =" & DBSource
' Put the connection string back together
ConnectionString = Join(Connection_Array, ";")
' Change connection string of table
MyConnection.OLEDBConnection.Connection = ConnectionString
'Refresh data
ActiveWorkbook.Connections(i).Refresh
Next
ErrHandler:
Dim errMsg As String
errMsg = "Error number: " & str(err.Number) & vbNewLine & _
"Source: " & err.Source & vbNewLine & _
"Description: " & err.Description
Debug.Print errMsg
End Sub