save as a text file

slygirl
08-30-2000, 04:39 AM
I'm writing a database program. Instead of saving to a database file, I want it to save as a text file. I wonder if it's possible to do it. Each field should be separated by ',' Thank you..

whelanp
08-30-2000, 05:59 AM
Its extremely easy to save data to a text file from VB.

OPEN "c: mp.txt" for ouput as #1

Do while not finished



assign variables here

Print #1 , intFieldId & ", " & strField1 & ", " & strField2 ..etc

Loop

close #1

This is the most basic way of saving data to a file (there are loads of alternatives)

Check out the help for the OPEN and PRINT commands.

However unless there is a VERY good reason for not doing so, you should keep your data in database.

Birddog
08-31-2000, 03:57 PM
Here is some code, copied from a routine in VBA, to get you started.
There's extra code but the basic idea is there

Private Sub cmdExtract_Click()
'rtn will export data to text file

Dim fso As New FileSystemObject
Dim txtFile As File
Dim ts As TextStream
Dim intx1 As Integer, intTableLimit As Integer
Dim strTextDelim As String, strDelimiter As String

cmdlgFile.DialogTitle = "Export File"
blnExport = True

SubGetFileID ' get output file id
If Len(strFileName) = 0 Then
Exit Sub
End If

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile (strFileName)
Set txtFile = fso.GetFile(strFileName)
Set ts = txtFile.OpenAsTextStream(ForWriting)
strSFEType = InputBox("Enter SFE Type to be Exported, 1-digit")

' set up progress bar

prgBar.Min = 0
prgBar.Visible = True
staBar.Caption = "Exporting Table"

' open the output result table
If blnIDNbrsOpen = False Then
Set rsIDNbrs = gdbsSFEDealer.OpenRecordset("tblDealerSatIDs", dbOpenDynaset)
blnIDNbrsOpen = True
End If

strTextDelim = """"
strDelimiter = ";"

' get # records in table

With rsIDNbrs
intTableLimit = .Fields.Count - 1 ' bypass 1st field seq #
' output fields names
strOutputBuffer = strTextDelim
For intx1 = 1 To intTableLimit
strOutputBuffer = strOutputBuffer & .Fields(intx1).Name & strTextDelim & strDelimiter
Next intx1
ts.WriteLine strOutputBuffer
strOutputBuffer = vbNullString
.MoveFirst
prgBar.Max = 1
Do While .EOF = False
prgBar.Max = prgBar.Max + 1
.MoveNext
Loop
.MoveFirst
prgBar.Value = 1
Do While .EOF = False
If !SFEType = strSFEType Then
For intx1 = 1 To intTableLimit
If .Fields(intx1).Value = "" Then
strOutputBuffer = strOutputBuffer & strDelimiter
ElseIf .Fields(intx1).Type <> 10 Then ' text field type
strOutputBuffer = strOutputBuffer & .Fields(intx1).Value & _
strDelimiter
Else
strOutputBuffer = strOutputBuffer & strTextDelim & _
.Fields(intx1).Value & strTextDelim & strDelimiter
End If
Next intx1
ts.WriteLine strOutputBuffer
strOutputBuffer = vbNullString
End If
.MoveNext
prgBar.Value = prgBar.Value + 1
Loop
End With
ts.Close
prgBar.Visible = False
staBar.Caption = "Export Complete"
Me.Refresh
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum