By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

search button

P: 1
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?
May 8 '08 #1
Share this Question
Share on Google+
2 Replies

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

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
May 9 '08 #2

P: 10
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"
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")
End Sub
May 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.