PDA

View Full Version : How Do You Read a CSV File into a Word ADODB and place values into Work Bookmarks



Philip1
10-21-2016, 06:07 AM
Hi All.

I have the following code which doesn't seem to work.
Can any please asssist?

At the moment I'm just trying to read the CSV file and use msgbox to display field values:


Option Explicit

Private Sub BtnLoad_Click()
On Error GoTo Err_Catch

Err_Catch:
Resume Next

Dim Customer, Salutation, FirstName, Surname, Telephone, Mobile, SiteMobile, EMail, Address1, Address2, Address3, TownCity, _
PostCode, SageNo, CurrentStatus, DTA, SLADate, Services, BagLimit, Charge, ChargePeriod, Sites, strSQL As String
Dim Bookmark, CurrentField As Range

Dim CSVConnection As New ADODB.Connection
Dim rs As New ADODB.Recordset

Set CSVConnection = New ADODB.Connection
Set rs = New ADODB.Recordset

CSVConnection.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
& path & ";Extensions=asc,csv,tab,txt;HDR=NO;Persist Security Info=False"

strSQL = "SELECT * FROM Customer Report.csv"

rs.Open strSQL, CSVConnection, adOpenStatic, adLockReadOnly

If rs.RecordCount > 0 Then
Do
Customer = rs("Customer")
Salutation = rs("Salutation")
FirstName = rs("First Name")
Surname = rs("Surname")
Telephone = rs("Telephone")
Mobile = rs("Mobile")
SiteMobile = rs("Site Mobile")
EMail = rs("EMail")
Address1 = rs("Address 1")
Address2 = rs("Address 2")
Address3 = rs("Address 3")
PostCode = rs("Post Code")
SageNo = rs("Sage Account")
CurrentStatus = rs("Current Status")
DTA = rs("DTA Service")
SLADate = rs("SLA Date")
Services = rs("Current Services")
BagLimit = rs("Bag Limit")
Charge = rs("Charge")
ChargePeriod = rs("Charge Period")
Sites = rs("Sites")

rs.MoveNext
Loop Until rs.EOF
End If

rs.Close
Set CSVConnection = Nothing

End Sub

gmayor
10-21-2016, 06:54 AM
See your identical question at http://www.msofficeforums.com/word-vba/32881-trying-read-csv-file-place-values-into.html and please do not post to multiple forums without cross posting, to avoid a duplication of effort by those of us who answer such questions.

Philip1
10-21-2016, 07:54 AM
Different website domains mate, didn't know these forums were linked. sorry.

gmaxey
10-21-2016, 08:07 AM
They are not linked! Graham just participates in multiple forums as do I. The point is if you post in one place and Graham helps you. Then post the same question in another group and I help you then Graham or I one have wasted our time providing you with an answer that you have already been given. If you tell us that you have cross-posted and provide the link then we can check if you have been given the answer already.