Results 1 to 21 of 21

Thread: Adding to Listbox [Resolved]

  1. #1

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40

    Adding to Listbox [Resolved]

    how do I display a set of data taken from a database from a combo box to a list box? Currently I've tried List1.text, List1.additem but the errors stated is that the method don't exist. i.e. (.additem) or that .text is invalid. Any help is appreciated.
    Last edited by esthetiques; Sep 16th, 2004 at 03:23 AM.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    drop a combobox & a listbox on form and paste this code.
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim iCtr As Integer
    3.    
    4.     For iCtr = 1 To 10
    5.         Combo1.AddItem "Combo Item " & iCtr
    6.     Next
    7.    
    8.     For iCtr = 0 To Combo1.ListCount - 1
    9.         List1.AddItem Combo1.List(iCtr)
    10.     Next
    11. End Sub

  3. #3

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    Hi Deepak, thanks for reply so quickly, can you explain what the integer does? I have a combo box that extracts the value from the database. and I would like to list it in the listbox, will the "for" statement work the same?
    Here's a screenshot of my program
    Attached Images Attached Images  

  4. #4
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    hi esthetiques . It seems that u r newser to vb.

    Well, Integer is a DataType. my code adds all items of ComboBox to ListBox.

    I think u want, when user selects an item from ComboBox u want to populate data from database into ListBox. If that is the case then u open connection to database (MS Access/SQL Server), then load data from table into a recordset. Loop through the recordset using For Loop and add items to ListBox.

  5. #5

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    I have my codes and connection. Here is my combo box codes, its just a segment.
    Here my subroutine loops through the database to get the data, but after that, I have problems with populating the listbox.

    Private Sub LoadGroups()

    'Declared variables
    Dim dbIP As New ADODB.Connection
    Dim rsIP As New ADODB.Recordset

    'Make the connection
    dbIP.Open "dsn=NCMSdB"

    'Clear the combo box
    cboGroups.Clear

    'Request the IP addresses with a SQL statement
    'executed by the connection to the database
    Set rsIP = dbIP.Execute("select Groups from IPTBL")

    'Set the global variable to be the first address
    'since that is the one that will be displayed
    intCurrGroups = rsIP("Groups")

    'Loop throught the IP addresses
    Do Until rsIP.EOF

    'Add the IP addresses to the list by showing
    'the group names
    cboGroups.AddItem _
    rsIP("Groups")

    'Move to the next record
    rsIP.MoveNext

    Loop

    'Close the DB
    dbIP.Close

    'Set to the first group
    cboGroups.ListIndex = 0

    End Sub

  6. #6
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    give a shot to this
    VB Code:
    1. Private Sub LoadGroups()
    2.     'Declared variables
    3.     Dim dbIP As New ADODB.Connection
    4.     Dim rsIP As New ADODB.Recordset
    5.    
    6.     'Make the connection
    7.     dbIP.Open "dsn=NCMSdB"
    8.    
    9.     'Clear the combo box
    10.     [B]ListBox1.Clear 'replace ListBox1 with ur ListBox name[/B]
    11.    
    12.     'Request the IP addresses with a SQL statement
    13.     'executed by the connection to the database
    14.     Set rsIP = dbIP.Execute("select Groups from IPTBL")
    15.    
    16.     'Set the global variable to be the first address
    17.     'since that is the one that will be displayed
    18.     intCurrGroups = rsIP("Groups")
    19.    
    20.     'Loop throught the IP addresses
    21.     Do Until rsIP.EOF
    22.         'Add the IP addresses to the list by showing
    23.         'the group names
    24.         [B]ListBox1.AddItem rsIP("Groups") 'replace ListBox1 with ur ListBox name[/B]
    25.    
    26.         'Move to the next record
    27.         rsIP.MoveNext
    28.     Loop
    29.    
    30.     'Close the DB
    31.     dbIP.Close
    32.    
    33.     'Set to the first group
    34.     cboGroups.ListIndex = 0
    35. End Sub

  7. #7

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    I've edited the SQL codes, and also the listbox name as instructed.
    Now there is an error that I keep getting. it states that ".Clear" is not a valid property. Is there a possibilty that I'm missing some reference or component?


    Private Sub cboGroups_Click()

    'Declared variables
    Dim dbIP As New ADODB.Connection
    Dim rsIP As New ADODB.Recordset

    'Make the connection
    dbIP.Open "dsn=NCMSdB"

    'Clear the combo box
    List1.Clear 'replace ListBox1 with ur ListBox name
    'Request the IP addresses with a SQL statement
    'executed by the connection to the database
    Set rsIP = dbIP.Execute("select * from IPTBL Where Groups = 1")

    'Set the global variable to be the first address
    'since that is the one that will be displayed
    intCurrGroups = rsIP("Groups")

    'Loop throught the IP addresses
    Do Until rsIP.EOF
    'Add the IP addresses to the list by showing
    'the group names
    List1.AddItem rsIP("Groups") 'replace ListBox1 with ur ListBox name 'Move to the next record
    rsIP.MoveNext
    Loop
    'Close the DB
    dbIP.Close
    'Set to the first group
    cboGroups.ListIndex = 0
    End Sub

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    it's only available to component drag sources, but it should work.
    looking at Clear for Data Objects...

    is your listbox called List1? change the code to the name of your listbox.
    Last edited by dglienna; Sep 16th, 2004 at 01:32 AM.

  9. #9
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    strange problem. can u post a dummy project here with this suff only so i can see into that.

  10. #10

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    Sure... here's the zipped project
    Attached Files Attached Files

  11. #11
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Talking hehe

    that is a TextBox not ListBox . Remove that TextBox and drop a ListBox on form.

  12. #12

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    reply.... I forgot to change my textbox back to a list box... thanks for the help deepak and dglienna it works ... can't believe i didn't see that in the first place

  13. #13
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    ok then u should add * resolved * to ur post subject.

  14. #14

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    Sorry about this Deepak, now the IP address from the database will show, but it only shows one. and if I select another group number from the combo box it does nothing. How do I resolve this? I've queried the database so that it shows a whole row.
    Also how do I make the data selection dynamic?

    [ set... = dbIP.Execute("select * from IPTBL Where Groups = 1") <--]



    List1.Clear 'replace ListBox1 with ur ListBox name
    'Request the IP addresses with a SQL statement
    'executed by the connection to the database
    Set rsIP = dbIP.Execute("select * from IPTBL Where Groups = 1")

    'Set the global variable to be the first address
    'since that is the one that will be displayed
    intCurrGroups = rsIP("Groups")

    'Loop throught the IP addresses
    Do Until rsIP.EOF
    'Add the IP addresses to the list by showing
    'the group names
    List1.AddItem rsIP("01") 'replace ListBox1 with ur ListBox name 'Move to the next record
    rsIP.MoveNext
    Loop
    'Close the DB
    dbIP.Close

  15. #15
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    formatting posts properly is a goods habit.

    OK we continue..

    set... = dbIP.Execute("select * from IPTBL Where Groups = 1") this should be like this

    Set rsIP = dbIP.Execute("select * from IPTBL Where Groups = " & cboGroups.Text)


    and next

    VB Code:
    1. Set rsIP = dbIP.Execute("select * from IPTBL Where Groups = " & cboGroups.Text)
    2.  
    3. 'Set the global variable to be the first address
    4. 'since that is the one that will be displayed
    5. intCurrGroups = rsIP("Groups")
    6.  
    7. 'Loop throught the IP addresses
    8. Do Until rsIP.EOF
    9.     'Add the IP addresses to the list by showing
    10.     'the group names
    11.     List1.AddItem rsIP("Groups") 'replace ListBox1 with ur ListBox name
    12.     rsIP.MoveNext 'Move to the next record
    13. Loop
    14. [B]
    15. 'Close recordset
    16. rsIP.Close[/B]
    17.  
    18. 'Close the DB
    19. dbIP.Close
    20.     'Set to the first group
    21. cboGroups.ListIndex = 0

  16. #16

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    no... doesn't work.
    also: List1.AddItem rsIP("Groups") extracts the number from the column. so it doesn't show the IP address.

  17. #17
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    List1.AddItem rsIP("01")

  18. #18

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    tried that but it only shows one IP address... the list box is supposed to show a whole row. This may sound silly but is it possible to make it an array or something like that?

    List1.AddItem rsIP("01,02,03,04")

  19. #19
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    VB Code:
    1. Dim iCol As Integer
    2. Dim sData As String
    3.  
    4. Do Until rsIP.EOF
    5.     'loop through the columns and collect data in sData variable
    6.     For iCol = 1 To 48
    7.         sData = sData & rsIP("0" & iCol) & " "
    8.     Next
    9.     List1.AddItem sData
    10.     rsIP.MoveNext
    11. Loop

  20. #20

    Thread Starter
    Member esthetiques's Avatar
    Join Date
    Sep 2004
    Posts
    40
    ok... thanks again deepak

  21. #21
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    u r welcome dude

Posting Permissions

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



Click Here to Expand Forum to Full Width