Results 1 to 2 of 2

Thread: searching a database problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    3

    searching a database problem

    So here is my problem

    I have a access database called cust.mdb and I want to design a form that allows the user to input a name in a textbox and then search a database to find the address and state associated with that name and have those displayed in labels right under the textbox. The database would include the name of a person, address, and state.

    My problem is a few things.

    It seems that you need to assign a parameter before filling in the database. How can this be done? I tried it out and got a fill error everytime.

    Also, in order to search the database, do you need to do a do/loop loop to find a match of the textbox to a value in the database? This is what is confusing me.


    Please help.

    Thanks

    Daron

  2. #2
    Junior Member
    Join Date
    Dec 2003
    Location
    California
    Posts
    19
    Assuming you have set up your connection, data adapter, and dataset already...

    There may be a better way to do this..

    VB Code:
    1. Dim ds As New dataSet()
    2.         Dim aName, bName, cName, dName As String
    3.         Dim r, i As Integer
    4.         aName = Me.txtName.Text
    5.         Me.OleDbConnection1.Open()
    6.         ds.Clear()
    7.         Me.OleDbDataAdapter1.Fill(ds.Tables("yourTableName"))
    8.         r = ds.Tables("yourTableName").Rows.Count()
    9.         For i = 0 To r - 1 'You subtract 1 from the count because the last record in a table always blank
    10.             bName = ds.Tables("yourTableName").Rows(i).Item("personsName") 'personsName = the name of the column that the name is stored in
    11.             cName = ds.Tables("yourTableName").Rows(i).Item("city")
    12.             dName = ds.Tables("yourTableName").Rows(i).Item("state")
    13.             If aName = bName Then
    14.                 Me.Label1.Text = dName
    15.                 Me.Label2.Text = cName
    16.                 Exit For
    17.             End If
    18.         Next
    19.         Me.OleDbConnection1.Close()
    20.     End Sub

    Hopefully the names in your database are fairly unique, otherwise the loop will stop once it finds the first match. If this will be a problem for you, then you will need to add functionality to search again, or refine the parameters you search by.

    As for the "parameter and fill" error you mentioned, are you referring to your query or SELECT FROM "tableName" Where "whatever" = "whatever" sql statement prior to filling your dataset?

    Hope this helps.
    Regards -

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