Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Adding/Updating Records in Access with Excel

Hi Everyone-

I have found some code that has helped me add records to an Access (2003)
table. I am having difficulty though finding a way to tell the VBA to either
Update/Append or add a new record when I click the button. My Unique field
in excel is in col. A and is tied to the Access field name "Project_Name".
If the record, based on the ID, already exists, I would like for it to simply
update all of the records. If the record is new, based on the project ID, I
would like for it to add a new row in the access DB. Below is the code that
I have so far...

DB name: Test Project DB.mdb
Table Name: ProjectDataCollection
Unique ID Location in Excel: Column A ("Project_Name")

Private Sub CommandButton1_Click()

' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=\\HSG\Files\HSG Resources\PM Task Force 2006\Tools -
Design Versions\Test Project DB.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "ProjectDataCollection", cn, adOpenKeyset, adLockOptimistic,
adCmdTable
' all records in a table
r = 2 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) 0
' repeat until first empty cell in column A
.AddNew ' create a new record
' add values to each field in the record
.Fields("Project_Name") = Range("A" & r).Value
.Fields("Estimated_Start") = Range("B" & r).Value
.Fields("Estimated_Launch_Fielding_Date") = Range("C" & r).Value
.Fields("Estimated_Close_Fielding_Date") = Range("D" & r).Value
.Fields("Estimated_Finish_Date") = Range("E" & r).Value

' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

End Sub

Thank you for your help!!!!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Adding/Updating Records in Access with Excel

You need a line: With rs
befo .Add New

Hth,
Merjet


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Adding/Updating Records in Access with Excel

Will this check to see if the record is already there?

"merjet" wrote in message
ups.com...
You need a line: With rs
befo .Add New

Hth,
Merjet



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Adding/Updating Records in Access with Excel

No. You need to search the records if you want to update existing
ones. The records having a primary key would make it easier to find a
match. The steps would be as follows. For each row in Excel search the
Access table. If a match is found, update the record. If a match is
not found, add a new record.

Hth,
Merjet


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 81
Default Adding/Updating Records in Access with Excel

Merjet,

Then how to amend the code, if I need to search the records and update
exsiting one?

"merjet" wrote:

No. You need to search the records if you want to update existing
ones. The records having a primary key would make it easier to find a
match. The steps would be as follows. For each row in Excel search the
Access table. If a match is found, update the record. If a match is
not found, add a new record.

Hth,
Merjet





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Adding/Updating Records in Access with Excel

Try this, changing references to suit.

Hth,
Merjet

r = 2 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) 0
' repeat until first empty cell in column A
bFound = False
With rs
.MoveFirst
Do
If .Fields("FieldName1") = Range("A" & r).Value then
.Fields("FieldName2") = Range("B" & r).Value
' edit more fields as needed
.Update ' stores the new record
bFound = True
End If
.MoveNext
Loop Until rs.EOF
If rs.EOF And bFound = False Then
.AddNew ' create a new record
' add values to each field in the record
.Fields("FieldName1") = Range("A" & r).Value
.Fields("FieldName2") = Range("B" & r).Value
' add more fields as needed
.Update ' stores the new record
End If
End With
r = r + 1 ' next row
Loop


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Records from Access Database table(more than 5 lakh records in this table) to Excel Sheet divya Excel Programming 1 October 26th 06 12:12 PM
Updating Records from MS Access Andrew Thacker Excel Discussion (Misc queries) 1 March 27th 06 08:10 AM
Updating records in an Access DB Lucho Excel Programming 3 September 25th 05 02:56 PM
Adding Records to Access Table Mark[_22_] Excel Programming 0 November 25th 03 10:54 PM
Access records updating from Excel. Dirk Batenburg Excel Programming 2 October 8th 03 03:06 PM


All times are GMT +1. The time now is 12:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"