Excel file does not load in MS access - vba

It runs without any Error message but the files are not loaded in MS access. what could be the reason? I do have a csv file in the directory
After edit
Option Compare Database
Option Explicit
Public Function import_date_files()
Dim report_path As String, file_name As String
report_path = "C:\Users\gobro7\MS access test\weekly_load\"
file_name = Dir(report_path & "*.xlsx")
Do While file_name <> vbNullString
DoCmd.TransferText acImportDelim, , Trim(Replace(file_name, ".xlsx", "")), report_path & file_name, True
file_name = Dir
Loop
MsgBox "Data loaded", vbInformation
End Function

You need to add a backslash between the path and the file name. You can either write
file_name = Dir(report_path & "\*.csv")
Do While file_name <> vbNullString
DoCmd.TransferText acImportDelim, , Trim(Replace(file_name, ".csv", "")), report_path & "\" & file_name, True
file_name = Dir
Loop
Or you add the backslash at the end of your path definition:
' Note the trailing backslash!
report_path = "C:\Users\gobro7\OneDrive - Levi Strauss & Co\MS access test\weekly_load\"
file_name = Dir(report_path & "*.csv")
Do While file_name <> vbNullString
DoCmd.TransferText acImportDelim, , Trim(Replace(file_name, ".csv", "")), report_path & file_name, True
file_name = Dir
Loop

Related

Loading duplicate files with ImportError in Ms access

I have a VBA code which is loading the files from the folder but it ends up loading each file two times but the duplicated one has Import errors.
Here is my VBA code
Option Compare Database
Option Explicit
Public Function import_data_files()
Dim report_path As String
Dim filename As String
report_path = "C:\Users\greencolor\Company & Co\Desktop\Autoreport\So_load\"
filename = Dir(report_path & "*.csv", vbDirectory)
Do While filename <> vbNullString
DoCmd.TransferText acImportDelim, , Trim(Replace(filename, ".csv", "")), report_path & filename, True
filename = Dir
Loop
MsgBox "Data files imported! Report is updated!", vbInformation
End Function

TransferSpreadsheet acImport defaulting to documents folder - Run-time error 3011 (Access VBA)

Using DoCmd.TransferSpreadsheet acImport isn't working for me currently. I am trying to import .xlsx files and I am using the wildcard (*) to complete the file name. When I use a MsgBox, FullPath is correct, but when the TransferSpreadsheet runs, it says it can't find the file in C:\Users\Me\Documents (the default location).
Dim FPath As String
Dim FName As String
Dim FullPath As String
FPath = CurrentProject.Path & "\Data\"
FName = "DataTable"
FullPath = Dir(FPath & FName & "*.xlsx")
If FullPath <> "" Then
DoCmd.TransferSpreadsheet acImport, 10, TableName, FullPath, 1
Else: MsgBox "Error - file not found"
End If
Why is it not looking in the place where I designate? Is this error incorrect and it is indicating something else?
Dir() only returns a file name or nothing (empty string). It does not return a full path.
Dim FPath As String
Dim FileName As String
FPath = CurrentProject.Path & "\Data\"
FileName = Dir(FPath & "DataTable*.xlsx")
If FileName <> "" Then
DoCmd.TransferSpreadsheet acImport, 10, TableName, FPath & FileName, 1
Else
MsgBox "Error - file not found"
End If

Add field filepath for multiple Excel file import within Access

I have the following Module in Access:
On Error Resume Next
Dim strDir As String
Dim strFile As String
Dim I As Long
I = 0
strDir = "C:\excelTest\"
strFile = Dir(strDir & "*.xlsx")
While strFile <> ""
I = I + 1
strFile = strDir & strFile
Debug.Print "importing " & strFile
DoCmd.TransferSpreadsheet acImport, , "mainTable", strFile, False 'has columnheaders
strFile = Dir()
Wend
MsgBox "Load Finished"
importExcelSheets = I
End Function
This imports the data from the xlsx files within the directory (C:\excelTest). This all works fine, but how can I add an additional field which stores the directory and file?
ie. If I have a file test.xlsx during the import a field is created and the path C:\excelTest\test.xlsx is stored.
After records are imported, run an SQL UPDATE action with criteria that distinguishes those new records from previously existing, possibly a date value. Something like:
CurrentDb.Execute "UPDATE tablename SET fieldname = '" & strFile & "' WHERE datefield = #" & <some date input here> & "#"

