PC Review


Reply
Thread Tools Rate Thread

Adding/Updating Records in Access with Excel

 
 
=?Utf-8?B?QkNMaXZlbGw=?=
Guest
Posts: n/a
 
      12th Jun 2007
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!!!!

 
Reply With Quote
 
 
 
 
merjet
Guest
Posts: n/a
 
      12th Jun 2007
You need a line: With rs
before: .Add New

Hth,
Merjet


 
Reply With Quote
 
BCLivell
Guest
Posts: n/a
 
      12th Jun 2007
Will this check to see if the record is already there?

"merjet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> You need a line: With rs
> before: .Add New
>
> Hth,
> Merjet
>
>


 
Reply With Quote
 
merjet
Guest
Posts: n/a
 
      12th Jun 2007
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


 
Reply With Quote
 
=?Utf-8?B?QW5ndXM=?=
Guest
Posts: n/a
 
      14th Jun 2007
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
>
>
>

 
Reply With Quote
 
merjet
Guest
Posts: n/a
 
      14th Jun 2007
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 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
Adding new records and updating existing records from different ta CBeavers Microsoft Access External Data 1 13th May 2008 01:20 AM
Updating access with additional records in excel spreadsheet =?Utf-8?B?UmVuZWU=?= Microsoft Access 1 24th Jan 2006 09:30 PM
Updating and Adding Records via a Query ZBC Microsoft Access Queries 2 5th May 2004 12:14 PM
Re: adding / updating records in a datagrid Dmitriy Lapshin [C# / .NET MVP] Microsoft VB .NET 0 10th Oct 2003 09:53 AM
Access records updating from Excel. Dirk Batenburg Microsoft Excel Programming 2 8th Oct 2003 03:06 PM


Features
 

Advertising
 

Newsgroups
 


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