Hello All,
I'm currently using a macro that pushes data from individual cells in Excel to the appropriate column in an Access table (referred to throughout as 'Table') at the click of a button. However, the code I'm using will currently only add a new record to the database whereas I want it to be able to update a record where the data in certain fields (the primary keys of the table) already exist. Here is the code I am using (the template for which was given to me by forum user CalcSux78 in this thread -> http://www.mrexcel.com/forum/excel-questions/910496-macro-export-individual-cells-table-access.html):
So let's say that FieldName1 is the primary key in Table. A record exists that already contains the value of cell A1 in the FieldName1 field within Table, however I have changed the contents of cells C3 and C4 and need the same button to reflect that update in Table (in FieldName2 and FieldName3, respectively). I imagine I can use some logic like:
"If FieldName1 Exists, then Update FieldName2 and FieldName3.
Else, Add FieldName1, FieldName2, and FieldName3."
The issue is that I am very new to VBA/macros/coding in general and have no idea how to execute this, so I am looking for any help, assitance, or pointer fingers in the right direction that someone could provide.
Thanks in advance for any and all insight!
I'm currently using a macro that pushes data from individual cells in Excel to the appropriate column in an Access table (referred to throughout as 'Table') at the click of a button. However, the code I'm using will currently only add a new record to the database whereas I want it to be able to update a record where the data in certain fields (the primary keys of the table) already exist. Here is the code I am using (the template for which was given to me by forum user CalcSux78 in this thread -> http://www.mrexcel.com/forum/excel-questions/910496-macro-export-individual-cells-table-access.html):
Code:
Public Const Conn As String = "Data Source=Path\Database.accdb;"
Private Sub Export_Click()
Dim rs As New ADODB.Recordset
Dim cn As New ADODB.connection
Dim tbl As String
tbl = "Table"
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & Conn
rs.Open tbl, cn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
With rs
.AddNew
.Fields("FieldName1") = [A1]
.Fields("FieldName2") = [C3]
.Fields("FieldName3") = [C4]
.Update
.Close
End With
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
So let's say that FieldName1 is the primary key in Table. A record exists that already contains the value of cell A1 in the FieldName1 field within Table, however I have changed the contents of cells C3 and C4 and need the same button to reflect that update in Table (in FieldName2 and FieldName3, respectively). I imagine I can use some logic like:
"If FieldName1 Exists, then Update FieldName2 and FieldName3.
Else, Add FieldName1, FieldName2, and FieldName3."
The issue is that I am very new to VBA/macros/coding in general and have no idea how to execute this, so I am looking for any help, assitance, or pointer fingers in the right direction that someone could provide.
Thanks in advance for any and all insight!