Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to populate some combo boxes at run time. I have this code in a separate module, there are other forms that call this sub and all work fine.

When I run the code the msg box for the count of combo boxes returns 6 but the loop only runs once??

Any help gratefully received.

VB
Public Sub PopulateCombos(myForm As Form)

        Dim mySQL As String = ""
        Dim myDispMember As String
        Dim myValue As String
        Dim x As Integer = 0



        'Call the SQL.Exceute query - query goes in the the parentheses
        'loop through all the combo boxes

        For Each myCombo As ComboBox In myForm.Controls.OfType(Of ComboBox).ToArray

            'reset the strings
            mySQL = ""
            myDispMember = ""
            myValue = ""

            MsgBox(myForm.Name)
            MsgBox(myCombo.Name)
            MsgBox(myForm.Controls.OfType(Of ComboBox).Count())
            x = x + 1
            MsgBox(x)

            Select Case myCombo.Name.ToString

                Case "cboClient" 'populate client list

                    mySQL = "SELECT id, strSname, strFName FROM tblContacts"
                    myDispMember = "strSName"
                    myValue = "id"

                Case "cboStatus" 'Populate cboStatus

                    mySQL = "SELECT id, strProjStatus FROM tblProjectStatus"
                    myDispMember = "strProjStatus"
                    myValue = "id"

               
            End Select

            SQL.ExecQuery(mySQL)

            If SQL.SQLDS.Tables.Count > 0 Then

                With myCombo

                    .DisplayMember = myDispMember
                    .ValueMember = myValue
                    .DataSource = SQL.SQLDS.Tables(0)
                    .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                    .AutoCompleteSource = AutoCompleteSource.ListItems

                End With

            End If



        Next i

    End Sub
Posted
Updated 20-Jan-15 12:07pm
v3
Comments
PIEBALDconsult 20-Jan-15 18:07pm    
Curious: what is "Next i" ?
Razors68 21-Jan-15 4:03am    
Hello, apologies I did try to change the loop by including for For .. Next Loop with i as the counter, but that didnt work either. the i should be removed the Next is the loop back to the For ... Each loop.
Razors68 21-Jan-15 4:09am    
Hello PiebalDconsult, apologies I did try to change the loop by including for For .. Next Loop with i as the counter, but that didnt work either. the i should be removed the Next is the loop back to the For ... Each loop.
Razors68 21-Jan-15 4:08am    
Thanks --SA, the combo boxes are definately on the form, I have called the count function and this returns 6, whic is the number of combo boxes on the form. I have also called the .parent function, which also returns the correct form name.
The code works fine for other forms too. I have deleted and re-created the combo boxes on the affected form and this seems to have solved the problem. I would still like to know why this error occurred to make sure that I can prevent it from occurring again.

Thanks again, Ray

1 solution

In addition to "Next i" (why?!): you may not have any controls of this type of the form. Such controls are rarely put on a form directly, more typically, they are put as children of some nested panels and other container controls (tab pages, for example). You need to abstract out the loop in a separate function and recursively call it in its implementation.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900