Problem: To convert MS Access tables (of the EIA Residential Energy Consumption Survey 1997 data) to CSV files for STATA import
This is also a general script for converting MS Tables to CSVs
Solution: Wrote a VBA script
- Open MS Access
- Tools | Macros | Visual Basic Editor
- Create a new module on your database by doing: Right Clickon database | Insert | Module
- Copy the following text (without the line numbers)
- Sub ExportAllTablesCsv()
- Dim dbMyDB As Database
- Set dbMyDB = OpenDatabase(“recs97_converted.mdb”)
- For Each tdfCurrent In dbMyDB.TableDefs
- fileoutname = “C:\” & tdfCurrent.Name & “.csv”
- If Left(tdfCurrent.Name, 2) <> “MS” Then
- DoCmd.TransferText acExportDelim, , tdfCurrent.Name, fileoutname, TRUE
- End If
- Next
- End Sub
- For your customization:
- change your mdb file to your mdb file
- The If statement (line 10) is there to deal with the fact that there are certain system tables that I needed to skip. None of my tables started with “MS”, so this was a simple non-general fix.
- If you look up help for TransferText it has a SpecificationName. I left that argument blank. You can change to tabs or other things using that.
- I had trouble with getting column headings, but jwhite at UtterAccess.com showed me the light. This code now gives column headings with the TRUE argument. For more details, check out http://www.utteraccess.com/forums/showflat.php?Cat=&Number=1752140
If you can think of a better solution, I’d love your thoughts!
Other possible/failed solutions:
- I tried running a query that joined all the tables with the EIA ID Num as the key for each table. Though this was easy, I got the “too many fields” error.
- I think there is a way to use SQL (or something???) to select all the tables and write them and to import those into another program. STATA, to my limited knowledge, doesn’t do SQL.
- STATA supposedly does XML. One can use TransferText to write XML too. I don’t know XML well enough to try it.
Any comment appreciated.
—-