If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Visual Basic > Please Help VB 6 ADODB, after adding new record, duplicate values exists in listbox

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-14-11, 02:00
buboy buboy is offline
Registered User
 
Join Date: Dec 2011
Posts: 1
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 15:52. Reason: add CODE /CODE tags.
Reply With Quote
  #2 (permalink)  
Old 01-09-12, 18:17
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
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

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On