Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 22nd, 2004, 03:02 PM
Authorized User
 
Join Date: Feb 2004
Location: , , Canada.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chacquard
Default export data to .dat format

Hi,

Does anyone know how to export data from access to a .dat file using vba. I'm able to export to a .csv file but can't seem to find the way to export to a .dat file.

Thank you.

Chantal

Reply With Quote
  #2 (permalink)  
Old December 28th, 2004, 03:51 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Can I ask why you want to export to .dat? Aren't those index files that the system creates?

mmcdonal
Reply With Quote
  #3 (permalink)  
Old January 5th, 2005, 02:46 PM
Authorized User
 
Join Date: Feb 2004
Location: , , Canada.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chacquard
Default

The content of the file is a simple text file.
We simply need to rename a .csv file to a .dat file


Reply With Quote
  #4 (permalink)  
Old January 8th, 2005, 10:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

To the best of my knowledge, the .dat extension is simply a generic extension used by many programs to indicate a plain ascii text file. It doesn't indicate any particular file format. You need to establish the format yourself. If you already have your data in a .csv file, simply changing the file extension to .dat should'nt alter the contents of your .csv file in any way. A little string parsing code that lops off the .csv extension and replaces it with .dat might be all you need.

Or, you could write code to export your data to a .dat file, and format the data as comma seperated values yourself. The code below uses basic I/O Open, Print and Close methods.

Public Sub ExportDatFile()

    Dim rst As ADODB.Recordset
    Dim strLine As String

    Set rst = New ADODB.Recordset

    With rst
        .ActiveConnection = CurrentProject.Connection
        .Source = "Categories"
        .Open Options:=adCmdTableDirect
    End With

    Open "C:\Data\Categories.dat" For Output As #1

    Do Until rst.EOF
        strLine = _
            rst.Fields("CategoryID").Value & ", " & _
            rst.Fields("CategoryName").Value & ", " & _
            rst.Fields("Description").Value

        Print #1, strLine
        rst.MoveNext
    Loop

    Close #1
    rst.Close

    Set rst = Nothing

End Sub

What you get is a csv formatted .dat (ascii text) file that can be viewed in any text editor, such as NotePad, and looks like:

1, Beverages, Soft drinks, coffees, teas, beers, and ales
2, Condiments, Sweet and savory sauces, relishes, spreads, and seasonings
3, Confections, Desserts, candies, and sweet breads
4, Dairy Products, Cheeses
5, Grains/Cereals, Breads, crackers, pasta, and cereal
6, Meat/Poultry, Prepared meats
7, Produce, Dried fruit and bean curd
8, Seafood, Seaweed and fish

When working with a .dat file, you need to know how it has been formatted up front in order to parse it correctly.

HTH,

Bob

Reply With Quote
  #5 (permalink)  
Old January 8th, 2005, 10:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Perhaps a better way to change to extension would simply be to make a copy of the file as follows:

Public Sub ChangeFileExtension()

    Dim SourceFile, DestinationFile

    SourceFile = "C:\Data\Categories.csv"
    DestinationFile = "C:\Data\Categories.dat"
    FileCopy SourceFile, DestinationFile

End Sub

The content of the two files will be identical when opened in a text editor.

Reply With Quote
  #6 (permalink)  
Old January 8th, 2005, 11:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

A final sub that accepts arguments to convert multiple .csv files in a directory to .dat files in the same or another directory:

Sub CopyFiles(strFilesToCopy As String, strDestinationDirectory As String)

    ' Call with CopyFiles "C:\Data\*.csv", "C:\Data"

    Dim strSourceFile As String
    Dim strDestinationFile As String
    Dim strSourceDirectory As String ' "C:\Data\"

    strSourceDirectory = Left$(strFilesToCopy, (InStr(strFilesToCopy, "*") - 1))

    strSourceFile = Dir(strFilesToCopy)

    ' loop through files in directory locating all .csv files
    Do While strSourceFile <> ""

        ' parse file name
        strDestinationFile = Replace$(strSourceFile, ".CSV", ".DAT")
        FileCopy strSourceDirectory & strSourceFile, _
            strDestinationDirectory & "\" & strDestinationFile

        strSourceFile = Dir

    Loop

End Sub


Reply With Quote
  #7 (permalink)  
Old January 10th, 2005, 03:50 PM
Authorized User
 
Join Date: Feb 2004
Location: , , Canada.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chacquard
Default

Thank you very much,

I used your second suggestion: it is short and simple and works very well.

Chantal

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to export the data to XML file format? mbshankar Pro VB 6 2 April 3rd, 2007 12:22 PM
export in txt format mallick_kuet C# 2005 0 January 20th, 2007 06:24 AM
export in txt format mallick_kuet BOOK: Professional Crystal Reports for VS.NET 0 January 20th, 2007 05:22 AM
Connecting and retrieving data from postgresql dat dotnetprogrammer VS.NET 2002/2003 0 February 19th, 2005 01:12 AM
Export in PDF Format MER78 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 October 9th, 2004 11:43 AM



All times are GMT -4. The time now is 07:40 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.