Connecting Tech Pros Worldwide Forums | Help | Site Map

search button

Newbie
 
Join Date: May 2008
Posts: 1
#1: May 8 '08
i'm new to VB and am hoping for some help.
i want to create a form that has a text box and command button at the top, so that when i enter a number in the textbox it searches a table in the database and then brings up some info from the table.
so for instance: textbox is textbox1, button is cmdFind, table is tbltest

in table test is a list of companies and their details. so when i type 100 in the textbox and hit find, it will bring up the company name, address etc.

i'm guessing this is fairly easy but i cant really find it in a book and so would really like a bit of code that i can copy and then alter to my needs. i think it will be recordset, select, like - etc.
can anyone help please?

lotus18's Avatar
Site Addict
 
Join Date: Nov 2007
Location: Zamboanga City, Philippines
Posts: 858
#2: May 9 '08

re: search button


You can do that by using Where clause in your sql statement.

E.g
Expand|Select|Wrap|Line Numbers
  1. "Select * From <TableName> Where <FieldName>='" & Text1.Text & "'"
It is much better if your field name here is the PK.

Rey Sean
Newbie
 
Join Date: Nov 2006
Location: Delhi
Posts: 10
#3: May 9 '08

re: search button


Hi kardiffBlue,

Hope this code will help u. In this I am using access database. u can change database and table as per ur requirement.


Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String

Private Sub cmdFind_Click()
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Visual Basic\text\company.mdb;Persist Security Info=False"
cn.Open
rs.CursorLocation = adUseClient
sql = "SELECT * FROM company WHERE ID=" & textbox1.Text & ""
rs.Open sql, cn, adOpenForwardOnly, adLockReadOnly
MsgBox "id: " & rs("id")
MsgBox "city: " & rs("city")
MsgBox "state: " & rs("state")
rs.Close
cn.Close
End Sub
Reply


Similar Visual Basic 4 / 5 / 6 bytes