Consulting

Results 1 to 4 of 4

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

  1. #1

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

    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

  2. #2
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    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])

    [vba]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[/vba]
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  3. #3
    thanks.
    i solved it already.

  4. #4
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Quote Originally Posted by spidermman
    thanks.
    i solved it already.
    Mind sharing your solution for the benefit of the forum? Thanks.
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •