Update record with command button

J

James

Hi everyone,

How do you update a specific record by using a command
button? I have the code (posted below) for inserting into
a table, but can I also use the code for updatding. What
changes I need to make inorder for the code to work?

Example: I have a comany name in the company table with a
ID set to 1. In the Company Info table, there is a field
for company address and the table is linked with the ID.
How do you update the record base on the ID?

The code is:
Private Sub cmdSetCompany_Click()

Dim ws As Workspaces
Dim rstComp1 As Recordset

On Error GoTo errHandler

Set ws = DBEngine.Workspaces(0)
Set rstComp1 = CurrentDb.OpenRecordset("Comp1Table",
dbOpenDynaset)

On Error GoTo errTrans

ws.BeginTrans

With rstComp1
.AddNew
!CompanyName = Forms!myform!CompanyName
!CompanyAddress = Forms!myform!CompanyAddress
.Update
End With

ws.CommitTrans

MsgBox "Company added to 1 tables"

EndIt:
Set ws = Nothing
Set rstComp1 = Nothing
Set rstComp2 = Nothing

Exit Sub

errTrans:
MsgBox "Error cmdSetCompany_Click (" & Err.Number & "): "
& Err.Description,
vbCritical
ws.Rollback
Resume EndIt

errHandler:
MsgBox "Error cmdSetCompany_Click (" & Err.Number & "): "
& Err.Description,
vbCritical
Resume EndIt

End Sub


Any help is appreciate

James
 
D

dgoss1

You should be able to use:
(rstComp1 is pointing to a valid record
With rstComp
.Edi
!CompanyName = NewCompanyNam
!CompanyAddress = NewAddres
.Updat
End Wit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top