![]() |
Export table with Specs
Hi Everyone,
I need to export a table to a file, and include the headings, and use ; as the delimiter. I can do it through the export function, using a specification, but would like to completely automate the process using VBA. Any suggestions appeciated. Thanks Mike Mike EchoVue.com |
Hi Mike,
I did this in ADO, though if you're just writing out Jet tables DAO would be the recommended choice (per MS). The conversion would be super simple. Also, I just provided the nuts and bolts using a few BASIC I/O functions. There are a few things you'd probably want to add to make the code more robust, based on what you need and what your data looks like. You'd want some error handling to see if the file you're writing to already exists, then either kill it, or open it for append. Also some error handling to see if the recordset is empty or not, etc. You may want to scrub the output a bit also, replacing single quotes in your data steam with double quotes, that sort of thing. I do have the code check for null fields, and just insert a ";" if it finds any. The only argument you need for the function is the name and path of the file you want to create, like: ExportRecordset("C:\CSV.txt") The function returns a long to indicate success or failure. I usually just return a 0 in the body of the function for success, and a 1 in an error handler for failure. Anyway, here's the function: Code:
Function ExportRecordset(strFileName As String) As Long Bob |
Hmm...I dont' seem to have declared strHeader. You might want to do that.
Also, just a word of warning. When you're playing around with this type of I/O code, always let it run to Close #intFileNumber if you can. If you don't close the file you won't be able to recreate it or delete it latter. You'd want the Close command in an error handler too, and I keep a sub routine with just the line "Close 1" handy, just in case things blow up before this line is executed. Bob |
Quote:
Quote:
What I meant to say is that, when you export a table via the user-interface commands using either a default or a user-defined export specification, notice that your exported string values are enclosed in double quotation marks. You won’t see that behavior when programmatically exporting with BASIC I/O functions. If you want your string data enclosed in some kind of text qualifier (e.g., double quotes), you will need to concatenate the text qualifiers in code yourself. A better example of export data scrubbing would be if your data has embedded carriage return or line feed characters (perhaps in a memo field), and you wanted to replace the CrLf stuff with, say, a single space (“ “). Best way to add export functionality to your apps is to create a class module that handles all your text exporting. Then you can add additional functionality as you need it, simply by adding additional properties and methods to your class, while keeping it all encapsulated in one place. You can then execute an export with code like: [code] Sub ExportTable() Dim objTextExport As clsTextExport Set objTextExport = New clsTextExport ‘Set objTextExport properties With objTextExport .AppendToFile = False .ExcludeFields = "" .ExportDatabase = CurrentDb .ExportFilename = "C:\Export.txt" .ExportSource = "tblData" .ExportType = "ASCII" .FieldDelimiter = vbTab .IncludeFieldNames = True .RecordDelimiter = vbCrLf .ReplaceWith = " " .TextQualifier = Chr(34) ‘Call objTextExport methods .Export End With End Sub HTH, Bob |
All times are GMT -4. The time now is 05:04 AM. |
Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.