Results 1 to 11 of 11
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Import a Comma Separated Text File Into ONE Access Column

    I have a text file that I need to import into access, and it has data like 1,3,5,22,44,18

    When I try to import the data it creates a column for each value after the column. What I am wanting is to import each value into ONE column.



    How would I do this with access vba?

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I'd say read it as a text file and dump the contents into a record. Something like this (should be ok even though it's Excel)
    https://www.automateexcel.com/vba/re...e-into-string/
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by Micron View Post
    I'd say read it as a text file and dump the contents into a record. Something like this (should be ok even though it's Excel)
    https://www.automateexcel.com/vba/re...e-into-string/
    okay - i understand the ReadTextFile() method, and see that my data would be stored in the variable strText

    How do I then use a recordset to save to a table?

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Use Tab delimited?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by Welshgasman View Post
    Use Tab delimited?
    the file is received as a comma delimited :/

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Yes, but if you say it is Tab delimited all the data comes in as one field.
    I imported it manually, but the logic should be the same, especially if you have the table already created?

    I am assuming this is a regular occurrence if you want to do it via VBA?
    Attached Thumbnails Attached Thumbnails Tab Delimited CSV.PNG  
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    is there a way to convert from a comma seperated to tab seperated file?

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by jo15765 View Post
    is there a way to convert from a comma seperated to tab seperated file?
    Yes, via Excel. Save as Tab delimited.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    jo15765,

    Can you tell us a little more about your project? It would help potential responders if there was more context. This is 1 csv file, but it must be used in conjunction with other data. Can you give us the 30,000 ft overview?

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    How do I then use a recordset to save to a table?
    Save what to a table? I interpreted your post as wanting to save one huge blob into one table record in one field. Now it is to be saved as delimited in some fashion?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    jo15765,
    I interpreted your requirement to:
    -you have a text file that has numbers separated with commas (comma separated variables)
    -you want to read that file
    -for each value create a new record in an Access table.

    I mocked up a sample that may help.

    Input file
    Name:"C:\users\jp\documents\documents_Lenovo\docum ents\samplecsv.txt"
    Values:12,23,24,26,32,43,54,34,58

    Output table
    Name: "joSample"
    Fields: RecId autonumber
    RecVal number
    Records:
    recid recval
    1 12
    2 23
    3 24
    4 26
    5 32
    6 43
    7 54
    8 34
    9 58

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: readcsv
    ' Purpose: Read a comma separated file and use the values between commas as records in atable
    ' Procedure Kind: Sub
    ' Procedure Access: Public
    ' Author: Jack
    ' Date: 28-Jul-23
    ' ----------------------------------------------------------------
    Sub readcsv()
              Dim SourceFileName As String 'full name of source txt file with csv data
              Dim db As DAO.Database  
              Dim rsOut As DAO.Recordset
              Dim strLine As String              
              Dim aryLine() As String          '
              Dim SourcefileNum As Integer
              Dim commaCount As Long
              Dim sql As String
              
    10        Set db = CurrentDb
              
    
    20        sql = "Create Table joSample (recid autoincrement not null primary key, recval integer null)"  'create table joSample
    30        db.Execute "Drop table joSample"   'remove table joSample if it exists
    40        db.Execute sql
    50        Debug.Print "Start: "; Now()
    60        SourceFileName = "C:\users\jp\documents\documents_Lenovo\documents\samplecsv.txt"
    70        SourcefileNum = FreeFile()
    80        Open SourceFileName For Input As #SourcefileNum
    90        Set db = CurrentDb
    100       Set rsOut = db.OpenRecordset("joSample")
             
              'Loop through the source file, parse it, and create records in table joSample
    110       While Not EOF(SourcefileNum)
    120           Line Input #SourcefileNum, strLine
    130           aryLine = Split(strLine, ",")         'split the strLine into array elements
    140           For commaCount = LBound(aryLine) To UBound(aryLine)
    150               Debug.Print aryLine(commaCount)
    160               rsOut.AddNew
    170               rsOut!RecVal = aryLine(commaCount)
    180               rsOut.Update
    190           Next
    200       Wend
    210       rsOut.Close
    220       db.Close
    230       Debug.Print "Finished: "; Now()
    End Sub
    Debug.Print output to immediate window

    Start: 28-Jul-23 4:34:58 PM
    12
    23
    24
    26
    32
    43
    54
    34
    58
    Finished: 28-Jul-23 4:34:58 PM
    Last edited by orange; 07-29-2023 at 06:36 AM. Reason: spelling

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

Similar Threads

  1. Replies: 1
    Last Post: 11-23-2017, 03:22 PM
  2. Replies: 3
    Last Post: 10-24-2017, 07:54 AM
  3. Replies: 4
    Last Post: 05-07-2015, 04:02 PM
  4. Replies: 8
    Last Post: 06-04-2014, 10:41 AM
  5. exporting query to text comma separated
    By Pasi in forum Queries
    Replies: 6
    Last Post: 12-27-2013, 06:42 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