Results 1 to 6 of 6
  1. #1
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317

    Exporting query to csv file: changing field names


    Hi Folks

    I need to export a query to a csv file using Visual Basic, but in the csv file two of the fields need to have the same name. So, for example, the fields called "Location_1" and "Location_2" in the query need both to be called "Location" in the csv file. How can I use Visual Basic to change the field names on exporting?

    NB Ideally both fields would be called "Location" in the query too, but trying to do this brings up a "duplicate output alias" error.

    Thanks

    Remster

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can use automation to edit the values in cells. Something like
    xlApp.ActiveSheet.Cells(1, 5).Value = "Location"

    https://www.accessforums.net/macros/...tml#post240852

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Access won't let you name two fields in the same query with the same name (hence, you would not be able to export them with the same name using normal exporting conventions).

    I might approach this a little differently.

    First, create a one record table that lists all the field name that you want. So in that table, your Field names might be something like "Field1, Field2, Field3, etc", and the values in your one record are the actual field names that you want to export. Then add an extra field at the end named "Sort1" and set this value to 1.

    Then, in your original query that has the actual data that you want to export, add a "Sort1" field to the end, and set this value to 2.

    Then, create a Union Query which combines your two queries together, and sorts by the "Sort1" field, so your "field names" show up in the first row.
    Then exporting the data WITHOUT headers.

    Note that if you have fields in your original query that are NOT text (i.e. numeric or date), you may need to use the FORMAT function to convert them to Text in order to combine them in your Union query (fields that line up usually have to be the same data type).

    Other options include:
    - To write VBA to export the records line-by-line (and you can tell it what to export for the first row, your field names)
    - Export it as it is, using field names "Location" and "Location_1", and after export replace "Location_1" with "Location", either manually or by some sort of script.
    - Create a one-line CSV file that has all the headers just the way you like them. Export the data without headers. Use a script to combine the one-line header CSV file with your data file.

  4. #4
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Many thanks for your replies.

    JoeM, I'm going with your main suggestion. Just one more puzzle. I want if possible to achieve everything in one query, but I don't want to export the Sort1 column. At the moment I have:

    SELECT Stuff1, Stuff2, Stuff3, 2 As Sort1 ----- I can't add fields to this table
    FROM Stuff
    UNION ALL
    SELECT Field1, Field2, Field3, Sort1
    FROM Fields
    ORDER BY Sort1, Stuff3

    Any suggestions? (I can get the field names at the start by selecting the fields from Fields before I select the fields from Stuff, but then I can't sort by Stuff 3, because I can sort only by fields from the first table.)

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You could create a query like this (and give it any name, I will use "Query1" for this example.:
    Code:
    SELECT Stuff1, Stuff2, Stuff3, 2 As Sort1
    FROM Stuff
    UNION ALL
    SELECT Field1, Field2, Field3, Sort1
    FROM Fields;
    You can then create a query off of this query.
    Add all the, including the SORT1 field.
    Then uncheck the "Show" box under the SORT1 field, but select the "Ascending" sort option under it.
    Then it will include this field in your sort, but not your export if you export this second query.

  6. #6
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Ah, I see, a second query that selects all the fields from Query1. That would certainly save me the trouble of listing them.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Exporting a query to Excel File
    By crowegreg in forum Import/Export Data
    Replies: 2
    Last Post: 08-08-2013, 05:25 PM
  2. Changing names to ID numbers
    By marksnwv1957 in forum Access
    Replies: 4
    Last Post: 08-28-2012, 03:47 PM
  3. Reports and changing the names
    By brobb56 in forum Access
    Replies: 9
    Last Post: 09-21-2011, 03:57 PM
  4. Changing names and labels
    By Geewaagh in forum Reports
    Replies: 29
    Last Post: 04-20-2010, 10:26 PM
  5. Changing field names in queries
    By scottsoo9 in forum Queries
    Replies: 1
    Last Post: 12-01-2009, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums