Convert Column Data in to CSV string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • artemetis
    New Member
    • Jul 2007
    • 76

    Convert Column Data in to CSV string

    I have a field of emailAddy in tblMain - consisting of some 100+ email addresses.

    I'd like to run a query that pulls each entry and outputs it to a txt/csv file in this format:

    emailAddy1,emai lAddy2,emailAdd y3, etc.

    I need all the emails to be in one consecutive string so I can copy/paste into a mass email.

    In advance...THANK S!
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    Without any consideration as to whether you are wanting to do it in the best way or not.

    Have you tried it with VBA?

    PSEUDOCODE
    [code=vba]
    Create a recordset object
    query the emails and fill the recordset
    create and open a file
    loop through the recordset
    add email from the current record in the recordset plus a comma to the file
    move to next record in the recordset
    repeat until end of recordset
    close file
    [/code]

    Comment

    • artemetis
      New Member
      • Jul 2007
      • 76

      #3
      Just something quick and easy.
      Not really caring how it's done....I'll just be copying and pasting the output.

      I thought of what you've posted, but not really sure how to approach this.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Execute the following code which will produce the desired results, writing them to a File Nnamed EMails.txt in the Root Directory of Drive C:, namely C:\EMails.txt:
        Code:
        Dim MyDB As DAO.Database
        Dim rstEMail As DAO.Recordset
        Dim strEMailAddrs As String
        
        Set MyDB = CurrentDb
        Set rstEMail = MyDB.OpenRecordset("tblMain", dbOpenForwardOnly)
        
        Open "C:\EMails.txt" For Output As #1
        
        Do While Not rstEMail.EOF
          If Not IsNull(rstEMail![emailAddy]) Then
            strEMailAddrs = strEMailAddrs & rstEMail![emailAddy] & ","
          End If
           rstEMail.MoveNext
        Loop
        
        strEMailAddrs = Left$(strEMailAddrs, Len(strEMailAddrs) - 1)
        
        Print #1, strEMailAddrs
        
        rstEMail.Close
        Set rstEMail = Nothing
        Close #1

        Comment

        • Delerna
          Recognized Expert Top Contributor
          • Jan 2008
          • 1134

          #5
          If adezii's code complains about the recordset object
          then you might want to check your references to see that the DAO type library is selected

          From the code page of your form
          choose the tools menu and the references option

          The type library you need is
          Microsoft DAO 3.6 Object Library



          Not really caring how it's done....I'll just be copying and pasting the output.
          And there's nothing wrong with that. If it works and performance is acceptable. By the way I wasn't trying to suggest that this is not the best way, just highlighting that I was simply answering your question, as asked.
          Compare adezii's code and my pseudo code and you will see similarities.
          I like to write my programs with pseudo code (commented out of course)
          and then write the actual code around the pseudo code...automati c comments :)

          Comment

          • artemetis
            New Member
            • Jul 2007
            • 76

            #6
            Thanks folks!

            ADezii's solution was just what I was looking for!!!

            Thanks again.
            Cheers!

            Comment

            Working...