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 [I]yourDatasource[/I]=[I]yourDatabaseName[/I].Openrecordset("select * from [[I]yourTablename[/I]] where [I]yourFieldnameTobeSearch[/I]='" & 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?

hi, tscpb am using the connection and its loading data to the datagrid quite ok.My problem is- how do i implement searchng as i stated earlier?

Hi, am using a connection to load data to the datagrid and this works fine but how do i get to search and sort the data in the datagrid?

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 [I]Tablename1 t1[/I] 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 [I]Tablename1 t1[/I] WHERE t1.Field1 = '" & strText & "'"
     Set RS = strConn.Execute(strSql )

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.