Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exporting an Access Table to a Text File in a Visual Basic Program

Posted on 2000-05-02
4
Medium Priority
?
200 Views
Last Modified: 2013-11-25
Hello!  
I have an Access 97 database called Dogs, and I have a table called Breeds.  In my Visual Basic 6.0 proggie, I have a menu item called mnuFileExport.  When this menu item is clicked, the table Breeds should be exported to a text file called BreedsUpdate.txt on the A: drive.  How would I do this?  

Here is what I have so far and it doesn't work.
Private Sub mnuFileExport_Click()
Open "C:\Windows\Desktop\Dogs.mdb" For Input As #1
Open "A:\BreedsUpdate.txt" For Output As #2
Print #1, txtType.Text; txtDescription.Text; txtPrice.Text
Close #1
Close #2
End Sub

Thanks :)
0
Comment
Question by:zippy221
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Accepted Solution

by:
manoj_r earned 80 total points
ID: 2770821
You can open a recordset and go through the records and print them in the file.

Try the code given below.

Dim db As Database
Dim rst As Recordset
Open "C:\BreedsUpdate.txt" For Output As #2
    Set db = OpenDatabase("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
    Set rst = db.OpenRecordset("Select * from customers")
   
    Do While Not rst.EOF
        Print #2, rst.Fields(0) & vbTab & rst.Fields(1)
        rst.MoveNext
    Loop
   
Close #2

0
 

Expert Comment

by:alamar
ID: 2771026
Well, let's see if we can make this a little more readable.
Try somthing like this:

'--------------------------------------------------------

Dim dbDogs As Database
Dim rsBreeds As Recordset
Dim i as integer         'Counter
Dim strOut as string  'output record string

'Clear out variables
strOut = ""
i=0

'Open file for output
Open "C:\BreedsUpdate.txt" For Output As #1

'Open the database
Set dbDogs = OpenDatabase("C:\Windows\Desktop\Dogs.mdb")

'build your recordset
Set rsBreeds = dbDogs.OpenRecordset("Select * from Breeds")

'go through each record, and output each record                                
Do While Not rsBreeds.EOF
    'First, build your output string
      for i=0 to rsBreeds.fields.count-1
           
          strOut = strOut & rsBreeds.fields(i) & ", "           'Make comma the delimiter
      next i
       'get rid of the last comma
        strOut = mid(strOut, 1, len(strOut)-2)

  Print #1, strOut

'Clear strOut
  strOut = ""
  rsBreeds.MoveNext
Loop
                                 
Close #1

'close files
rsBreeds.close
dbDogs.close

'--------------------------------------------------------
Hope this helps!


Alamar the WwonderLlama
0
 
LVL 2

Expert Comment

by:Simonac
ID: 2774816
U can use the following function:

Dim appAccess As Access.Application

appAccess.DoCmd.TransferText [parameters]

The equivalent also exist to tansfer to excel, Lotus1-2-3, etc...
0
 

Author Comment

by:zippy221
ID: 2778560
Thanks that was really clear and to the point.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question