Question [VB 2005] Form-To-Form ComboBox Item Defaulting.

spmaguire

New member
Joined
Jan 18, 2011
Messages
3
Location
USA
Programming Experience
1-3
Hi all,

This is my first post here. I have been creating a call management application and I am having a problem with something that I have spent weeks on trying to get working and nothing I try is working for me.

Let me explain further.... My application is an MDI application written in VB.NET 2005 Professional, using .NET Framework 2.0. It uses a back end database using Microsoft SQL Server 2005.

When the MDI application launches, a form is displayed to the end user asking for phone number (MaskedTextBox) and a partner name. The partner names are within a ComboBox.

When the end user types in the phone number and selects a value from within the ComboBox, s/he clicks on the start button. This then opens a second form. The phone number from the MaskedTextBox is to default in a second MaskedTextBox (this I am able to get working without issue.) The partner name selected from the ComboBox is to default on the second ComboBox. This is the part where I have been working on it for weeks and not able to get it to function.

The second form has many ComboBoxes, all of which pull down items from a particular table within the database. Some of them are editable and some are not. The ones that are editable are also auto complete Suggest/Append on the ListItems.

I wrote a sub to handle the population of these ComboBoxes and also to handle if they are editable or not. All of this is working. However, my main issue is that I can't get the value on the second form to default to what ever was selected on the first form. I do not get any errors but regardless of what I do, the second form always stays as the first item in the list regardless.

The code I am using is as follows:
VB.NET:
    <CLSCompliant(True)> Public Sub PopulateDropDownListFromDB(ByVal cboName As Elegant.Ui.ComboBox, _
                                                               ByVal TableName As String, ByVal ColumnName As String, _
                                                               ByVal UseAutoComplete As Boolean, _
                                                               ByVal UseSQLDistinct As Boolean)

        Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
            'Specify an SQL query.
            Dim sSQL As String = Nothing

            If UseSQLDistinct Then
                'NOTES: frmReportSelection has some dupes from the DB, use DISTINCT in the SQL.
                sSQL = "SELECT DISTINCT " & ColumnName & " FROM " & TableName
            Else
                sSQL = "SELECT " & ColumnName & " FROM " & TableName
            End If

            'Assign the SQL query and database connection to the DataAdapter.
            Dim MyDataAdapter As New SqlDataAdapter(sSQL, SetDatabaseConnection)

            'Create a new instance of a DataSet.
            Dim MyDataSet As New DataSet
            MyDataSet.Locale = CurrentCulture

            Try
                'Clear the DataSet of any previously obtained database information.
                MyDataSet.Clear()
                'Refill the DataAdapter with new information obtained from the database.
                MyDataAdapter.Fill(MyDataSet, TableName)

                With cboName
                    'First check to see if any records.  If no records, add a default item.
                    Dim MyDataTable As DataTable = New DataTable
                    MyDataTable.Locale = CurrentCulture
                    MyDataTable = MyDataSet.Tables(TableName)

                    'Fill the ComboBox.
                    'Assign the database table to the DataSource property.
                    .DataSource = MyDataSet.Tables(TableName)
                    'Assign the database column name to the DisplayMember and ValueMember properties.
                    .DisplayMember = ColumnName
                    .ValueMember = ColumnName

                    If UseAutoComplete = True Then
                        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                        .AutoCompleteSource = AutoCompleteSource.ListItems
                    End If

                    If bNewCallSingle = True Then
                        If .Name Is "cboPartnerName" Then
                            With frmWorkLogEntryStart
                                frmWorkLogEntry.cboPartnerName.SelectedItem = .cboPartnerName.Items(.cboPartnerName.FindStringExact(.cboPartnerName.Text, 0))
                            End With
                        End If
                    End If

                    If MyDataTable.Rows.Count > 0 Then 'Has records.
                        If .Editable = False _
                        AndAlso UseAutoComplete = False Then
                            .SelectedIndex = 0
                        End If
                    Else 'No records in the DataTable.
                        .DataSource = Nothing
                        .Items.Add("[Please Add An Item]")
                        .SelectedIndex = 0
                    End If
                End With
            Catch ex As SqlException
                'Show the end user a message if an error is generated.
                MessageBox.Show("There was an error retrieving data from table name " & TableName & " for the column " & ColumnName & "." & vbNewLine & vbNewLine & _
                                "We are going to prepopulate the dropdown menu with a default value; therefore, you can disregard the error." & vbNewLine & vbNewLine & _
                                "We highly suggest that you click on the plus sign next to the dropdown menu to add some options of your own." & vbNewLine & vbNewLine & _
                                "The reason why you got this error in the first place is:" & vbNewLine & _
                                "No data items in the database " & TableName & " to pull." & vbNewLine & vbNewLine & _
                                "The exception error is:" & vbNewLine & _
                                ex.Message, _
                                "Database Information Retrieval Issues.", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1, _
                                MessageBoxOptions.DefaultDesktopOnly, _
                                False)

                'If no rows are returned to populate the ComboBox, this will return an error, prepopulate with [Please Add An Item]
                With cboName
                    .DataSource = Nothing
                    .Items.Add("[Please Add An Item]")
                    .SelectedIndex = 0
                End With
            End Try
        End Using 'SetDatabaseConnection.
    End Sub

Also, to let you know, when the end user clicks on the start button on the initial screen, a boolean flag called bNewCallSingle is set to true. The following part is what I have tried so far is not making the change. Any suggestions?
VB.NET:
                    If bNewCallSingle = True Then
                        If .Name Is "cboPartnerName" Then
                            With frmWorkLogEntryStart
                                frmWorkLogEntry.cboPartnerName.SelectedItem = .cboPartnerName.Items(.cboPartnerName.FindStringExact(.cboPartnerName.Text, 0))
                            End With
                        End If
                    End If

Screenshots attached for you. Thanks in advance for any guidance you might be able to offer. I was also thinking of saving the selection into My.Settings and then reading it as a default into the second form. I haven't tried that yet.
 

Attachments

  • Untitled.png
    Untitled.png
    167.1 KB · Views: 34
Top Bottom