can you help me fix the code?
Not asking for much, are you?????
(smile, I'm only joking!!!!)
OK, so I have never used the ADO Stream Object before.
With around 232,000 records, I don't know how long this will take. I would suggest you first try this code with a table that has 100 - 200 records.
Code:
Option Compare Database 'these two lines should be at the top of every code page
Option Explicit 'these two lines should be at the top of every code page
Private Sub Command6_Click()
On Error GoTo HandleErr
Dim MyTable As DAO.Recordset
Dim binaryStream As Object
Dim ADOStream As Object
Dim sString As String
Dim i As Integer
Dim FieldCount As Integer
Dim FileName As String
Dim Processing As Long
Dim ModNum as Integer
'text file name
FileName = "E:\Update web\text\all.txt"
Processing = 0
'open recordset
Set MyTable = CurrentDb.OpenRecordset("SELECT * FROM AllText")
If MyTable.BOF And MyTable.EOF Then
MsgBox "No records"
MyTable.Close
Set MyTable = Nothing
Exit Sub
End If
'populate record set
MyTable.MoveLast
'report after how many records??
If MyTable.Recordcount >500 then
ModNum = 100
Else
ModNum = 10
End If
MyTable.MoveFirst
'get number of fields in record set
FieldCount = MyTable.Fields.count - 1
'create the stream
Set ADOStream = CreateObject("ADODB.Stream")
'Init stream
ADOStream.Open
ADOStream.Position = 0
ADOStream.Charset = "UTF-8"
ADOStream.LineSeparator = adCRLF
'write field names
For i = 0 To FieldCount
sString = sString & MyTable.Fields(i).Name & ","
Next
'remove trailing comma
If Len(Trim(sString)) > 0 Then
sString = Left(sString, Len(sString) - 1)
End If
'write the header (field names)
ADOStream.writetext sString, adWriteLine
'get recordset data
Do While Not MyTable.EOF
sString = ""
Processing = Processing + 1
'tell me every ModNum of lines
If Processing Mod ModNum = 0 Then
' you need to create a text box on the form and
' un-comment the following line and another line at the bottom
' to see the record count
'Me.tbRecordCount = Processing
End If
'put the data in fields in a string
For i = 0 To FieldCount
sString = sString & MyTable.Fields(i) & ","
Next
'remove trailing comma
If Len(Trim(sString)) > 0 Then
sString = Left(sString, Len(sString) - 1)
End If
ADOStream.writetext sString, adWriteLine
MyTable.MoveNext
Loop
'save text file
ADOStream.SaveToFile FileName, 2
'Tell Me
' Me.tbRecordCount = Processing ' don't forget me.....
MsgBox "Done"
HandleErr_Exit:
'clean up
On Error Resume Next
ADOStream.Close
MyTable.Close
Set MyTable = Nothing
Exit Sub
HandleErr:
MsgBox Err.Number & " / " & Err.Description
Resume HandleErr_Exit
End Sub
If you create a text box control on the form, name it tbRecordCount and uncomment the two lines with blue.
Whew!!!! This should get you started....