Import CSV into a new table -MS Access - ms-access

I am trying to import CSV files located in a folder into Access as new tables and I have been trying with a code
Public Const rootdir = "C:\Users\deb670s\Desktop\importcsv"
Sub import()
Dim nr As Integer
Dim file As AcBrowseToObjectType
file = Dir$(rootdir & "*.csv")
nr = 1
Do While file <> ""
DoCmd.TransferText acImportDelim, "ImportSpec", "NewTableName-" & nr, rootdir & file, True, , msoEncodingCentralEuropean
file = Dir$
nr = nr + 1
Loop
End Sub
But I get an error saying variable not defined at msoEncodingCentralEuropean Can someone tell me where am I going wrong?

The last parameter must be a CodePage value:
Code Page Identifiers

Hummmm... I think you should try it this way.
http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpAllWktsSepTbl
Just change the XLS to CSV.

Related

Access 2016 VBA code to import files with variable names

I am trying to use Access VBA to automate the import (by the push of a button) of daily files that exist in a format like this:
Final_Data_ver20181101063035.xlsx
where the numeric piece translates as: yyyymmddhhmmss.
However, without knowing exactly what time the file was created, Access can't find it. I've been trying something like:
Dim datepiece as String
Dim file as String
datepiece = format(Date,"yyyymmdd")
file = "c:\Users\brian\Final_Data_ver" & datepiece & "*" & ".xlsx"
But I keep getting errors when I try this. It's looking for a file literally named:
Final_Data_ver20181102*.xlsx
Maybe I'm just not able to use wildcards in a statement like this. Any help would be appreciated.
Thanks!
This is an example of how you could find all files like Final_Data_ver20181102*.xlsx in Folder c:\:
Sub Test()
Const TEMPLATE As String = "c:\Final_Data_ver20181102*.xlsx"
Dim filename As String
filename = Dir(TEMPLATE)
Do
If filename = vbNullString Then Exit Do
'Instead of just printing the filename you can use it to import it like you did before.
Debug.Print filename
filename = Dir
Loop
End Sub

import multiple CSV file into Access database and save them into different tables based on the file name

I've got about 100 CSV files that I'm trying to import them into Access and then rename the tables based on the file names.
Here is the code I've found but the "tablename" should be my file name. however, I can't get it to work as I'm new to scripting.
Function Import_multi_csv()
Dim fs, fldr, fls, fl
Set fs = CreateObject("Scripting.FileSystemObject")
Set fldr = fs.getfolder("D:Files\")
Set fls = fldr.files
For Each fl In fls
If Right(fl.Name, 4) = ".csv" Then
DoCmd.TransferText acImportDelim, , "TableName", "D:Files\" & fl.Name, False
End If
Next fl
End Function
Also, I have three columns in my files and I want the third column to be imported as a double.
Any help will be appreciated.
It should be this:
DoCmd.TransferText acImportDelim, , "[" & fs.GetBaseName(fl.Name) & "]", "D:Files\" & fl.Name, False
As for your second question, you could create, save, and use an import specification.

Importing CSV into MS-Access using form button, confusing error

I'm trying to import a CSV file that is created from a web form I developed. When the form submits it creates a record in my CSV with a multitude of customer information.
As per requirements I needed to put it into a CSV, and then separately have it import into an Access database for others to use (Two steps required for server security).
The way I'm trying to do it is with a simple form with a button on it inside Access, that simply says Import, that will pull an update of the CSV whenever the user needs it.
My error is confusing me as it's stating
"Field 'F1' doesn't exist in destination table 'Applications' "
I do not have a field in my CSV labeled F1, or even any record that contains 'F1', and there is no field named F1 in my access table Applications (obviously).
Here is my VB module code from Access
Option Compare Database
Sub ImportingCSV()
Function Import()
On Error GoTo Macro1_Err
DoCmd.TransferText acImportDelim, "", "Applications", "C:\Users\ALee\Documents\formTesting22.csv", False, ""
Import:
Exit Function
Macro1_Err:
MsgBox Error$
Resume Macro1_Exit
End Function
And here is my CSV file format (spaced out for your readability)
OPUCN#WVQNAJT4PD,
2017.05.03,
test,
v,
90545452929,
4062033985,
No,
VM#TEST.VMTEST,
10003937683827,
test,
test,
689 395 3967,
2048 2983999,
No,rle#don.ca,
111 e Streeth south,
12,
Temporary,
Commercial,
100,
200,
300,
208/120V,
Three-Phase,
Underground (UG),
Ganged Position*,
23,
"dsbsdhfbslhfbshfbsdhlfbgshdfgsfslfgljshgfljshgfljshgflsj"
The error is telling me that the field for the second phone number ("4062033985" in the CSV) doesn't have a field in the table Applications, but it does! "F1" in the CSV is Customer Mobile. When I import manually through Access's import wizard this works fine.
Hopefully someone can point me in the right direction, not familiar with VB script or macros in access.
Don't import the file.
Link the csv file as a table. Then create a query to read and convert (purify) the data.
Use this query as source for further processing of the date like appending data to other tables.
a CSV file is a spreadsheet... try...
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml,[YourDestinationTable],"C:\YourFileDirectoryPath, filename, and extension",true,[Spreadsheet name if multiple sheet names]
There are all kinds of ways to do this sort of thing. This is certainly the simplest method.
Private Sub Command0_Click()
DoCmd.TransferText acImportDelim, "", "Book1", "C:\your_path_here\Book1.csv", True, ""
End Sub
Let's say you want to import several CSV files, all of the same type, into the same table. Just run the script below.
Option Compare Database
Option Explicit
Private Sub Command0_Click()
DoImport
End Sub
Function DoImport()
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in CSV worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the CSV files
strPath = "C:\your_path_here\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strTable = Left(strFile, Len(strFile) - 4)
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Function
You can do all kinds of other thins too; navigate to a file using the msoFileDialogFilePicker; you can loop through record sets and load them, one by one, into your table. As Gustav suggested, you can link to your file (staging) and write records into a table (production). You should probably try all of these methods, and play around with

need to dynamically insert attachment to an access form from a local system and save the address into a table

Hey i am new to access database.
I am creating a form in which i need to attach a excel file from the local system. I tried to use the attachment control to attach the document. But i am not able to store it into a table. I need to use that excel document for my further processing. I need to get the path from which the data is selected from my local system.
I hard coded the path and i was able to do my operation but now i need to dynamically fetch the data from the location.
thanks in advance
My code for hard coding looks like this
Private Sub Command4_Click()
Dim dbs As DAO.Database
Set dbs = CurrentDb
If (ifTableExists("featuretable") = True) Then
dbs.Execute "Delete * from featuretable"
End If
Dim filepath As String
filepath = "C:\Users\jolly#iese.fhg.de\Desktop\featurevalues.xlsx"**
DoCmd.TransferSpreadsheet acImport, , "featuretable", filepath, True
fmfeaturesubform.Form.Requery
End Sub
"Attach" and "import" are completely different things. i guess you want to import the excel sheet.
one way would be use the Application.FileDialog:
http://msdn.microsoft.com/en-us/library/office/ff196794(v=office.15).aspx
another way would be search your current folder and import matching filenames:
Dim mBaseFolder As String
Dim mFname as string
mBaseFolder = "C:\test\" ' or application.CurrentProject.Path
mFname = Dir(mBaseFolder & "*.xls")
Do While fname <> ""
DoCmd.TransferSpreadsheet acImport, , "featuretable", mFname , True
mFname = dir()
Loop

MS Access importing UTF-8 file

I import several '|' separated files to my DB with this:
Private Sub cmdImport_Click()
Dim i As Integer
Dim path1 As String
path1 = "C:\ImportFiles"
Destination = "C:\ImportFiles\processed"
Set fs = Application.FileSearch
With fs
.LookIn = Path1
.FileName = "*.*"
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
DoCmd.TransferText acImportDelim, "ImportRPS", "tbImportRPSTemp", .FoundFiles(i), False
fileName = .FoundFiles(i)
fileName = Replace(fileName, path1, "")
Set fs1 = CreateObject("Scripting.FileSystemObject")
fs1.MoveFile Path1 & fileName, Destination & Format(Now, "YYYYDDMMTHHMMSS") & fileName
Next i
Else
MsgBox "No files to update", vbInformation
Exit Sub
End If
End With
Me.Requery
End Sub
Problem is, the files are in UTF-8 while the access database I'm working with is not, it imports with '' in front of the first record and my table ends looking like this:
Type Nmbr Date
100 12312 15082013
What can I do to import the files whitout problem? Is it possible to convert the file to ANSI and them import it or are there better ways? I can't do it manually because those files are supposed to arrive by the dozens every day.
According to http://msdn.microsoft.com/en-us/library/office/ff835958.aspx the last parameter of TransferText is the code page. In windows UTF-8 is code page 65001.
So, try adding , , 65001 to your DoCmd.TransferText line. I think it should look like:
DoCmd.TransferText acImportDelim, "ImportRPS", "tbImportRPSTemp", .FoundFiles(i), False, , 650001
I don't have the ability to test this code right now, but I believe it should work.