Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Unanswered: Please Help VB 6 ADODB, after adding new record, duplicate values exists in listbox

    Code:
    Private Sub cmdadd_Click()
    rs1.Open "Select * from AccountTypes", con, adOpenDynamic, adLockOptimistic
    rs.Open "Select * from Accounts", con, adOpenDynamic, adLockOptimistic
        rs1.Find "AccountType = '" & Combo1.Text & "'"
        If cmdadd.Caption = "&Add" Then
        txtAccount.Text = ""
        cmdadd.Caption = "&Save"
        txtAccount.SetFocus
        Else
        rs.AddNew
        rs.Fields("AccountTypeID") = rs1.Fields("AccountTypeID")
        rs.Fields("Account") = txtAccount.Text
        rs.Update
        txtAccount.Text = rs.Fields("Account")
        List1.Clear
        Do While rs.EOF <> True
        List1.AddItem rs.Fields("Account")
        rs.MoveNext
        Loop
        cmdadd.Caption = "&Add"
        End If
    
    Set rs = Nothing
    Set rs1 = Nothing
    Last edited by loquin; 01-09-12 at 16:52. Reason: add CODE /CODE tags.

  2. #2
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    I would first create a new button, used to save the code, and move the 'save' code to that button. Have the button's visible state initially set to false.

    During the 'Add' button's code execution, make the Add button invisible, and the save button visible. And, in the save button code, make IT invisible, and the add button visible. Finally, place the new button so that it's behind the existing 'add' button.

    That way, you can keep the logic separate for the two separate tasks.

    Next. If, instead of the recordset FIND method, you instead use the recordset Filter property, it can make your code a bit cleaner, IMO.

    Code:
    rs1.Filter =  "AccountType = '" & Combo1.Text & "'"
    ' Now, the recordset only 'contains' records which match your criteria.
    ' Then, When done, 
    rs1.Filter = adFilterNone
    'restores the recordset.
    Try issuing a listbox.refresh immediately after the listbox.clear.
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Posting Permissions

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