search access database for particular record

How do I search a Microsoft Access database for a particular record? THe user must enter the book's isbn number for search in a text box, and after searching for particular isbn no show that book's title, etc. Can someone please help? Thanks.
[254 byte] By [tobesuccessful] at [2007-11-19 19:54:32]
# 1 Re: search access database for particular record
Dont remember the syntax but it's all over the forum:

build your query

select * from yourTable where book='" And txtbook.Text And ",Price='" and txtPrice.Text

if there can be a situation where there are no parameters - you should edit your query.

When done.
dim conn
dim rs
rs.open conn,query - or something close - I'm one sec from bed - so I'm sorry its not the exact syntax :sick:
giladasaf at 2007-11-9 20:10:56 >
# 2 Re: search access database for particular record
giladasaf is basically correct. But, to be specific:
'Create database connection (adoConnection) and recordset object (adoRecordset)
'Connect to database and set recordset connection, cursor type, and lock type

Dim sqlQuery As String
sqlQuery = "SELECT * FROM TableName WHERE ISBN = '" & txtISBN.Text & "'"

adoRecordset.Open sqlQuery, adoConnection
I assume the field is a text field in the database, due to ISBNs being split with dashes and sometimes ending with an X instead of a number, but if you're storing it as a number, you would need to change sqlQuery to be:
sqlQuery = "SELECT * FROM TableName WHERE ISBN = " & txtISBN.Text
Then, when the query completes (assuming no error), you would need to check to see if a record was returned:
If adoRecordset.BOF And adoRecordset.EOF Then
'No records were returned
End If
Then, when you know you have a result, you can use .Fields("columnname") to reference that column from the record. For information regarding establishing a connection to an Access database with ADO, you can search the forums. There are probably plenty of examples available.
ChaosTheEternal at 2007-11-9 20:12:02 >
# 3 Re: search access database for particular record
How do I get the record info that was found to show in textboxes?
tobesuccessful at 2007-11-9 20:13:05 >
# 4 Re: search access database for particular record
Just use the Value property of the Field.

For example:
With adoRecordset
.Open "SELECT userID, userName FROM Users", adoConnection
If Not (.BOF And .EOF) Then
.MoveFirst 'Not sure if it's necessary, just habit for me
txtUserID.Text = CStr(.Fields("userID").Value)
txtUserName.Text = CStr(.Fields("userName").Value)
End If
.Close
End With
If you don't know the names of the columns being returned (like with a "SELECT * FROM Table"), you can use numbers to reference the columns in the order they occur. More likely, though, you'd use numbers when doing queries like "SELECT Count(*) FROM Table"
ChaosTheEternal at 2007-11-9 20:13:59 >
# 5 Re: search access database for particular record
CStr(.Fields("userID").Value)

Whats the cstr? Is this the name of the recordset?
For example? cstr as adodb.recordset
CStr is a built in VB function that will convert the value passed to a String, if it can.

In that code example, adoRecordset is the name of the recordset object. I just use With adoRecordset so that, while within the With block, I don't need to type adoRecordset when I want to access the properties and methods of the object.

You can use a With block for any control or object (or subobject of a control or object, or subobject of a subobject) to lessen the amount of typing needed.

For example, create a new project, add a command button, then, in Form_Load, type:
With Command1
Then, on the next line, just type a period, and you will have intellisense showing you all the properties and methods of Command1 as if you just typed:
Command1.

If you do use With blocks, though, remember to put an End With before you end the sub or function (best time to do End With is when you aren't accessing the object or control frequently in code).

'Connect to database and set recordset connection, cursor type, and lock type

Can you give me som sample code on how to set cursor type, and lock type?
A small example:
Dim adoConnection As ADODB.Connection
Dim adoRecordset As ADODB.Recordset

'Establish connection object and connect to database
Set adoConnection = New ADODB.Connection
adoConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourAccessDatabasePath.mdb;Persist Security Info=False"
adoConnection.Open

'Establish recordset object, set cursor type, lock type, and the connection
Set adoRecordset = New ADODB.Recordset
adoRecordset.CursorType = ADODB.CursorTypeEnum.adOpenKeyset
adoRecordset.LockType = ADODB.LockTypeEnum.adLockOptimistic
adoRecordset.ActiveConnection = adoConnection.ConnectionString
You will have to change what is in blue to be the location of your Access database.

And if you have any more questions, please ask them in the forum and not in a private message. There are others who can and will help you if you need it.
ChaosTheEternal at 2007-11-9 20:15:04 >