TXT Tab Delimited (1 Viewer)

bhelmy

Registered User.
Local time
Today, 07:09
Joined
Dec 6, 2015
Messages
62
need I help
when this issue solve I will finish my project .

NEED CODE

1-
I have a ( Query1 ) I need to export it as a txt file tab delimited with a header of Query .
2- I have a ( Query2 ) I need to export it as a txt file tab delimited with a header of Query and space row before header .

thx
 

Attachments

  • 1.txt
    610 bytes · Views: 64
  • 2.txt
    612 bytes · Views: 57

namliam

The Mailman - AWF VIP
Local time
Today, 06:09
Joined
Aug 11, 2003
Messages
11,695
1. You can simply use the Docmd.Transfertext to create it
2. See 1, but you need some vba to make the blank line or a "smart" union query to insert the blank line.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:09
Joined
May 7, 2009
Messages
19,231
Code:
Option Compare Database
Option Explicit
'*********************
'* arnelgp
'*
'* parameters:
'*
'* strTable             table/query name or select statement
'* strOutput            path and filename to which it will saved (like: z:\test.txt)
'*                          if not supplied, file will be saved on same folder as the
'*                          db (text.txt)
'* strDelimiter         delimiter to use for export
'* bolExtraSpaceAbove   if you need extra space above your header, just passed True
'*

Public Sub DataToText(ByVal strTable As String, Optional ByVal strOutput As String = "", _
                            Optional ByVal strDelimiter As String = "", _
                            Optional ByVal bolExtaSpaceAbove As Boolean = False)
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim outFile As Integer
    Dim i As Integer
    Dim LineOfText As String
    
    If strOutput = "" Then strOutput = CurrentProject.Path & "\Text.txt"
    If strDelimiter = "" Then strDelimiter = Chr(9)
    If Dir(strOutput) <> "" Then Kill (strOutput)
    outFile = FreeFile
    Open strOutput For Output As #outFile
    Set db = CurrentDb
    Set rs = CurrentDb.OpenRecordset(strTable, dbOpenSnapshot)
    
    
    With rs
        If Not (.BOF And .EOF) Then
            .MoveFirst
            If bolExtaSpaceAbove Then
                Print #outFile, ""
            End If
            'if you want column headers, uncomment the following 5 lines of code
            For i = 0 To .Fields.Count - 1
                LineOfText = LineOfText & .Fields(i).Name & strDelimiter
            Next i
            LineOfText = Left(LineOfText, InStrRev(LineOfText, strDelimiter) - 1)
            Print #outFile, LineOfText

            'loop through records
            Do While Not .EOF
                LineOfText = ""
                'build up line of text
                For i = 0 To .Fields.Count - 1
                    LineOfText = LineOfText & Nz(.Fields(i)) & strDelimiter
                Next i
                LineOfText = Left(LineOfText, InStrRev(LineOfText, strDelimiter) - 1)
                'write line of text to file
                Print #outFile, LineOfText
            .MoveNext
            Loop
        End If
    End With
    
    Close #outFile
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    
End Sub
 

bhelmy

Registered User.
Local time
Today, 07:09
Joined
Dec 6, 2015
Messages
62
sorry
i am new vb devloper can you please put this code in a sampel witch attached
i form need buttom to export to C:\

PLEASE
 

Attachments

  • txt tab - Copy.accdb
    1.6 MB · Views: 49

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:09
Joined
May 7, 2009
Messages
19,231
it is better to save the text file to MyDocuments. saving to C:\ in some system, requires that you have all privelege in that drive.
 

Attachments

  • txt tab - Copy.accdb
    1.6 MB · Views: 68

bhelmy

Registered User.
Local time
Today, 07:09
Joined
Dec 6, 2015
Messages
62
thx sooooo mush
but one error more 3061 : too few parameters Expected 1
i have parameters in Qery


thx thx 4 every things
 

Attachments

  • txt tab - Copy.accdb
    1.6 MB · Views: 50

drybone

Registered User.
Local time
Today, 00:09
Joined
Mar 31, 2015
Messages
29
Is this exported txt file going to be used to populate a pdf?
 

bhelmy

Registered User.
Local time
Today, 07:09
Joined
Dec 6, 2015
Messages
62
i can't understand .... but this error when export the txt file
but solved when changing parameter as fixed one not depend on parameter in form
 

bhelmy

Registered User.
Local time
Today, 07:09
Joined
Dec 6, 2015
Messages
62
many thx
i finished my project --- u help me ....
 

Users who are viewing this thread

Top Bottom