Check if file exists in Sharepoint and save

I need to check if a file exists on Sharepoint. If no, I want to save it: if it already exists, I want to save a copy of it
Sub save()
Dim newFile as string, fName as string, path as string
fName = Sheets(“Universe”).Cells(2,2).Value
newFile = fName & Format$(Date, “dd-mm-yyyy”)
path = “SharePoint URL”
If Len(Dir(path & newFile & “.xlsm”) = 0 Then
ActiveWprkbook.SaveAs Filename := path & newFile & “.xlsm”,
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:= False
Else
ActiveWprkbook.SaveAs Filename := path & newFile & “Copy” & “.xlsm”,
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:= False
End If
End sub
The first time the file is successfully saved; however, it always finds the If condition to be true and proceeds to overwrite the existing file.
How do I change the code such that it recognises when a file has been created and so makes of copy of it?
Maybe try this :
An example of path : "Z:Documents\Reports\2013\" would be "\\sharepoint\Documents\Reports\2013\"
Sub save()
Dim newFile As String, fName As String, path As String
fName = Sheets(“Universe”).Cells(2, 2).Value
newFile = fName & Format$(Date, “dd - mm - yyyy”)
path = “SharePoint URL”
myWorkbookBasePath = path & newFile & “.xlsm”
If Dir(myWorkbookBasePath, vbDirectory) = "" Then
ActiveWorkbook.SaveAs Filename:=myWorkbookBasePath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Else:
ActiveWorkbook.SaveAs Filename:=myWorkbookBasePath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End If
End Sub
Note :
Make sure that your path end with a \
I Think ActiveWprkbook should be ActiveWorkbook

DIR function and Naming Tables in Access

I am using the DIR function to import a set of excel files into access. I then pass the attributes of the DIR to make the name of the table in access the same as the excel file. The only problem is that I also get xls in the name how I can I stop this?
Code below:
Sub Sample2()
Const cstrFolder As String = "F:\TCB_HR_KPI\Data View\"
Dim strFile As String
Dim i As Long
strFile = Dir(cstrFolder & "*.xls")
If Len(strFile) = 0 Then
MsgBox "No Files Found"
Else
Do While Len(strFile) > 0
Debug.Print cstrFolder & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strFile, cstrFolder & strFile, True
i = i + 1
strFile = Dir()
Loop
MsgBox i & " Files are imported"
End If
End Sub
Strip it off as necessary with a helper function...
Function StripFileExt(FileName As String) As String
Dim Pos As Long
Pos = InStrRev(FileName, ".")
If (Pos > 0) And (Pos > InStrRev(FileName, "\")) Then
StripFileExt = Left$(FileName, Pos - 1)
Else
StripFileExt = FileName
End If
End Function
Use the Split Function to split on ".", and take the first element of that array for your table name.
Split(strFile, ".")(0)
You could store that result in a intermediate variable. Or just use the expression directly in the TransferSpreadsheet statement.
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
Split(strFile, ".")(0), cstrFolder & strFile, True
Note: Based on your previous question, I assumed the workbook file names contain only a single dot: names from REPORT1.xls thru REPORT67.xls However if the file names you're dealing with this time can include more than one dot, my first suggestion is inappropriate.
In that case, you can still use an expression which includes Split(), but that expression would not be as simple.
Left(strFile, Len(strFile) - Len(Split(strFile, ".")(1)) -1)
Notice that approach would accommodate any of the other Excel file extensions in addition to .xls
Do you want this ?
Sub Sample2()
'
Const cstrFolder As String = "F:\TCB_HR_KPI\Data View\"
'
Dim i As Long, lng As Long
'
Dim strExt As String, strFile As String, strTable As String
'
strExt = ".xls"
lng = Len(strExt)
strFile = Dir(cstrFolder & "*" & strExt)
'
If Len(strFile) = 0 Then
MsgBox "No Files Found"
Else
Do While Len(strFile) > 0
'
' Debug.Print cstrFolder & strFile
'
strTable = Left(strFile, Len(strFile) - lng)
'
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, cstrFolder & strFile, True
i = i + 1
strFile = Dir()
Loop
MsgBox i & " Files are imported"
End If
'
End Sub
As this a file like Sample1.xls will be improrted as the table Sample1.