When I export an Access table to a .csv file, it changes the field names replacing the # with a period. For example, the field "Name#1" becomes "Name.1". The application that will be ingesting the output file requires the # instead of the period. How can I either prevent this or rename the column header in the output?
I am using Access 2013.
Per request, here is the code:
Dim strPath As String
strPath = Application.CurrentProject.Path & "\Test"
DoCmd.TransferText acExportDelim, , "Customer_Info", strPath, True
Related
I have a Continuous form within a Navigation Form in access. I have set the form to allow "data entry" in the property sheet settings under the Data tab. With this setting the form loads to a new data entry record. The problem is with the "Allow to Data entry" set to yes, when I try export to Excel, it always exports a sheet with empty cells, it only shows the fields for the search criteria but no data.
I set the "Allow data entry to no" and when I did that the export to Excel button worked and exported data to an Excel sheet.
ExportWithFormatting
Object Type: Form
OutputFormat: Excel Workbook xlsx
OUtputFile: Blank
Auto Start: Yes
Template File:
Encoding: OUtput
Quality: Print
I would like for the export to Excel button to export data to Excel based on the search criteria I have in my form.
It not clear why you using data enter = yes, as that means the form will ONLY show new records you are entering. So, that idea is likely a bad idea.
However, assuming the form displays what you want after filtering?
Then something like this should work.
Place a button on the form (say up in the heading).
Private Sub Command47_Click()
Dim strSQL As String
strSQL = Me.RecordSource
strSQL = strSQL & " WHERE " & Me.Filter
With CurrentDb.QueryDefs("qryExport")
.SQL = strSQL
End With
' now export the query with critera to excel
Dim strOutFile As String
strOutFile = "c:\ExcelData\Test.xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
"qryExport", strOutFile, True
end Sub
The query used in above is assumed to be a query on the table, and can be quite much any legal sql query - since our code OVERWRITES the query and sql each time, and then we export that query.
I am using DoCmd.TransferText in MS Access to convert data from a query to a CSV file but there are double quotes around each field. The csv file will be used by an application and do not accept the format.
I know I can do it manually by Access Export wizard and set "Text Qualifier" to 'None' for removing double-quotes, but I need to do it in VBA. I would really appreciate if anyone can help me to do it with VBA
Here is the codes that I am using:
DoCmd.TransferText acExportDelim, , "tbl_ScrubCSV", "P:\0TPSScrub\" & Instrumentnu & ".csv", False
Expected result should be like:
E0A,M.V. KULTUS COVE,.,,,CA
but the CSV file exported like:
"E0A","M.V. KULTUS COVE",".","","","CA"
I am using the VBA DoCmd.TransferText command to import data from a CSV text file into a new table in my Access database. I have run into an issue where the text data in the first three columns in some of the files is imported as currency. I cannot figure out what is happening.
Here is a test database along with one CSV that imports correctly (VollintineLines.csv) ...
PipeID,UpstreamMH,DownstreamMH,Diameter,GISLength,Status
WS010353S,WS010353,WS010163,36,227.1984614,Fully Surveyed as Phase Work
WS011155S,WS011155,WS011154,8,418.5435318,Not Surveyed
WS011154S,WS011154,WS011153,8,303.9618911,Fully Surveyed as Phase Work
... and one that doesn't (CourtLines.csv).
PipeID,UpstreamMH,DownstreamMH,Diameter,GISLength,Status
FS020628S,FS020628,FS020462,10,278.72,Not Surveyed
FS020463S-1,FS020463,FS020462,12,248.39,Not Surveyed
FS020216S,FS020216,FS020215,12,227.53,Fully Surveyed as Phase Work
(Please ignore the unnamed objects in the database, it was just to figure out what is going on here and I didn't bother naming things.)
Here is the import code, you have to enable the Microsoft Office 16.0 Object Library Reference.
Private Sub Command0_Click()
Dim Path As FileDialog
Dim FileName As Variant
DoCmd.SetWarnings False
DoCmd.Hourglass True
Set Path = Application.FileDialog(msoFileDialogFilePicker)
With Path
.AllowMultiSelect = False
.Title = "Select your File"
.Filters.Add "All Files", "*.*"
If .Show = -1 Then
For Each FileName In .SelectedItems
DoCmd.TransferText acImportDelim, , "TempPipeData", FileName, True
Next FileName
Else
MsgBox "No File Selected to Import."
End If
End With
DoCmd.SetWarnings True
DoCmd.Hourglass False
End Sub
You have apparently encountered a rather obscure bug affecting TransferText calls that do not use an Import Specification. (It is also discussed on another site here.)
Workarounds include:
Use an Import Specification as described in this answer.
Create the table first, specifying the desired column types (Text in this case), and then import from the CSV file into the existing (empty) table.
If neither of the above options is desirable, then you could use COM Automation to
launch an instance of Excel,
have Excel open the CSV file,
save it to XLS or XLSX,
use TransferSpreadsheet in Access VBA to import the Excel data, then
delete the temporary XLS[X] file.
I have built this simple Access DB (2010) that imports 11 fields from Excel spreadsheet into a single table. What I have not done yet is to check if record in table exists and to not import it again creating duplicates.
Business Case:
User receives hundreds of spreadsheets per day (a form is used by employees in a field). User saves all these spreadsheets in a designated folder from where, VBA code picks up only some data and inserts in a single table in Access DB. When import is complete I then ask user to move all those spreadsheets into archive folder manually. I understand this process is very prone to where same spreadsheet may be imported again. I would like add a check to my VBA code to check if record exists and ignore it. It would need to check 3 fields: employee name, date, and location as there should only be one report per employee per day per location.
I am a novice and am just learning VBA so some solutions I found online are not sufficient enough for me at this point. Would like a specific example of code that I could reuse. My current code (which I also found online and modified to work for me) is something like this.
Function DoImport()
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim blnHasFieldNames As Boolean
Dim intWorksheets As Integer
Dim strWorksheets(1 To 1) As String
Dim strTables(1 To 1) As String
strWorksheets(1) = "data"
strTables(1) = "my_table"
blnHasFieldNames = True
strPath = "folder path were user originally saves all reports"
For intWorksheets = 1 To 1
strFile = Dir(strPath & "*.xlsm")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
'MsgBox strPathFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, strTables(intWorksheets), strPathFile, blnHasFieldNames, strWorksheets(intWorksheets) & "$"
strFile = Dir()
Loop
Next intWorksheets
End Function
Thank you vary much.
In your table, create a Unique Index using the three fields. This example assumes the names of the fields are EmployeeName, RecordDate, and Location:
This will not allow duplicates of the three fields to be inserted.
I'm importing a *.csv file into an Access table with the following code:
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
If .Show = -1 Then
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="New References", _
FileName:=.SelectedItems.Item(1), _
HasFieldNames:=True
TextReferenceImport = .SelectedItems.Item(1)
Else
TextReferenceImport = ""
End If
End With
Set fd = Nothing
However, I get the following error:
Microsoft Visual Basic
Field 'Name' doesn't exist in destination table 'New references.'
Obviously, the field name in both Access and the *.csv file is "Name". This is also the first column of the *.csv file. I have also set HasFieldNames to False, and changed the first columns name to F1 in access, and that seemed to work so I don't think there is anything wrong with the import.
Why does access read these strange characters that are not present in the *.csv or table, that then disrupt the import?
Those  characters are the UTF-8 BOM (byte order mark).
Unless it's practical to strip them out of your CSV before doing the import, I think you will need to create an Import Specification.
Begin an import manually from the Access user interface. After selecting your CSV file, select "First Row Contains Field Names" from the second page of the "Import Text Wizard". Then click the "Advanced" button and select "Unicode (UTF-8)" for the "Code Page" property on the import specification dialog. And then click "Save As" to give your import specification a name.
Then you can use that saved import specification when you call DoCmd.TransferText from VBA. Here is an example I tested in Access 2007 ...
DoCmd.TransferText TransferType:=acImportDelim, _
SpecificationName:="YourTable Import Specification", _
TableName:="YourTable", _
FileName:="C:\Users\hans\Documents\YourTable.csv", _
HasFieldNames:=True