G
Guest
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!!!!
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!!!!