I am currently using the routine proposed by Vivek to export the data of an Access 2013 table (tables linked by dsn odbc) to a csv file
Everything works fine
But how to do the reverse operation, therefore having the table empty and wanting to import (via vba code then automatically) the same data exported before, how to proceed?
But why use this severely limited file format? Export to an Access database, and you keep everything from the original table:
DbEngine(0).CreateDatabase "C:\Test\Backup.accdb", dbLangGeneral, dbVersion120
DoCmd.CopyObject "C:\Test\Backup.accdb", "Products", acTable, "Products"
If you wish to copy all tables in the database, use this simple call:
SaveAsText 6, "", "C:\Test\Tables.accdb"
Related
I'm quite new to MS Access. I just set up a new Access database to be used with our schematics tool. Our tool can access the database, so this works fine. At the end of our schematics capturing process, we generate a bill of material (BOM), listing every part that is in the design.
I want to be able to evaluate which part (each has a unique part ID "TUPID") is used on which board (unique board-ID "UBRID") - so I set up a Many-to-Many-table. First tests entering some data there by hand look good, importing text-files containing "UBRID", "TUPID" works fine too.
Now I want to be able to import the generated BOM, which is plain text containing only the "TUPID" - the "UBRID" is not known by the schematics tool. I know, I can manipulate the text file before I run a query, but I would rather not. I want to start the import operation from a form, which lists all boards (thus getting the "UBRID" by selecting the relevant board) then I want to start the import - but how is this done? Do I have to run a query for each line of the text file, or what is the best way to achieve my goal?
Looking forward for any tips!
Assuming import is for one UBRID set at a time - import records then run UPDATE action to populate those records with UBRID. Something like:
CurrentDb.Execute "UPDATE tablename SET UBRID = '" & Me.UBRID & "' WHERE UBRID IS NUll"
I need to skip the first three lines of a CSV file when loading into MS Access. The default CSV import does not drop the first three lines. I am thinking of writing a macro to ignore the first three lines.
My research has shown the DoCmd object but its methods do not cater to drop the first x lines.
Any thoughts?
The DoCmd.TransferText method takes a SpecificationName as one of its arguments. Follow the instructions at How to Create an Import Specification to create and save an import specification.
Now go into Access Options, Navigation Options, and show hidden/system objects. You should now be able to open a table named 'mSysIMEXSpecs'. Find the import spec you created earlier based on the SpecName column. Change the 'StartRow' column for that import spec to 3 (the StartRow is zero-based).
Now you should be able to use the DoCmd.TransferText method passing the name of the import spec you created and it will skip your first three lines.
In Access VBA I would use two TextStream objects, one to .ReadLine the original CSV file line-by-line and the other to .WriteLine to a temporary file, skipping the first three lines of the input file. Do some web searches on Scripting.FileSystemObject and I'm sure you'll find some sample code for this.
Then I would use DoCmd.TransferText acImportDelim to import the temporary file into Access.
The Docmd.TransferText method is a good option to go with but as an alternative to modifying your import spec, you could consider importing everything and doing some validation after the import.
So you could, import the entire file and then use a delete query to delete data from the table.
As mentioned in a comment above, modifying the spec via a system table could be tricky for someone else to find where-as a delete query with a nice little comment in your code could work a treat. If it's required use it, if not it could be commented out.
I am working on an Access 2013 database that someone else created. It has a module that exports several reports as PDF files to a specific folder. Some of the reports are exporting successfully but 3 of them aren't. An example of the code used is as follows:
DoCmd.RunSavedImportExport "Export-rptJobsToClose_FS2"
I receive an error that the database can't save the output data to the file you've selected. I realize that the path is saved in the "Export-rptJobsToClose_FS2" saved export. I would like to see the path so I have tried opening the MSysIMEXSpecs table but when I do, it is totally empty. So is the corresponding table MSysIMEXColumns. If I create a new SavedExport definition and use the same name as the one in the code, I get the message that it already exists. How is that possible that it already exists when those system tables are empty? I have tried creating saved exports with new names, but if they don't work I can't reuse those names as I get the message that they already exist. So, I have to keep thinking of new names and can't see any information about the Saved Exports that I have already created. Thanks for any help.
MSysIMEX* tables contain import specifications for correct data transfer. Saved import-exports stored in other place. You can see all names of saved imports/exports using menu External Data -> Saved Imports/Exports, there you can also see and edit destination path and import/export name.
Thru VBA you can reach the collection of saved imports/exports by using collection CurrentProject.ImportExportSpecifications, destination path stored in XML attribute of each Item.
The code below prints all existing import-export specifications
Dim ie As ImportExportSpecification
For Each ie In CurrentProject.ImportExportSpecifications
Debug.Print ie.Name
Next
Saved import/exports in Access are not the same thing as import/export specifications. If you want to see the saved import/export definition, you can dump it by typing the following command into the Immediate window.
? CodeProject.ImportExportSpecifications(*SpecificationName*).XML
I'm importing data from a CSV file into an Access table. The number is something like
-21000000 (-2.1E7). I'm using TransferText to do the import.
DoCmd.TransferText acImportDelim, , "matching report temp", Source_folder & "\" & Source
In the "matching report temp" table, the field is set up as Double. The import generates a type conversion failure. However, when I open the CSV file in Excel and copy the offending row, I can successfully use Paste Append to add it to the table manually - so the number doesn't exceed the capacity of the field.
As far as I can tell, this only happens with large negative numbers. Smaller numbers, and positive numbers of the same magnitude seem to import fine. I know I can specify an Import Specification in TransferText, but I don't see a way to set the field type to Double.
How can I get around this problem? I don't want to have to manually track down the import errors and append them by hand.
Don't let TransferText create the Access table for you. Create the Access destination table first and assign the field types you need.
When you run TransferText, the CSV data will be appended to that existing table. It should work without error as long as you choose compatible data types for the Access fields.
I examined your CSV file. The header row is troublesome because some field names are missing:
Reporting Unit,,$ Dollars,Offset Unit,,$ Dollars,Variance
That seemed to be a complication for DoCmd.TransferText. So I imported manually from the Access UI. And that gave me an import error on row 49 with the 3rd and 6th columns. In Access, the destination fields were both created as Long Integer. However the values for those fields in the CSV row are 2262169190 and -2262169190 ... both beyond the capacity of Access' Long Integer, -2,147,483,648 to 2,147,483,647.
As a cheap workaround, I selected Text for the data type of those 2 columns when I imported. That allowed the import to work without error. Once you get the data into Access successfully, you could cast those (string) values to a numeric type when you need to use them.
It also worked when I chose Double as the type for those 2 columns. It sounds like that's what you actually want.
If you want to get the import working with DoCmd.TransferText, I think you'll need to create an import specification. If possible, I would also first modify the header line of the CSV file so that all the fields have names.
I actually tested that approach without altering the CSV file. After creating an import specification in the Access UI (see screen capture below), this TransferText operation worked in Access 2007 without import errors.
DoCmd.TransferText acImportDelim, _
"IC_Y1301_Specification", _
"IC_Y1301_LD10279_F25210001", _
"C:\Users\hans\Downloads\IC_Y1301_LD10279_F25210001.CSV", _
True
I am trying to use an automated macro to export a Ms-Access table to a csv file. I want the destination file to have a unique name, and I reckoned that using now()yyyymmddhhnn would be a good way to achieve this.
I have got transfer text working ok from my macro, and I have set up an export file spec for the transfer.
I am using ="C:\batchfile_" & Format(Now(),"yyyymmddhhnn") & ".csv" in the filename argument in the macro. This bit works.
But when I try to run the macro, it tells me that the filename doesn't exist and then the export doesn't complete. I am not sure why this is, but I think it is because the export file specification is expecting the destination file to have the same filename and column structure as the source table.
Does anyone know a way around this?
Eric
This is very old thread, I am posting my solution so that it may be usefull for some one else
transfer text works fine, as long as variables are supplied properly, you can check for other options other than filename, datasource alternatively create using file open statement
by opening text file and convert recordset data into CSV format.