![]() |
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!!!! |
Adding/Updating Records in Access with Excel
You need a line: With rs
befo .Add New Hth, Merjet |
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 |
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 |
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 |
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 |
All times are GMT +1. The time now is 05:19 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com