Hi, i got a datagrid that has its gets records from a table in the db.Its workin perfectly well but i want to have a textbox to search and display the records in the datagrid.I guess the change event will come in handy.I really dont know how to go about it.Anyone know how to do it?I greatly appeciate new ideas too.thanx
What is the datasource of your datagrid? try to use a query in your datasource, here try this one
set <em>yourDatasource</em>=<em>yourDatabaseName</em>.Openrecordset("select * from [<em>yourTablename</em>] where <em>yourFieldnameTobeSearch</em>='" & Text1.Text & "')
How are you loading your data in you datagrid? Is the datasourse set in the code, i.e. connection, recordset etc or is your grid linked to an ADODC control?
There is a purpose in what I asked as you have two slightly different ways to approach this. Say for instance you are using the ADODC control as the datasource for the grid:
1. You need to build the connection string via Property Page for the ADODC.
2. After doing the above you need to specify your recordsource. You have 4 command types to choose from - Unknown, Text, Table or StoredProc. Choose 1 - adcmdText
3. Enter a generic "SELECT... FROM TableName(s)..." as your text query. If you do not do this you will not be able to change your query on the fly as you enter a criteria in your text control.
4. If you are using another control, such as a command button to initiate the query, then use the click event, otherwise you could use the LostFocus Event of the textbox itself. The Change event will not work.
5. In this event, set your textbox.text property to a variable and include in a concatenated query that you pass to the ADODC such as below. Also include another variable (strSql) to hold your query string:
Private Sub cmdEnter_Click()
dim strText, strSql as String
On Error Goto ErrHandler:
strText = txtInput.Text
strSql = "SELECT * FROM <em>Tablename1 t1</em> WHERE t1.Field1 = '" & strText & "'"
adoADODC1.RecordSource = strSql
adoADODC1.Refresh
ErrHandler:
End Sub
The same code will work in the LostFocus Event of the Textbox.
Another way is as follows:
1. Programmatically set your connection
2. Declare a variable as your Connection, Recordsource
3. Declare variables for Textbox.Text, query string
strSql = "SELECT MAX(Col) from <em>Tablename1 t1</em> WHERE t1.Field1 = '" & strText & "'"
Set RS = strConn.Execute(strSql )