Using search command in the database. . .help

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 77
Reputation: nagatron is an unknown quantity at this point 
Solved Threads: 0
nagatron nagatron is offline Offline
Junior Poster in Training

Using search command in the database. . .help

 
0
  #1
Aug 16th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 44
Reputation: K.Vanlalliana is an unknown quantity at this point 
Solved Threads: 3
K.Vanlalliana K.Vanlalliana is offline Offline
Light Poster

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

 
0
  #2
Aug 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: nagatron is an unknown quantity at this point 
Solved Threads: 0
nagatron nagatron is offline Offline
Junior Poster in Training

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

 
0
  #3
Aug 16th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

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

 
0
  #4
Aug 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: nagatron is an unknown quantity at this point 
Solved Threads: 0
nagatron nagatron is offline Offline
Junior Poster in Training

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

 
0
  #5
Aug 16th, 2008
Well the dload is where the database and the MSFlexGrid are being connected. I think I received another penalty from takmaven.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

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

 
0
  #6
Aug 17th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

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

 
0
  #7
Aug 17th, 2008
ok. sorting out the problem. just a few minutes plz.

Regards
Shaik Akthar
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: nagatron is an unknown quantity at this point 
Solved Threads: 0
nagatron nagatron is offline Offline
Junior Poster in Training

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

 
0
  #8
Aug 17th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

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

 
0
  #9
Aug 17th, 2008
Hi,

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

Check this out:

  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

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

 
0
  #10
Aug 17th, 2008
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, 32 views)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC