Access Database Search

donmc

New member
Joined
Aug 31, 2005
Messages
2
Programming Experience
Beginner
I am new to VB.net and I tiring to create a simple windows form that will search an access database, I would like to have a textbox that the user can enter the search criteria in it and the results would display in a datagrid. This sounded easy but I cannot get it to work or find any examples online.
 
Well look on google for accessing a database using vb.net. To get the data into a datagrid you could use a dataset as the datagrids source.
 
Hey donmc,

You need to change your dataadapter to allow for a parameter. I.E.

I have about 10 search forms, each with a different parameter, but the same form layout.
I use SQL as my database.

In my DataAdapter, I have SELECT field1, field2, field3 FROM table1 WHERE field1 = @field1
( I think Access you use ? instead of @)

On the form, have the datagrid bound to your table.
Next to the textbox I have a button. On the button click event I add the following code;

connection1.open
me.dataAdapter1.selectCommand.parameter("@field1").value = textbox1.text
me.dataAdapter1.fill(dataset1.table1)
connection1.close

You can then accomodate for error handling code, where after the code above you could add;

If me.dataset1.table1.rows.count = 0 Then
messagebox.show("No data found for that criteria!", "Error")
End If


The thing to remember, which caught me out when I first started programming, is that if you have master-child, when you are searching for data, you need to add a parameter to your child table on the ID field. You then add this to the search button click code.

I.E.

Private Sub Button1_Click (blah blah)....

me.connection1.open
me.dataAdapter1.selectcommand.parameters("@ComputerID").value = txtCompID.text
me.dataApapter1.fill(dataset1.table1)
If me.dataset1.table1.rows.count = 0 Then
messagebox.show ("ComputerID entered does not exist!")
me.connection1.close

Else
fillChildData()
End If

End Sub

Private Sub fillChildData()

me.dataAdapter2.selectcommand.parameters("@ComputerID").value = txtCompID.text
me.dataAdapter2.fill(dataset1.table2)
me.connection1.close
End Sub


Hope that helps pointing you in the right direction.
 
Back
Top