Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2014, 05:31 AM
Registered User
 
Join Date: Apr 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to read data from csv file and write it into Access Table

Hi Gurus ,

Am new to VB programming,I have a requirement where, there will be table which is already created in Access and I need to write code which will read the file and enter the data in the record. Please help me out how to achieve this. I use Access as my DB

The input file is .CSV file and its in format of :
FIRST_NAME, LAST_NAME, HOME_ADD, COMPANY, DESIG
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC

This values sholud be read from "C:\input\data.csv" folder read and write in Access table which is already created.
 
Old July 2nd, 2014, 01:33 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default See VBScript

You can use VBScript to accomplish this. Try:

Code:
 
Dim MyArray As Variant
Dim fso As Variant
Dim objStream As Variant
Dim objFile As Variant
Dim sSQL As String
Dim rs As ADODB.Recordset '(or some other method)
Dim i As Integer
i = 0
 
sSQL = "SELECT * FROM tblMyLocalTable"
Set rs = New ADODB.Recordset
rs.open sSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
 
 
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:\input\data.csv") Then
    Set objStream = fso.OpenTextFile("C:\input\data.csv", 1, False, 0)
End If
Do While Not objStream.AtEndOfStream
    strLine = objStream.ReadLine
       ReDim MyArray(0)
    MyArray = Split(strLine, ",")
    
     rs.AddNew
     rs("FIRST_NAME") = MyArray(0)
     rs("LAST_NAME") = MyArray(1)
     rs("HOME_ADD") = MyArray(2)
     rs("COMPANY") = MyArray(3)
     rs("DESIG") = MyArray(4)
     rs.Update
     i = i + 1
Loop
 
MsgBox i
Well, you get the idea. Take out the first line of the csv file so there are no column heading lines.

I hope that helps, or gives you some ideas.
__________________
mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old February 22nd, 2018, 09:40 PM
Registered User
 
Join Date: Feb 2018
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code Posting helpfull

Thanks mmcdonal, your code that you posted was just what I was looking for, much appreciated.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a csv file and write the data into Access table in VB rockysingh Pro VB Databases 0 April 8th, 2014 05:27 AM
read from csv file black2 C# 2008 aka C# 3.0 3 July 7th, 2009 04:11 AM
csv file into access table with vba Perseus Access VBA 2 September 14th, 2005 02:57 PM
ASP to read .CSV into Access liquidmillsap Classic ASP Databases 1 January 4th, 2005 11:01 AM
Logging File Access Read & Write ntbluez VBScript 1 November 13th, 2003 12:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.