linking ado's and combo boxes

  • 19 years ago

    I have a small assignment to do for school and it is driving me insane.I have a small database (3tables) and I need to link it to a datagrid (easy enough) but then i need to have a combo box which will allow me to search for a particular value in a field (studentno) and display all the corresponding information from the database in the datagrid, as well as having the feature to add, modify and delete records from the database. how do i do this?
    please help
    Rob

  • 19 years ago

    Well I hope I'm not misunderstanding the question, but this is what I think you can do...


    [The following code is for Access 2000.  In Access 97, you must use version 3.51 of the Jet Engine]


    Dim conn As Connection
    Dim rst As Recordset
    Dim DatabasePath As String


    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")
    DatabasePath = "C:\db1.mdb"


    'Open the connection to your database
    conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=" & DatabasePath
    rst.ActiveConnection = conn


    'Open the recordset using a simple SQL statement
    rst.Open "SELECT [studentno] FROM [TableName]", , adOpenDynamic, adLockOptimistic


    'Loop through the recordset to grab all the student numbers
    Do Until rst.EOF
        cboBox.AddItem rst.Fields("studentno")
        rst.MoveNext
    Loop


    'Now that you have all the student numbers in the combobox, you can use SQL to grab the rest of the data


    rst.Open "SELECT * FROM [TableName] WHERE [studentno]=" & cboBox.Text


    If you have any more questions, don't hesitate to ask.  And if I'm wrong about something, correct me (it's been a while)  =]


    Good Luck

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe