MS Access importing UTF-8 file - ms-access

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.

Related

importing multiple *.csv files into access - every file into separate table

I have over a thousand files to import into a ACCESS database.
Every file needs to be imported into separate ACCESS table.
It needs to support importing this files every day, because, there are polish stocks prices, so every day, around 08 p.m. I'm downloading a *.zip file containing 1000 *.csv files, and I'll need to import them another time, to get today's prices.
I need to have some settings changed, in order to have my data imported correctly.
next thing is:
advanced settings:
how it looks like.
I don't know how to write these advanced changes in the VBA code.
In EXCEL I could record the macro, and then see the syntax, with the settings I have chosen, but is it possible to do the same in ACCESS ?
There are two codes I've found on the internet.
first:
Function Impo_allExcel()
Dim my_file As String
Dim my_path As String
my_path = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_csv"
ChDir (my_path) 'why my_path is inside the brackets??
my_file = Dir()
Do While my_file <> "" 'is not empty (<> means not equal to), Excel VBA enters the value into
'this line above doesn't work, when I'm trying to debug it with F8
If my_file Like "*.csv" Then
' this will import ALL the *.CSV files
'(one at a time, but automatically) in this folder.
' Make sure that's what you want.
DoCmd.TransferSpreadsheet acImport, 8, "Contacts_AVDC_NEW", my_path & my_file
' what this above line says ? please explain.
End If
my_file = Dir() ' what it means?
Loop
End Function
second is:
Function Do_Import_from_CSV()
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
' real path to the folder that contains the CSV files
strPath = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_csv"
' Replace tablename with the real name of the table into which the data are to be imported
strFile = Dir(strPath & "*.csv") 'what this line means?
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() 'what this means?
Loop
End Function
Could U please briefly explain these codes for me,
tell me what is the difference between them,
and how can I incorporate the settings I need into these codes,
or into the one that suits me better.
thanks a lot.

Export all tables to txt files with export specification

