Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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

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
zippy221
Asked:
zippy221
1 Solution
 
manoj_rCommented:
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
 
alamarCommented:
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
 
SimonacCommented:
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
 
zippy221Author Commented:
Thanks that was really clear and to the point.
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Tackle projects and never again get stuck behind a technical roadblock.
Join Now