p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access (http://p2p.wrox.com/forumdisplay.php?f=18)
-   -   Export table with Specs (http://p2p.wrox.com/showthread.php?t=36426)

echovue December 23rd, 2005 06:10 PM

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.




Bob Bedell December 24th, 2005 02:24 AM

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:


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:


Function ExportRecordset(strFileName As String) As Long

   Dim cnn As ADODB.Connection
   Dim rst As ADODB.Recordset
   Dim fld As ADODB.Field

   Dim strDelimiter As String
   Dim intFileNumber As Integer
   Dim intCount As Integer
   Dim intNumberofFields As Integer
   Dim intIndex As Integer

   Set cnn = CurrentProject.Connection

   Set rst = New ADODB.Recordset
   rst.Open "tblData", cnn, adOpenKeyset, adLockOptimistic, adCmdTable

   strDelimiter = ";"

   '-- Build header string --
   For intCount = 0 To rst.Fields.Count - 1
      strHeader = strHeader & strDelimiter & rst.Fields(intCount).Name
   Next intCount
   strHeader = Right$(strHeader, Len(strHeader) - Len(strDelimiter))

   intFileNumber = FreeFile
   Open strFileName For Output As intFileNumber

   '-- Write out header --
   Print #intFileNumber, strHeader

   '-- Write out records --
   intNumberofFields = rst.Fields.Count - 1

   Do Until rst.EOF
      For intIndex = 0 To intNumberofFields
         If (IsNull(rst.Fields(intIndex))) Then
            Print #intFileNumber, strDelimiter;
            If intIndex = intNumberofFields Then
               Print #intFileNumber, Trim$(CStr(rst.Fields(intIndex)));
               Print #intFileNumber, Trim$(CStr(rst.Fields(intIndex))); strDelimiter;
            End If
         End If
      Print #intFileNumber,

   Close #intFileNumber

End Function



Bob Bedell December 24th, 2005 03:22 AM

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 Bedell December 25th, 2005 01:16 PM


quote:You may want to scrub the output a bit also, replacing single quotes in your data steam with double quotes, that sort of thing.

Not exactly what I meant to say (was trying to write SQL and talk about table exporting at the same time).

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:

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
   End With

End Sub



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.