Results 1 to 36 of 36

Thread: [RESOLVED] combo1 and list1 help please

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Question [RESOLVED] combo1 and list1 help please

    on a form, i have a combo1 wit a list of items.. what i want to do is select an item from combo1 and a list of items in list 1 based on selection of combo1

    ex:
    combo1 list has: tomatoes, peppers, and so on..

    so if say i selected tomatoes, then in list1 would have a list of tomatoes like: super 100, sweet pop cherry and so on..

    or if i selected peppers, then in list1 would have a list of peppers like: red carolina reaper, yellow carolina reaper, tabasco and so on

    so how do i go about that? cause i dunno what to do in this section:

    Code:
    Private Sub Combo1_Change(Index As Integer)
    
    End Sub
    thank you
    Last edited by elRuffsta; Dec 1st, 2023 at 01:30 PM. Reason: resolved

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,376

    Re: combo1 and list1 help please


  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Another very helpful 'link' is MSDN!!!!!
    Sam I am (as well as Confused at times).

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Quote Originally Posted by jdc2000 View Post
    The exact code needed might depend on where you are getting your list values from.
    the combo1 will obviously have things in it's list.. so when i select an item, it becomes then combo1's text..

    so, for example:

    combo1 has: tomato, cucmber, pepper... so say i pick pepper.. now combo1 text is pepper.. at that point want to add items to list1


    Code:
    Private Sub Combo1_Change(Index As Integer)
    
    if combo1.text = "Peppers" then
    list1.additem ("Arbol")
    list1.additem ("Tabasco")
    list1additem ("Red Carolina Reaper")
    elseif combo1.text = "Tomato" then
    list1.additem ("Roma")
    list1.additem ("Super Sweet 100")
    elseif combo1.text ="Cumcumber" then
    list1.additem ("Diva")
    list1.additem ("Early Prince")
    list1.additem ("Marketmore 86")
    end if
    
    list1.refresh
    
    End Sub

    something like that is what i was thinking.. but i tried it and it didn't work
    Last edited by elRuffsta; Nov 29th, 2023 at 03:41 PM.

  5. #5
    Member
    Join Date
    Aug 2020
    Posts
    47

    Re: combo1 and list1 help please

    Try this

    Code:
    Private Sub Form_Load()
        Combo1.AddItem "Peppers"
        Combo1.AddItem "Tomato"
        Combo1.AddItem "Cumcumber"
    End Sub
    
    Private Sub Combo1_Click()
        List1.Clear
        
        If Combo1.Text = "Peppers" Then
            List1.AddItem ("Arbol")
            List1.AddItem ("Tabasco")
            List1.AddItem ("Red Carolina Reaper")
        ElseIf Combo1.Text = "Tomato" Then
            List1.AddItem ("Roma")
            List1.AddItem ("Super Sweet 100")
        ElseIf Combo1.Text = "Cumcumber" Then
            List1.AddItem ("Diva")
            List1.AddItem ("Early Prince")
            List1.AddItem ("Marketmore 86")
        End If
    End Sub

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Here's an example with a combobox and a listbox:

    Code:
    Option Explicit
    Dim arrayPeppers(10) As String
    Dim arrayTomatoes(10) As String
    Dim arrayCucumbers(10) As String
    
    
    Private Sub Combo1_Click()
        Dim i As Integer
        List1.Clear
        Select Case Combo1.Text
            Case "Peppers"
                For i = 0 To UBound(arrayPeppers)
                    List1.AddItem (arrayPeppers(i))
                Next i
            Case "Tomatoes"
                For i = 0 To UBound(arrayTomatoes)
                    List1.AddItem (arrayTomatoes(i))
                Next i
            Case "Cucumbers"
                For i = 0 To UBound(arrayCucumbers)
                    List1.AddItem (arrayCucumbers(i))
                Next i
        End Select
    End Sub
    
    
    Private Sub Form_Load()
    
        Combo1.AddItem ("Category")
        Combo1.AddItem ("Tomatoes")
        Combo1.AddItem ("Peppers")
        Combo1.AddItem ("Cucumbers")
        Combo1.ListIndex = 0
        loadArrays
    End Sub
    
    
    Private Sub loadArrays()
        arrayPeppers(0) = "Arbol"
        arrayPeppers(1) = "Tabascp"
        arrayPeppers(2) = "Red Carolina Reaper"
        
        arrayTomatoes(0) = "Roma"
        arrayTomatoes(1) = "Super Sweet 100"""
        
        arrayCucumbers(0) = "Diva"
        arrayCucumbers(1) = "Early Prince"
        arrayCucumbers(2) = "Marketmore 86"
    End Sub
    Note: I pre-loaded arrays with your 'data'. But may I suggest you create a database (or even text files) to hold your data. You could then change your data (like add "Cherry" to your Tomatoes data), and you would not have to change your CODE.
    Sam I am (as well as Confused at times).

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Quote Originally Posted by Zann View Post
    Try this

    Code:
    Private Sub Combo1_Click()
        List1.Clear
        
        If Combo1.Text = "Peppers" Then
            List1.AddItem ("Arbol")
            List1.AddItem ("Tabasco")
            List1.AddItem ("Red Carolina Reaper")
        ElseIf Combo1.Text = "Tomato" Then
            List1.AddItem ("Roma")
            List1.AddItem ("Super Sweet 100")
        ElseIf Combo1.Text = "Cumcumber" Then
            List1.AddItem ("Diva")
            List1.AddItem ("Early Prince")
            List1.AddItem ("Marketmore 86")
        End If
    End Sub
    only works for the peppers
    Last edited by elRuffsta; Nov 29th, 2023 at 03:50 PM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    i was thinking that.. but i really dunno how.. well, i did it once years ago for a wordsearch creator/player app.. each search was .map and it would fill in the squares and list and level from selected .map file.. but i have since lost that code.. it's been years. and i haven't played with vb in a very very long time.

    it would work.. just think of how many vegetables there are and how many varities of each.. that's a lot of seperate text files.. and i dunno anything about saving and readin to/from an access DB.. all that would just confuse the hell out of me to the point i would just give up.. i woulodn't mind it being in an access DB, but just too much for me to learn between work and my 2.5yr old granddaughter living with me i just don't have the time honetly to lear all that.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    also..

    Code:
    Private Sub List1_Click()
    Combo2.Text = List1.Text 'Why does the list clear after - i want the list to keep it's current list      ???
    End Sub

  10. #10
    Member
    Join Date
    Aug 2020
    Posts
    47

    Re: combo1 and list1 help please

    This is test project

    New folder.zip

  11. #11
    Addicted Member
    Join Date
    Jul 2021
    Posts
    165

    Re: combo1 and list1 help please

    Your Combo1 appears to be a control array, but it shouldn't be.
    Go to Combo1 properties, and clear the "Index" property.

    Now, suppose you have 3 items in Combo1 in this order: Peppers, Tomatoes, Cucumbers

    Your code can be like this:
    Code:
    Private Sub Combo1_Click()
        Dim a, p, i
        List1.Clear
        a = Array( _
                Array("Arbol", "Tabasco", "Red Carolina Reaper"), _
                Array("Roma", "Super Sweet 100""), _
                Array("Diva", "Early Prince", "Marketmore 86") _
            )
        p = Combo1.ListIndex
        For i = 0 To UBound(a(p))
            List1.AddItem a(p)(i)
        Next
    End Sub

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Quote Originally Posted by Zann View Post
    This is test project

    New folder.zip
    ahh.. i didn't have the: Option Explicit

    but yeah it works now.. now i have to change an image based on selected list item..

    ty for the help

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Zann:

    EDIT: nvm... i figured out out to display the image
    Attached Files Attached Files
    Last edited by elRuffsta; Nov 29th, 2023 at 07:53 PM.

  14. #14
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    You didn't like my post?
    Sam I am (as well as Confused at times).

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    i tried it, but it didn't work for me.. yer example that is..

    but i did like the idea from loading from an access database.. i am currently trying to design one instead of using files and constand code updates.. if yer willing to help me work it out then i will continue working on the database.. i explained this above about all the files it would take.. think of every vegetable and then all the varieties of each.. again, that's alot of files.

    this project is for a seed packet designer.. where i design the front and back of seed packets - which we save and sell the seeds. i am opening a vegetable nursary for my 2.5yr old granddaughter that i want to leave behind for her one day..
    Attached Files Attached Files
    Last edited by elRuffsta; Nov 29th, 2023 at 05:33 PM.

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Sure....I'll hang around and check this thread.
    But what didn't WORK in the example I sent? (Of course, that was using hard-coded arrays at load time rather than bringing in 'outside' data.)
    Sam I am (as well as Confused at times).

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Here is a quick and dirty example using MS Access as the DB

    PS---Add a Reference to MS ActiveX Data Objects X.X Library....I use 2.8, but same say you can get by with 2.6

    Add Reference from main Menu.
    Attached Files Attached Files
    Sam I am (as well as Confused at times).

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    i don't see that in my components.. and i can't open that db.. i can only open ms access 2000 databases and no i don't have the msdn liabrary.. i got my copy of vb6 many years ago through a friend

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    References are from the Main (TOP) menu, they are NOT Components. Once you add that reference, you will not SEE anything different. But once you start coding, you will see properties of various DB related stuff. Hard to explain here, but download, unzip and run my project. Then look at the code.
    Sam I am (as well as Confused at times).

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Code:
    Public Function dbConnection()
        Set cnn = New ADODB.Connection
        With cnn
    '        .Provider = "Microsoft.Jet.OLEDB.4.0" 'this is for access 2000-03   (.mdb files)
            .Provider = "Microsoft.ACE.OLEDB.12.0"  'THIS is for ACCESS 2007   (.accdb files)
            .ConnectionString = "User ID=Admin;password=;" & " Data Source=" & App.Path & "\myDataBase.accdb;"
            .CursorLocation = adUseClient
            .Open 'ERROR.............................................................................................
        End With
    End Function
    provider could not be found or not properly installed

  21. #21
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    That's interesting...I am assuming you UNZIPPED my program to a directory, which would now include the file, myDataBase.accdb.
    Sam I am (as well as Confused at times).

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Quote Originally Posted by SamOscarBrown View Post
    That's interesting...I am assuming you UNZIPPED my program to a directory, which would now include the file, myDataBase.accdb.

    yes i did

    see my new attached updated DB - don't worry about the other stuff.. just vegetable selection and name atm.. so type would go into the combobox and the names in the listbox based on selection
    Attached Files Attached Files
    Last edited by elRuffsta; Nov 29th, 2023 at 09:37 PM.

  23. #23
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Here ya go....

    Code:
    Option Explicit
    
    
    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim cmd As ADODB.Command
    
    
    Public Function dbConnection()
        Set cnn = New ADODB.Connection
        With cnn
            .Provider = "Microsoft.Jet.OLEDB.4.0" 'this is for access 2000-03   (.mdb files)
    '        .Provider = "Microsoft.ACE.OLEDB.12.0"  'THIS is for ACCESS 2007   (.accdb files)
            .ConnectionString = "User ID=Admin;password=;" & " Data Source=" & App.Path & "\nursary.mdb;"
            .CursorLocation = adUseClient
            .Open
        End With
    End Function
    
    
    Private Sub Combo1_Click()
        If Combo1.Text = "FRUITS" Then Exit Sub
        List1.Clear
        Set cmd = New ADODB.Command
        Set cmd.ActiveConnection = cnn
        cmd.CommandText = "select name from table1 where type = '" & Combo1.Text & "'"
        Set rs = cmd.Execute
        Do While Not rs.EOF
            List1.AddItem rs!Name
            rs.MoveNext
        Loop
    End Sub
    
    
    Private Sub Form_Load()
        dbConnection
        Combo1.AddItem "FRUITS"
        Set cmd = New ADODB.Command
        Set cmd.ActiveConnection = cnn
        cmd.CommandText = "select distinct type as dtype from table1 order by type"
        Set rs = cmd.Execute
        Do While Not rs.EOF
            Combo1.AddItem rs!dType
            rs.MoveNext
        Loop
        Combo1.ListIndex = 0
    End Sub
    just make sure you have that Reference added, and have a combobox (combo1) and a listbox (list1) on your form. (you can simply remove all the code in my last project and replace it with this above.)

    Sammi

    PS_and make sure your nursary.mdb file is in the same directory as all your others for the project
    Sam I am (as well as Confused at times).

  24. #24
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Thought I'd make it easy for you....here's my little project:
    Attached Files Attached Files
    Sam I am (as well as Confused at times).

  25. #25
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Just playing around a bit (and modified your field names in your table)....see what you can glean from this ex:
    Attached Files Attached Files
    Sam I am (as well as Confused at times).

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Quote Originally Posted by SamOscarBrown View Post
    Just playing around a bit (and modified your field names in your table)....see what you can glean from this ex:
    impressive.. i'll have to try and play around with this a lil bit.. cause there is going to be an image that changes via imagelist based on item selected as well - ex: if i pick say RED CAROLINA REAPER from the "Pepper" list, then image1 will show a picture of it via imagelist1... but yeah, thank you very much.. i am really liking this! ty again

    EDIT: i really like this! needs some additions, but i "think" i can follow it easily.. the images may be an issue or not.. haven't gotten that far yet. but i can add new stuff to the DB and access it.. this makes things a hell of a lot easier!


    EDIT:

    ok, so, this works on ZANN's code.. but i do not know how to make it work in your code..

    Code:
    Private Sub List1_Click()
        If List1.Text = "Red Carolina Reaper" Then
        Image1.Picture = ImageList1.ListImages(1).Picture
        ElseIf List1.Text = "Cucamelon" Then
        Image1.Picture = ImageList1.ListImages(2).Picture
        End If
    End Sub
    some help please?

    i mean i just tried it with yer code..

    Code:
    [Private Sub List1_Click()
        Set cmd = New ADODB.Command
        Set cmd.ActiveConnection = cnn
        cmd.CommandText = "select latinName, type2, Scoville, seedCount from table1 where typename = '" & List1.Text & "'"
        Set rs = cmd.Execute
        If Not IsNull(rs!latinName) Then txtLatin.Text = rs!latinName
        If Not IsNull(rs!type2) Then txtType2.Text = rs!type2
        If Not IsNull(rs!Scoville) Then txtSHU.Text = rs!Scoville
        If Not IsNull(rs!seedcount) Then txtSeedCount.Text = CStr(rs!seedcount)
        
        If List1.Text = "Red Carolina Reaper" Then
        Image1.Picture = ImageList1.ListImages(1).Picture
        End If
        
    End Sub
    but that's a lot of IF statements.. so if i have over 50 images.. there has to be a better way to call the image based on selected item no?
    Attached Files Attached Files
    Last edited by elRuffsta; Nov 30th, 2023 at 08:26 PM.

  27. #27
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Disregard...look at next post.
    Sam I am (as well as Confused at times).

  28. #28
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Here, look at this:

    SeedPack.zip
    Sam I am (as well as Confused at times).

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Quote Originally Posted by SamOscarBrown View Post
    Here, look at this:

    SeedPack.zip


    that's exactly what i wanted to do... thank you. i'll have to study the code after work. thank you again
    Last edited by elRuffsta; Dec 1st, 2023 at 11:39 AM.

  30. #30
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    No problem...simple stuff. Basically, when you create your images, put them all into the directory called Images. Make sure you name them the same as the "TypeName", example, if TypeName is "Ruby Rows", your image name would be "Ruby Rows.jpg"
    Sam I am (as well as Confused at times).

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    yeah i did that i tried to have another textbox with the type name but it doesn't work.. i looked at the coding and just tried copy and add it in where to the sections that put the text in the textboxes but i get an error on command exe

  32. #32
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    attach your latest...i'll look
    Sam I am (as well as Confused at times).

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    Quote Originally Posted by SamOscarBrown View Post
    attach your latest...i'll look
    there ya go.. ty

    i put notes inside the coding
    Attached Files Attached Files

  34. #34
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: combo1 and list1 help please

    Not sure what your problem is...you correctly added the textbox and populated it correctly. I simply removed all stuff related to seedcount (to include removing that field from the DB table).

    Here's the updated:
    Attached Files Attached Files
    Sam I am (as well as Confused at times).

  35. #35

    Thread Starter
    Hyperactive Member
    Join Date
    May 2014
    Location
    OH
    Posts
    261

    Re: combo1 and list1 help please

    ty

    yeah, i dunno.. you said i did it right but it wouldn't run for me.. oh well.. but thank you again very much i really appreciate the help

  36. #36
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,078

    Re: [RESOLVED] combo1 and list1 help please

    Just look at the latest in post 34...I 'fixed' any errors you had in there, but that textbox wasn't one of them.
    Sam I am (as well as Confused at times).

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