PC Review


Reply
Thread Tools Rate Thread

ADODB Problem with transferring data from Excel to Access

 
 
wjc.vrieling@gmail.com
Guest
Posts: n/a
 
      1st Aug 2008
Hello,

I want to transfer data from Excel sheet 'DATA' to Access table
'300_APO PRICELIST'.

As you can see in the below script I can only append data to the
Access table, but I also want Access to update the table in case of
already existing data.

I spent all afternoon to figure out how to do that but with no
results. Can somebody help?

Thank you in advance!!!

Gr,

Chris

Sub UploadP(Version, EstNumber, MyFilter)

Dim MyConnect As String
Dim MyAccess As String
Dim MyRecordset As ADODB.Recordset
Dim MyRange As String
Dim MySQL As String
Dim MyTable As ADODB.Recordset

MyConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & _
"Extended Properties=Excel 8.0"

MyAccess = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=F:\Departments\Business Finance\10 - SOURCES\Database
\FHC_Database.mdb"


Set MyTable = New ADODB.Recordset

Set MyTable = Nothing

MySQL = "SELECT DISTINCT [CODE],[Shipto_Customer],
[Shipto_Customer_Name],[Material_No]," & _
"[Material_Name],[cal_month], [PRICE]" & _
"FROM [DATA$]" & _
"WHERE([Data type] ='APO') and ([Category]='" & MyFilter &
"')"

Set MyRecordset = New ADODB.Recordset
MyRecordset.Open MySQL, MyConnect, adOpenStatic, adLockReadOnly

Set MyTable = New ADODB.Recordset
MyTable.Open "SELECT * FROM [300_APO PRICELIST]", MyAccess,
adOpenDynamic, adLockOptimistic

Do Until MyRecordset.EOF


MyTable.AddNew


[MyTable]![code] = [MyRecordset]![code]
[MyTable]![Shipto Customer] = [MyRecordset]![Shipto_Customer]
[MyTable]![Shipto Customer Name] = [MyRecordset]!
[Shipto_Customer_Name]
[MyTable]![Material No] = [MyRecordset]![Material_No]
[MyTable]![Material Name] = [MyRecordset]![Material_Name]
[MyTable]![Cal year / month] = [MyRecordset]![cal_month]
[MyTable]![Unit price - average] = [MyRecordset]![Price]

MyTable.Update

MyRecordset.MoveNext
Loop

Set MyTable = Nothing
Set MyRecordset = Nothing


End Sub
 
Reply With Quote
 
 
 
 
Dick Kusleika
Guest
Posts: n/a
 
      1st Aug 2008
On Fri, 1 Aug 2008 10:45:47 -0700 (PDT), (E-Mail Removed) wrote:

>Hello,
>
>I want to transfer data from Excel sheet 'DATA' to Access table
>'300_APO PRICELIST'.
>
>As you can see in the below script I can only append data to the
>Access table, but I also want Access to update the table in case of
>already existing data.
>


I made a few changes to the field names to fit my testing data, so don't
just copy and paste this code.

Do Until MyRecordset.EOF

'Set the record pointer
MyTable.MoveFirst
'This will move the record pointer if code exists
MyTable.Find "[code] = " & [MyRecordset]![code]

'EOF is true if nothing found, so add a new record
If MyTable.EOF Then
MyTable.AddNew
End If

[MyTable]![code] = [MyRecordset]![code]
[MyTable]![Shipto_Customer] = [MyRecordset]![Shipto_Customer]
[MyTable]![Shipto_Customer_Name] =
[MyRecordset]![Shipto_Customer_Name]
[MyTable]![Material_No] = [MyRecordset]![Material_No]
[MyTable]![Material_Name] = [MyRecordset]![Material_Name]
[MyTable]![cal_month] = [MyRecordset]![cal_month]
[MyTable]![Price] = [MyRecordset]![Price]

MyTable.Update

MyRecordset.MoveNext
Loop
--
Dick Kusleika
Microsoft MVP-Excel
http://www.dailydoseofexcel.com
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transferring data from Access to Excel in VBA very slow Michel S. Microsoft Access 7 28th Aug 2006 06:14 PM
Transferring data from MS Access to MS Excel Joe Microsoft Excel Misc 2 5th Apr 2006 06:24 PM
Access to Excel Automation ADODB Problem Matt Slattery Microsoft Excel Programming 2 29th Sep 2004 12:59 PM
Problems in transferring data from access to excel Shaun Coney Microsoft Access External Data 1 21st Apr 2004 07:26 PM
transferring data from access to excel dave Microsoft Excel Programming 8 2nd Sep 2003 03:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:06 AM.