|
-
Apr 12th, 2000, 07:50 PM
#1
Thread Starter
Fanatic Member
Anyone got any neat ways of saving the contents to, say, a richtext file, or do I have to generate a richtext box at the same time?
-
Apr 13th, 2000, 03:59 AM
#2
Thread Starter
Fanatic Member
Me again, just putting this back to the top of the list...
-
Apr 13th, 2000, 07:46 AM
#3
Fanatic Member
Try this:
I made this example against an Access Database. Drop two text boxes and a command button and name it to the following. Cut and copy the code below. This will write out to a text file. I even have codes that puts it into an Excel file.
__________________________________________________________
Option Explicit
Private Sub Form_Load()
Me.Caption = "SELECT * FROM [Inventory_State]"
txtDatabaseName.Text = "C:\Microsoft\Visual Basic 5\Database\TestFile.mdb"
txtFileName.Text = App.Path & "\books.txt"
End Sub
'OVERALL: Export the file.
Private Sub cmdExport_Click()
On Error GoTo MiscError
' Open the output file.
Dim int_fnum As Integer
int_fnum = FreeFile
Dim file_name As String
file_name = txtFileName.Text
Open file_name For Output As int_fnum
' Open the database.
Dim db As Database
Set db = OpenDatabase(txtDatabaseName.Text)
' Open the recordset.
Dim rs As Recordset
Set rs = db.OpenRecordset(Me.Caption)
' Start with the names of the fields.
Dim int_Total_Columns As Integer
int_Total_Columns = rs.Fields.Count
Dim int_Column_Width() As Integer
ReDim int_Column_Width(0 To int_Total_Columns - 1)
Dim int_X As Integer
For int_X = 0 To int_Total_Columns - 1
' We're only working with Text here. Other
' types are different. For example, an
' integer may take 2 bytes to store but 6
' characters to display.
int_Column_Width(int_X) = rs.Fields(int_X).Size
If int_Column_Width(int_X) < Len(rs.Fields(int_X).Name) Then
int_Column_Width(int_X) = Len(rs.Fields(int_X).Name)
End If
int_Column_Width(int_X) = int_Column_Width(int_X) + 1
Print #int_fnum, rs.Fields(int_X).Name;
Print #int_fnum, Space$(int_Column_Width(int_X) - Len(rs.Fields(int_X).Name));
Next
Print #int_fnum, ""
' Process the records.
Dim str_Field_Value As String
Dim num_processed As Integer
Do While Not rs.EOF
num_processed = num_processed + 1
For int_X = 0 To int_Total_Columns - 1
str_Field_Value = rs.Fields(int_X).Value
Print #int_fnum, str_Field_Value & Space$(int_Column_Width(int_X) - Len(str_Field_Value));
Next
Print #int_fnum, ""
rs.MoveNext
Loop
' Close the file and database.
rs.Close
db.Close
Close int_fnum
MsgBox "Processed " & Format$(num_processed) & " records."
Exit Sub
MiscError:
MsgBox "Error " & Err.Number & _
vbCrLf & Err.Description
End Sub
[Edited by Nitro on 04-13-2000 at 08:49 PM]
Chemically Formulated As:
Dr. Nitro
-
Apr 13th, 2000, 06:24 PM
#4
Thread Starter
Fanatic Member
Cheers Doc, I'll give it a go in a mo.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|