Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 8th, 2014, 05:31 AM
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #2 (permalink)  
Old July 2nd, 2014, 01:33 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #3 (permalink)  
Old February 22nd, 2018, 09:40 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:24 PM.


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