PDA

View Full Version : [Access] How to edit old record and add new record on same command button?



spidermman
11-05-2008, 09:38 AM
Private Sub Save_Click()
?.
?.
Dim strnumber as string
strnumber = Me.Text26.Value

With check ?check is a recordset
.FindFirst ("shipment_no = ' " & strnumber & "'")
If .NoMatch Then
.AddNew
check![shipment_no] = Me.Text26
check![nom to receiver] = Me.Check1
check![nom to terminal] = Me.Check7
.Update

Else
.Edit
check![shipment_no] = Me.Text26
check![nom to receiver] = Me.Check1
check![nom to terminal] = Me.Check7
.Update

End If

End With

End Sub


I have a form which consists of 2 checkboxes for 2 labels (nom to receiver and nom to terminal). From the above code, strnumber is a shipment number which is unique for each shipment. For every new shipment, when I click the Save command button, it will save the values of the 2 checkboxes into a recorsdset (check). However, when I reopen the form to make changes to the checkboxes and click save, it will add duplicate records into the recordset.

From my above code, I would like to edit the record for any previous record when I click the save_button but it didn?t work. Instead it just keep on adding duplicate records.

Anyone knows how to edit previous record and add when the record is new?

thanks

CreganTur
11-05-2008, 12:16 PM
At first blush your code looks correct... but since it's not editing existing records something is obviously going on.

First of all, the FindFirst method requires your recordset to be a Dynaset or Snapshot type recordset- does your recordset match either of these criteria?

Next, I'm assuming that you're working with DAO (Data Access Objects). Try using the .Fields method and see if that gives you better results than the 'Recordset Bang FieldName' method you're currently using (check![nom to terminal])

Dim strnumber as string
strnumber = Me.Text26.Value

With check ‘check is a recordset
.FindFirst ("shipment_no = ' " & strnumber & "'")
If .NoMatch Then
.AddNew
.Fields("shipment_no") = Me.Text26
.Fields("nom to receiver") = Me.Check1
.Fields("nom to terminal") = Me.Check7
.Update

Else
.Edit
.Fields("shipment_no") = Me.Text26
.Fields("nom to receiver") = Me.Check1
.Fields("nom to terminal") = Me.Check7
.Update

End If

End With

spidermman
11-07-2008, 09:57 AM
thanks.
i solved it already.

CreganTur
11-10-2008, 06:38 AM
thanks.
i solved it already.

Mind sharing your solution for the benefit of the forum? Thanks.