Results 1 to 2 of 2

Thread: [2005] SQL Query combobox.

Threaded View

  1. #1

    Thread Starter
    Member mjbrown20's Avatar
    Join Date
    Nov 2006
    Posts
    33

    Unhappy [2005] SQL Query combobox.

    Hello,

    This really can't be too difficult, but for some reason I am having a hard time.

    I have an Access database that I want to include in my project. It has columns Manufacturer, Model, Description, Price.

    I simply want to use SELECT DISTINCT to populate mfrComboBox.

    And then populate modelCombobox with SELECT from [table] WHERE [manufacturer] LIKE mfrComboBox.selecteditem.

    So far, I have tried databinding, tableadapters, and an sqldataadapter. Not sure where I'm going wrong with those.

    I have done this in Excel with the code below. The only difference now, is that I want to have the database as part of the project, rather than specifying the path to the database. What's really frustrating is that I have already accomplished this, and for some reason can seem to get it in VB2005.

    Any help would be greatly appreciated. Thank you.

    (excel VBA code that accomplishes the same thing)

    VB Code:
    1. Sub Populate()
    2.     Dim CnString As String
    3.     Dim Cn As ADODB.Connection
    4.     Dim Rs As ADODB.Recordset
    5.      
    6.     CnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    7.     "Data Source=C:\Folder\Workbook.xls;Extended Properties=Excel 8.0"
    8.      
    9.     Set Cn = New ADODB.Connection
    10.     Cn.Open CnString
    11.     Set Rs = New ADODB.Recordset
    12.      
    13.     Rs.Open "SELECT DISTINCT [Column] FROM [Sheet] ORDER BY [Column]", _
    14.     Cn, adOpenStatic, adLockOptimistic
    15.      
    16.     With Rs
    17.         Do While Not .EOF
    18.              
    19.             UserForm1.ComboBox1.AddItem Rs!Column
    20.             .MoveNext
    21.              
    22.         Loop
    23.         .Close
    24.     End With
    25. End Sub
    26.  
    27.  
    28. Private Sub ComboBox1_Change()
    29.     Dim CnString As String
    30.     Dim Cn As ADODB.Connection
    31.     Dim Rs As ADODB.Recordset
    32.     Dim CBS As Variant
    33.      
    34.     CBS = UserForm1.ComboBox1.Value
    35.     UserForm1.ComboBox2.Clear
    36.      
    37.     CnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    38.     "Data Source=C:\Folder\Workbook.xls;Extended Properties=Excel 8.0"
    39.      
    40.     Set Cn = New ADODB.Connection
    41.     Cn.Open CnString
    42.     Set Rs = New ADODB.Recordset
    43.      
    44.     Rs.Open "SELECT * FROM [Sheet] WHERE [Column]=" & CBS & _
    45.     "ORDER BY [Column]", Cn, adOpenStatic, adLockOptimistic
    46.      
    47.     With Rs
    48.         Do While Not .EOF
    49.              
    50.             UserForm1.ComboBox2.AddItem Rs!Column
    51.             .MoveNext
    52.              
    53.         Loop
    54.         .Close
    55.          
    56.     End With
    57. End Sub
    Last edited by mjbrown20; Nov 18th, 2006 at 10:27 PM.

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