I have a Access DB containing several different tables, each with a different structure (number & names of fields, number of rows, title).
What I would like to do is to export all these tables into txt files, with a given separator ("|"), point as decimal separator, quotes for strings.
I have browsed the internet and what I got was:
use DoCmd.TransferText acExportDelim command
save a customized export specification and apply it
I get an error messagge ("object does not exist") and I think it is related to the fact that the export specification is "sheet-specific", i.e. does not apply to tables with different fields and fieldnames.
Can you help me?
thanks!!
EDIT.
I post also the original code I run. As I said before, I am new to VBA, so I just looked for a code on the web, adapted it to my needs, and run.
Public Sub ExportDatabaseObjects()
On Error GoTo Err_ExportDatabaseObjects
Dim db As Database
Dim db As DAO.Database
Dim td As TableDef
Dim sExportLocation As String
Dim a As Long
Set db = CurrentDb()
sExportLocation = "C:\" 'Do not forget the closing back slash! ie: C:\Temp\
For a = 0 To db.TableDefs.Count - 1
If Not (db.TableDefs(a).Name Like "MSys*") Then
DoCmd.TransferText acExportDelim, "Export_specs", db.TableDefs(a).Name, sExportLocation & db.TableDefs(a).Name & ".txt", True
End If
Next a
Set db = Nothing
MsgBox "All database objects have been exported as a text file to " & sExportLocation, vbInformation
Exit_ExportDatabaseObjects:
Exit Sub
Err_ExportDatabaseObjects:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_ExportDatabaseObjects
End Sub
Before running the code, I manually exported the first table saving the Export_specs to a file.
Consider a db with two tables, A and B.
When I run the code A is properly exported, then I get the following errore message "3011 - The Microsoft Access database engine could not find the object 'B#txt'. Make sure the object exists and that you spell its name and the path name correctly. If 'B#txt' is not a local object, check your network connection or contact the server administration".
So, it's kind of complex. I've created a routine that imports files using ImportExport Specs, you should be able to easily adapt to your purpose. The basic operation is to create a spec that does exactly what you want to one file. Then, export this spec using this code:
Public Function SaveSpecAsXMltoTempDirectory(sSpecName As String)
Dim oFSO As FileSystemObject
Dim oTS As TextStream
Set oFSO = New FileSystemObject
Set oTS = oFSO.CreateTextFile("C:\Temp\" & sSpecName & ".xml", True)
oTS.Write CurrentProject.ImportExportSpecifications(sSpecName).XML
oTS.Close
Set oTS = Nothing
Set oFSO = Nothing
End Function
Then open this file in Notepad and replace the file name with some placeholder (I used "FILE_PATH_AND_NAME" in this sample). Then, import back into database using this code:
Public Function SaveSpecFromXMLinTempDirectory(sSpecName As String)
Dim oFSO As FileSystemObject
Dim oTS As TextStream
Dim sSpecXML As String
Dim oSpec As ImportExportSpecification
Set oFSO = New FileSystemObject
Set oTS = oFSO.OpenTextFile("C:\Temp\" & sSpecName & ".xml", ForReading)
sSpecXML = oTS.ReadAll
For Each oSpec In CurrentProject.ImportExportSpecifications
If oSpec.Name = sSpecName Then oSpec.Delete
Next oSpec
Set oSpec = CurrentProject.ImportExportSpecifications.Add(sSpecName, sSpecXML)
Set oSpec = Nothing
oTS.Close
Set oTS = Nothing
Set oFSO = Nothing
End Function
Now you can cycle thru the files and replace the placeholder in the spec with the filename then execute it using this code:
Public Function ImportFileUsingSpecification(sSpecName As String, sFile As String) As Boolean
Dim oSpec As ImportExportSpecification
Dim sSpecXML As String
Dim bReturn As Boolean
'initialize return variable as bad until function completes
bReturn = False
'export data using saved Spec
' first make sure no temp spec left by accident
For Each oSpec In CurrentProject.ImportExportSpecifications
If oSpec.Name = "Temp" Then oSpec.Delete
Next oSpec
sSpecXML = CurrentProject.ImportExportSpecifications(sSpecName).XML
If Not Len(sSpecXML) = 0 Then
sSpecXML = Replace(sSpecXML, "FILE_PATH_AND_NAME", sFile)
'now create temp spec to use, get template text and replace file path and name
Set oSpec = CurrentProject.ImportExportSpecifications.Add("Temp", sSpecXML)
oSpec.Execute
bReturn = True
Else
MsgBox "Could not locate correct specification to import that file!", vbCritical, "NOTIFY ADMIN"
GoTo ExitImport
End If
ExitImport:
On Error Resume Next
ImportFileUsingSpecification = bReturn
Set oSpec = Nothing
Exit Function
End Function
Obviously you'll need to find the table name in the spec XML and use a placeholder on it as well. Let me know if you can't get it to work and i'll update for export.

Import Specific Files from Folder into Access Table

Background:
I receive a daily sales files that I would like to import into access automatically. They are currently saved to a specific folder with a consistent naming convention. I don't review these files everyday and would like to make the import process a push button procedure. There are other files in the folder that I don't need, so I can't just import the entire file.
File Naming Convention: DAILY.SALES.20160611
(the 20160611 is the Year - 2016, Month - June, and Day 11th)
Help needed:
I can import all the files, but I can't figure out how to specify only those files that begin with "Daily.Sales". Below is the code I have that can import everything without specifying. My assumption is that it has something to do with the path or strFile, but none of the variations that I've tried has worked.
It would be nice if the code could actually check if the file has already been uploaded before uploading it, however, if I have to delete the table after each use and re-upload everything that is still easier.
Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
Dim filename As String
Dim path As String
DoCmd.SetWarnings False
path = "C:\Desktop\Test\"
Dim objXL As Object
Dim wb As Object
Set objXL = CreateObject("Excel.Application")
strFile = Dir(path & "*.xls")
While strFile <> ""
Set wb = objXL.Workbooks.Open(path & strFile)
If wb.Sheets(1).Range("A1") <> "No Data" And wb.Sheets(1).Range("A1") <> "" Then
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
End If
strFile = Dir()
Debug.Print strFileList(intFile)
wb.Close False
Set wb = Nothing
Wend
'see if any files were found
If intFile = 0 Then
MsgBox "No files found"
Exit Sub
End If
'cycle through the list of files
For intFile = 1 To UBound(strFileList)
filename = path & strFileList(intFile)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "Stage", filename, False
Call Format_Staging_Table
Call Copy_from_Stage_to_Master
Call Clear_Staging_Table
Next intFile
DoCmd.SetWarnings True
You can ignore the Call pieces, they are to format the data once I get it in...
Thanks for any help or advice that anyone might be able to provide!
Ok, I just answered a question like this 1 minute ago. I think you should take a look at this link.
http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpAllWktsSepTbl
Just modify that to suit your needs... Basically...change the path and folder to match your file name...

Import CSV into a new table -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.

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