Hi all
I am pretty new to access. I know most of the basics like creating tables writing query's etc....
I need help with a macro that I am working on.
I have a table which I need to automatically update every month with new data.
I need to overwrite the current data with my new data every month.
The new data I am putting into this table each month is in a txt file format.
So basically I want the macro to go into a folder and find the latest txt file in that folder based on the last updated date. I then want it to load this data (and overwrite) in my table.
I have done this successfully in Excel with a macro and my code is below for this.
Any help with this would be great
Thanks
Andrew
I am pretty new to access. I know most of the basics like creating tables writing query's etc....
I need help with a macro that I am working on.
I have a table which I need to automatically update every month with new data.
I need to overwrite the current data with my new data every month.
The new data I am putting into this table each month is in a txt file format.
So basically I want the macro to go into a folder and find the latest txt file in that folder based on the last updated date. I then want it to load this data (and overwrite) in my table.
I have done this successfully in Excel with a macro and my code is below for this.
Code:
Sub Auto_Open()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim SaveColNdx As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim Sep As String
'Delete last months data
Sheets("Sheet1").Select
Cells.Select
Selection.ClearContents
Range("A1").Select
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Sep = vbTab
'Delete last months data
Sheets("Sheet1").Select
Cells.Select
Selection.ClearContents
Range("A1").Select
'Specify the path to the folder
MyPath = "S:\Claims Analytics Reporting\Project_Opal\UK Extracts\2.1.1\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first txt file from the folder
MyFile = Dir(MyPath & "*.Txt", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each txt file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Txt file from the folder
MyFile = Dir
Loop
'Open the latest file
Open MyPath & LatestFile For Input Access Read As #1
While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend
'Refresh pivots
Sheets("Pivot Check").Select
Range("C9").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
Range("I8").Select
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
End Sub
Any help with this would be great
Thanks
Andrew