943,587 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 16th, 2008
0

Using search command in the database. . .help

Expand Post »
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.

PLS HELP ME. . .THANK YOU
REGARDS,
NEIL
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Aug 16th, 2008
-1

Re: Using search command in the database. . .help

hi,


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.
Reputation Points: 10
Solved Threads: 4
Light Poster
K.Vanlalliana is offline Offline
44 posts
since Jul 2008
Aug 16th, 2008
-1

Re: Using search command in the database. . .help

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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Aug 16th, 2008
0

Re: Using search command in the database. . .help

Try this.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub cmdSearch_Click()
  2.  
  3. On Error GoTo errhan
  4.  
  5. Set rst = New ADODB.Recordset
  6.  
  7. rst.CursorLocation = adUseClient
  8. SQL = "Select * from profile2 where name like '" & Trim(fldSearch.Text) & "%'"
  9. rst.Open SQL, con, adOpenKeyset, adLockReadOnly
  10.  
  11. MSFlexGrid1.Clear
  12. While Not rst.EOF
  13. 'replace field1,field2 etc with the fields u want
  14. MSFlexGrid1.addItem rst!field1 & vbTab & rst!field2
  15. rst.MoveNext
  16. Wend
  17.  
  18. Call dload
  19.  
  20. exitPoint:
  21. If Not rst Is Nothing Then
  22. if rst.state then rst.close
  23. Set rst = Nothing
  24. End If
  25. Exit Sub
  26.  
  27. errhan:
  28. MsgBox "Err No : " & Err.Number & vbCrLf & Err.Description
  29. Resume exitPoint
  30.  
  31. End Sub

can u tell me what is this dload procedure?
Last edited by aktharshaik; Aug 16th, 2008 at 5:13 pm. Reason: Not marked the code tags.
Reputation Points: 26
Solved Threads: 40
Posting Whiz
aktharshaik is offline Offline
316 posts
since Aug 2008
Aug 16th, 2008
0

Re: Using search command in the database. . .help

Well the dload is where the database and the MSFlexGrid are being connected. I think I received another penalty from takmaven.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Aug 17th, 2008
0

Re: Using search command in the database. . .help

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.


may be i can help u with this

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub cmdSearch_Click()
  2.  
  3. On Error GoTo errhan
  4.  
  5. 'Close the recordset if open first before searching
  6. If Not rst Is Nothing Then
  7. if rst.state then rst.close
  8. Set rst = Nothing
  9. End If
  10. 'Instantiate again
  11. Set rst = New ADODB.Recordset
  12.  
  13. rst.CursorLocation = adUseClient
  14. SQL = "Select * from profile2 where name like '" & Trim(fldSearch.Text) & "%'"
  15. 'Open the recordset
  16. rst.Open SQL, con, adOpenKeyset, adLockReadOnly
  17.  
  18. 'Now u can call this procedure and get the recordset to work without error
  19. 'because it is not yet closed
  20. Call dload
  21.  
  22. exitPoint:
  23. Exit Sub
  24.  
  25. errhan:
  26. MsgBox "Err No : " & Err.Number & vbCrLf & Err.Description
  27. 'Put the error control code here to close recordset if any error occurs
  28. If Not rst Is Nothing Then
  29. if rst.state then rst.close
  30. Set rst = Nothing
  31. End If
  32. Resume exitPoint
  33.  
  34. End Sub
  35.  
  36.  
  37. Private Sub dlload()
  38.  
  39. MSFlexGrid1.Clear
  40. While Not rst.EOF
  41. 'replace field1,field2 etc with the fields u want
  42. MSFlexGrid1.addItem rst!field1 & vbTab & rst!field2
  43. rst.MoveNext
  44. Wend
  45.  
  46. End Sub

hope it solves ur problem.

Regards
Shaik Akthar
Reputation Points: 26
Solved Threads: 40
Posting Whiz
aktharshaik is offline Offline
316 posts
since Aug 2008
Aug 17th, 2008
0

Re: Using search command in the database. . .help

ok. sorting out the problem. just a few minutes plz.

Regards
Shaik Akthar
Reputation Points: 26
Solved Threads: 40
Posting Whiz
aktharshaik is offline Offline
316 posts
since Aug 2008
Aug 17th, 2008
0

Re: Using search command in the database. . .help

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Aug 17th, 2008
0

Re: Using search command in the database. . .help

Hi,

I guess, You have not Declared a Connection Object.. and you need to open Conn object
before opening the recordset...

Check this out:

vb Syntax (Toggle Plain Text)
  1. 'Declare this Form-Level
  2. Dim Conn As New ADODB.Connection
  3.  
  4. 'In FormLoad open the connection object:
  5. With Conn
  6. .ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source = C:\MyDB.mdb"
  7. .Open
  8. End With


You need to mention Connection object while opening recordset:
vb Syntax (Toggle Plain Text)
  1. With rst
  2. .CursorLocation = adUseClient
  3. .ActiveConnection = Con
  4. .CursorType = adOpenDynamic
  5. .LockType = adLockPessimistic
  6. SQL = "Select * from profile2 where name like '%" & Trim(fldSearch.Text) & "%'"
  7. .Open SQL,Conn
  8. End With


Regards
Veena
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006
Aug 17th, 2008
0

Re: Using search command in the database. . .help

Hi,

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.

Hope this may help u.

Regards
Shaik Akthar
Attached Files
File Type: zip SEARCH.zip (11.8 KB, 96 views)
Reputation Points: 26
Solved Threads: 40
Posting Whiz
aktharshaik is offline Offline
316 posts
since Aug 2008

This thread is solved

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.
Message:
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





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC