VBA to change Workbook data connections

thommo41

Board Regular
Joined
Nov 10, 2006
Messages
142
Hi,
This is really bugging me now. I found some snippits of code on the web and this forum, which helped me get to what I have. But I can't get it working!
My issue is we have a remote office, using an access DB which links to an Excel file. We sometimes send the files to other people to make changes, but the link is then broken (it was a network location).
You can just change it manually, or accept the many prompts that come up, but it needs doing at each end, each time.
I thought I'd do a routine to help them change the connection paths, this is what I have...
Code:
Sub alan()
Dim i As Long
Dim cnt As Long
Dim modtext As String, modfile As String
Dim modrange As String, OldPath As String, NewPath As String

OldPath = "\\192.168.1.100\Multimedia\Commissioning Folder\"
NewPath = "C:\matts\"

cnt = ActiveWorkbook.Connections.Count

For i = 1 To cnt
    modfile = ActiveWorkbook.Connections.Item(i).OLEDBConnection.SourceDataFile
    'MsgBox (Left(modfile, 7))
    If Left(modfile, 5) = Left(NewPath, 5) Then
    'Do nothing
    Else
        MsgBox (modfile)
        modfile = VBA.Replace(modfile, OldPath, NewPath)
        MsgBox (modfile)
        ActiveWorkbook.Connections.Item(i).OLEDBConnection.SourceDataFile = modfile
    End If
        modtext = ActiveWorkbook.Connections.Item(i).OLEDBConnection.Connection
        modtext = VBA.Replace(modtext, OldPath, NewPath)
        ActiveWorkbook.Connections.Item(i).OLEDBConnection.Connection = modtext
 Next i

End Sub

I added the sourcedatafile part to the routine afterwards, because the connection string part worked fine. But I now get Run-time error '1004' Application-defined or object-defined error...
Can anyone help me out? The IF part was for troubleshooting the problem, it's not essential I don't think?
Thanks, Alan
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I doubt if you need the SourceDatafile property, the connection property should do. Does it not?
SOmetimes the path is in both the Connection and in the CommandText.
 
Upvote 0
Morning. The Connection property alone seems ok, until you "refresh all" to fetch the data. Then you get an excel error, saying <oldpath> is not a valid path...
 
Upvote 0
Hi, sorry to open an old thread but I was wondering if you ever did find a solution to this problem as I am facing the same source file issue, thanks


Hi,
This is really bugging me now. I found some snippits of code on the web and this forum, which helped me get to what I have. But I can't get it working!
My issue is we have a remote office, using an access DB which links to an Excel file. We sometimes send the files to other people to make changes, but the link is then broken (it was a network location).
You can just change it manually, or accept the many prompts that come up, but it needs doing at each end, each time.
I thought I'd do a routine to help them change the connection paths, this is what I have...
Code:
Sub alan()
Dim i As Long
Dim cnt As Long
Dim modtext As String, modfile As String
Dim modrange As String, OldPath As String, NewPath As String

OldPath = "\\192.168.1.100\Multimedia\Commissioning Folder\"
NewPath = "C:\matts\"

cnt = ActiveWorkbook.Connections.Count

For i = 1 To cnt
    modfile = ActiveWorkbook.Connections.Item(i).OLEDBConnection.SourceDataFile
    'MsgBox (Left(modfile, 7))
    If Left(modfile, 5) = Left(NewPath, 5) Then
    'Do nothing
    Else
        MsgBox (modfile)
        modfile = VBA.Replace(modfile, OldPath, NewPath)
        MsgBox (modfile)
        ActiveWorkbook.Connections.Item(i).OLEDBConnection.SourceDataFile = modfile
    End If
        modtext = ActiveWorkbook.Connections.Item(i).OLEDBConnection.Connection
        modtext = VBA.Replace(modtext, OldPath, NewPath)
        ActiveWorkbook.Connections.Item(i).OLEDBConnection.Connection = modtext
 Next i

End Sub

I added the sourcedatafile part to the routine afterwards, because the connection string part worked fine. But I now get Run-time error '1004' Application-defined or object-defined error...
Can anyone help me out? The IF part was for troubleshooting the problem, it's not essential I don't think?
Thanks, Alan
 
Upvote 0
As I already said: the path is sometimes in both the connection string and the CommandText string. So you need to check both strings.
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top