Hello to all VB masters, I have a problem with the search command. I have a button name "btnSearch" a text field name "fldSearch" a table named MSFlexGrid and a database named db.mdb. The function of the search button is to search the data being entered in the fldSearch and display it in the MSFlexGrid.
Example:
If I put the name "Mark" in the fldSearch, then I press the button "btnSearch" the database will search for a name "Mark" and display it in the MSFlexGrid how many "Mark" being registered in the database.
Firstly, make a connection for your database 'db.mdb'
After calling the connection, you have to write VB code for your need:
Write the SQL command statement for searching in the btnSearch Click event as beow:
if fldSearch.Text is the textbox in which you write the string/ any word to search.
sql="Select * FROM Table_Name WHERE Column_Name like '" & Trim(fldSearch.Text) & "%'"
The above SQL will search all the like word written in the search text box.
If you want to write to search only 'Mark' then you rill mody this:
sql="Select * FROM Table_Name WHERE Column_Name ='Mark'"
To send you the complte code, you have to tell me the database name, Table anem and Table structure, and how you connect the database. I hope this will solve your problem.
Private Sub cmdSearch_Click()
On Error GoTo errhan
Set rst = New ADODB.Recordset
With rst
.CursorLocation = adUseClient
.ActiveConnection = Con
.CursorType = adOpenDynamic
.LockType = adLockPessimistic
SQL = "Select * from profile2 where name like '" & Trim(fldSearch.Text) & "'"
End With
Set rst = Nothing
Call dload
errhan:
If Err.Description <> vbNullString Then
MsgBox Err.Description
End If
End Sub
IS THIS CODE FRAGMENT CORRECT? IT SEEMS THE PROGRAM STILL SAYS THE CONNECTION IS CLOSE AND I CAN'T TRACE THE PART WHY IT IS STILL CLOSE.
Last edited by Tekmaven; Aug 16th, 2008 at 7:25 pm. Reason: Code tags
Ok. I think that you have the recordset declared globally and accessing the recordset in the dload procedure to fill the FlexGrid. then the problem with it is;
1. you are not opening the recordset in the cmdSearch_Click() event.
2. you are setting the recordset to nothing the the cmdSearch_Click() at the end which closes the recordset if open.
I really appreciate this help, I will wait. . . thank you so much. Hope you can give me the solution coz it is been 3 days I stuck with this problem. . thank you so much.
Plz chk out the sample project i have given here as attachment. it may help u get some logic.
u need not query the database each time u want to search. fill the flexgrid once when form loads and then use that static data to find the rows with your search values af any required column.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline:logic help
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline:Do while loop