Macro to copy server file into local folder, and change data source to this new file

rickywangca

New Member
Joined
Jun 18, 2019
Messages
1
Hi all, I'm trying to figure out this problem with copying a file to a new directory, and then using this new file as the data source for all my tables. The copying subroutine works, but I just can't seem to get the source to connect, either it gives an error or just doesn't run. Here's the code; any help appreciated!!:confused::confused::confused:

Code:
Sub FileCopy()

'Declare Variables
Dim FSO As Object
Dim user As String


'Create Object
user = (Environ("Username"))
Set FSO = VBA.CreateObject("Scripting.FileSystemObject")


Call FSO.CopyFile("S:\*source_directory*", "C:\Users\" & user & "\Downloads\", True)


End Sub

~~~~~~~~

Code:
Sub sourceChange()


Application.DisplayAlerts = False


Dim i As Integer
Dim user As String
Dim awc As WorkbookConnection


user = (Environ("Username"))


For i = 1 To ActiveWorkbook.Connections.Count
    Set awc = ActiveWorkbook.Connections.Item(i)
    With awc.OLEDBConnection
        .SourceDataFile = "C:\Users\" & user & "\Downloads\database.accdb"
        .SourceConnectionFile = "C:\Users\" & user & "\Downloads\database.accdb"
        .connection = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\" & user & "\Downloads\database.accdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Info Validation=False"
        .Reconnect
    End With
Next i


Application.DisplayAlerts = True


End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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