Results 1 to 5 of 5

Thread: loading and passing combobox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    26

    loading and passing combobox

    Hey all. I'm having problems determining how to do this.
    I have a class that loads 42000+ items into a combobox that is used on multiple forms. I want to load this combobox during the time that the splash-screen is showing, and then just reference it from other forms.

    My class code looks something like this:
    Code:
        Public Shared Function LoadZipCodes(ByRef cmbCombo As ComboBox)
            Dim cmbZip As ComboBox
            GenericCode(1, "Zip Code", cmbZip)
            cmbCombo = cmbZip
            Return cmbCombo
        End Function
    I know that it populates the combobox, and I'm pretty sure to put the call to that function in the splashscreen_load sub. What is the best way to pass it to another form, or should I make a global variable that can be used project-wide and just have my comboboxes ref that for the data? I want to try to eliminate processing time as much as possible between switching forms.

    Your help is much appreciated.

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    I would do it as a global, since it is going to be accessed throughout the application.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    26
    Ok, sorry to bother again, but I'm getting the error of:
    "Object reference not set to an instance of an object"
    when I do this. Also where should my call to the function to load the combo box be if I want it to run only once and run while the user can see the SplashScreen form?

    here is my code:

    Module
    Code:
    Public cmbZips As ComboBox
    public sub Main()....
    Splash Screen
    Code:
      Private Sub loadZipCodes()
            Dim clsPrac As clsPractice
            clsPrac.GenericCode(1, "Zip Code", cmbZips)
        End Sub
    clsPractice
    Code:
       Public Shared Sub GenericCode(ByVal intProcess As Integer, ByVal strProcess As String, ByRef cmbBox As ComboBox)
            'intProcess 1 = Load Zip Code
            'intProcess 2 = Load States
            Dim objAdapter As OleDb.OleDbDataAdapter
            Dim objTable As New DataTable
            Dim objItem As ListItemNumeric
            Dim strSQL As String
            Dim strConn As String
            Dim intLoop, records As Integer
            Dim str1, str2, str3 As String
    
            Select Case intProcess
                Case 1 ' load zip codes
                    strSQL = "Select * from Zip, State where Zip.State = StateAbb"
                    str1 = "num"
                    str2 = "Zip"
                Case 2 ' load states
                    strSQL = "Select StateID, StateAbb, StateName From State"
                    str1 = "StateID"
                    str2 = "StateName"
                Case 3 'load tech
                    strSQL = "Select * from Tech, Inventory, Company where inv_tech_num = tech_num and tech_comp_num = company_num"
                    str1 = "Tech.num"
                    str2 = "tech_num"
                Case Else
                    MessageBox.Show("There is some kind of error", "ERROR")
            End Select
    
            strConn = ConnectStringBuild()
    
            Try
                objAdapter = New OleDb.OleDbDataAdapter(strSQL, strConn)
                objAdapter.Fill(objTable)
                records = objTable.Rows.Count - 1
                If records > 43000 Then records = 9
                For intLoop = 0 To records
                    objItem = New ListItemNumeric
                    With objTable.Rows(intLoop)
                        objItem.ItemID = CInt(.Item(str1))
                        objItem.ItemValue = .Item(str2).ToString()
                        If intProcess = 1 Then
                            objItem.City = .Item("City").ToString()
                            objItem.State = .Item("StateName").ToString()
                        ElseIf intProcess = 3 Then
                            objItem.TechCompNum = .Item                        objItem.Email = .Item("tech_email").ToString
                        End If
                    End With
                    cmbBox.Items.Add(objItem)
                Next
    
                Dim x As Integer
                x = CInt(objTable.Rows(1).Item(str1))
    
            Catch objException As Exception
                MessageBox.Show(objException.Message, strProcess)
            End Try
        End Sub
    once again, your help is appreciated. and I'm unsure of how to instantiate the comboBox so I avoid getting that error. Thanks.

  4. #4
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    declare it as Public Shared

    and u can reference it easily from other forms
    Regards

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Public Class Practice
    2.  
    3.     Public Enum LoadType
    4.         ZipCode
    5.         State
    6.         Tech
    7.     End Enum
    8.  
    9.     'proper syntax to use:
    10.     'Practice.GenericCode(LoadType.State, ComboBox1)
    11.     Public Shared Sub GenericCode(ByVal loadTypes As LoadType, ByRef cmbBox As ComboBox)
    12.         Dim objAdapter As OleDb.OleDbDataAdapter
    13.         Dim objTable As New DataTable
    14.         Dim objItem As ListItemNumeric
    15.         Dim strSQL As String
    16.         Dim strConn As String
    17.         Dim intLoop, records As Integer
    18.         Dim str1, str2, str3 As String
    19.  
    20.         Select Case loadTypes
    21.             Case LoadType.ZipCode
    22.                 strSQL = "Select * from Zip, State where Zip.State = StateAbb"
    23.                 str1 = "num"
    24.                 str2 = "Zip"
    25.             Case LoadType.State
    26.                 strSQL = "Select StateID, StateAbb, StateName From State"
    27.                 str1 = "StateID"
    28.                 str2 = "StateName"
    29.             Case LoadType.Tech
    30.                 strSQL = "Select * from Tech, Inventory, Company where inv_tech_num = tech_num and tech_comp_num = company_num"
    31.                 str1 = "Tech.num"
    32.                 str2 = "tech_num"
    33.             Case Else
    34.                 MessageBox.Show("There is some kind of error", "ERROR")
    35.         End Select
    36.  
    37.         'Shared methods can't call instance methods so I hope this is another shared method
    38.         strConn = ConnectStringBuild()
    39.  
    40.         Try
    41.             objAdapter = New OleDb.OleDbDataAdapter(strSQL, strConn)
    42.             objAdapter.Fill(objTable)
    43.             records = objTable.Rows.Count - 1
    44.             If records > 43000 Then records = 9
    45.             For intLoop = 0 To records
    46.                 objItem = New ListItemNumeric
    47.                 With objTable.Rows(intLoop)
    48.                     objItem.ItemID = CInt(.Item(str1))
    49.                     objItem.ItemValue = .Item(str2).ToString()
    50.                     If loadTypes = LoadType.ZipCode Then
    51.                         objItem.City = .Item("City").ToString()
    52.                         objItem.State = .Item("StateName").ToString()
    53.                     ElseIf LoadType.Tech Then
    54.                         objItem.TechCompNum = .Item 'missing fieldname
    55.                         objItem.Email = .Item("tech_email").ToString
    56.                     End If
    57.                 End With
    58.                 cmbBox.Items.Add(objItem)
    59.             Next
    60.  
    61.             Dim x As Integer = CType(objTable.Rows(1).Item(str1), Integer)
    62.  
    63.         Catch objException As Exception
    64.             MessageBox.Show(objException.Message, loadTypes.ToString)
    65.         End Try
    66.     End Sub
    67.  
    68. End Class

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