If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > Update Access Table from Excel Sheet, ADO

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-20-04, 14:12
VectorX VectorX is offline
Registered User
 
Join Date: Mar 2004
Posts: 6
Question Update Access Table from Excel Sheet, ADO

Hi,

I am trying to Update a Access 2002 DB Table using the data from a Excel Sheet (Visual Basic 6).

---------------------------------------------------------
Dim cn As ADODB.Connection
Dim rsT As ADODB.Recordset

Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & "c:" & _
"\b.xls;Extended Properties=" & """Excel 8.0; HDR=No;"""
.CursorLocation = adUseClient
.Open
End With
Set rsT = cn.OpenSchema(adSchemaTables)

rsT.Open "SELECT * FROM [Sheet1$]", cn
----------------------------------------------------------

ok with that I can get the data in Excel to a RS, great. Now I need to use SQL to update it, "UPDATE SET;" to update the access table.

Why dont I use the rs.Update() method with a cn to the DB well that makes things complicated, and I can do it with the SQL faster and basically in one step, if I could set that part up.

Help would be appriciated.
Reply With Quote
  #2 (permalink)  
Old 03-21-04, 09:39
SCIROCCO SCIROCCO is offline
Registered User
 
Join Date: Mar 2004
Location: www.scirocco.ca
Posts: 346
Re: Update Access Table from Excel Sheet, ADO

Are you updating a table in access or adding new records to a table in access?

If you are adding new records do this:

rsT.MoveFirst
Do While not rsT.Eof

cn.Execute "INSERT Table1(Field1) SELECT '" & rsT.fields("Field1") & "'"

rsT.MoveNext

Loop

If you are updating existing records in a table do this (I am giving a simple linking example as I do not know how your recordset links to your table):

rsT.MoveFirst
Do While not rsT.Eof

cn.Execute "UPDATE Table1 SET Field1='" & rsT.fields("Field1") & "' FROM Table1 WHERE Table1.Field2='" & rsT.fields("Field2") & "'"

rsT.MoveNext

Loop
__________________
http://www.scirocco.ca/images/banner...occobanner.gif

Download for FREE the ADO/DAO Data Controls that makes life EASIER developing database applications in: VB, FoxPro, Access, VC++, .NET etc... Navigate, Add New, Delete, Update, Search, Undo and Save your changes. Supports Disconnected Recordsets and Transactions!

Or try our Ask An Expert service to answer any of your questions!
Reply With Quote
  #3 (permalink)  
Old 03-22-04, 00:18
VectorX VectorX is offline
Registered User
 
Join Date: Mar 2004
Posts: 6
SCIROCCO,

thanks for your reply, yes im trying to update the recs, and u code works for that. when i saw it, i went - why didnt i think of that.

boy did i feel stupid.

again thanks alot for you input.

-VX-
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 On
Pingbacks are On
Refbacks are On