|
-
Dec 4th, 2003, 10:11 AM
#1
Thread Starter
New Member
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
-
Dec 4th, 2003, 10:12 PM
#2
Junior Member
Assuming you have set up your connection, data adapter, and dataset already...
There may be a better way to do this..
VB Code:
Dim ds As New dataSet()
Dim aName, bName, cName, dName As String
Dim r, i As Integer
aName = Me.txtName.Text
Me.OleDbConnection1.Open()
ds.Clear()
Me.OleDbDataAdapter1.Fill(ds.Tables("yourTableName"))
r = ds.Tables("yourTableName").Rows.Count()
For i = 0 To r - 1 'You subtract 1 from the count because the last record in a table always blank
bName = ds.Tables("yourTableName").Rows(i).Item("personsName") 'personsName = the name of the column that the name is stored in
cName = ds.Tables("yourTableName").Rows(i).Item("city")
dName = ds.Tables("yourTableName").Rows(i).Item("state")
If aName = bName Then
Me.Label1.Text = dName
Me.Label2.Text = cName
Exit For
End If
Next
Me.OleDbConnection1.Close()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|