Results 1 to 9 of 9

Thread: CSV to access with vba

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    5

    CSV to access with vba

    Hi,
    could you help me please.

    I have csv file:

    #text1.1=#text2.1 !text3.1
    #text1.2=#text2.2 !text3.2
    ...

    I need fill this csv onto access table with vba:
    table:
    ----------------------------------------------------
    | ID(automatic) | all row | text1.x | text2.x | text3.x |
    ----------------------------------------------------
    Could you help me with vba, please (beginner)

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV to access with vba

    You can use the .TransferSpreadsheet method inside of Access to Import the sheet in one line of code.
    VB Code:
    1. DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Table1", "C:\Test.xls", False
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: CSV to access with vba

    I don't think that'll work for him, from his example. No common delimiters, for one thing. It looks like he'll have to read it in and parse the string somehow.
    Tengo mas preguntas que contestas

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV to access with vba

    Ah yes, I missed that. Then its not a Comma Separated Values file.
    I was going to add that in order todo csv into Access you would need to open the csv in excel programmaticalls and then save as a xls file. Then execute the transfer.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Re: CSV to access with vba

    csv for examle:
    #a_string1=#a_string2 !a_string3 ...first row of csv(record of table)
    #a_string1=#a_string2 !a_string3 ...second rowof csv(record of table)
    ...

    I have in access this table:
    my_table(
    id automatic_number,
    all_row text,
    string1 text,
    string2 text,
    string3 text,
    )

    my_table.all = all row in csv
    my_table.string1 = string between first "#" and first "="
    my_table.string2 = string between second "#" and first " !"
    my_table.string3 = string between first " !" and end of row(\n)

    I'd like fill data from csv into my_table.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV to access with vba

    You can parse out the data using Instr and Mid$
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Re: CSV to access with vba

    and how save data into table (beginner)?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CSV to access with vba

    Easiest is to use ADO. Here is one of best tutorials on ADO and DBs.

    http://www.vbforums.com/showthread.php?t=81916
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: CSV to access with vba

    Just because you have a file with a .csv extension doesn't mean it's a csv file. csv stands for comma separated values. In that case, you data would look something like this:
    VB Code:
    1. #a_string1=, #a_string2, !a_string3
    You can use something besides a comma as a delimiter - the pipe symbol is a common one - but at any rate, it has to be the same delimiter.
    If you didn't want to strip out the #, =, and ! symbols, and you just wanted the individual values, you could import in one line of code.
    As it is, you'll have to read each line in individually in a loop, strip out the parts you don't want, then do an update on your table. If there are a lot of records, that could take a while.
    Tengo mas preguntas que contestas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width