Click to See Complete Forum and Search --> : Easy question.
Tiger Claw
Jan 13th, 2000, 01:47 AM
I was wondering on how I should do to be able to search a database for a name and then display that persons adress in a textbox.
It shouldn't be that hard to do but since I'm in a hurry (the program is a schoolwork that should be finished soon) I won't have the time to experiment myself since there are other functions that must be programmed.
If anyone could help me by writing down the code I would be grateful.
Please include explanations if needed so that I won't get totally confused and have to spend the entire night trying to figure it out :-)
HarryW
Jan 13th, 2000, 03:54 AM
If you can use SQL it's easier - you can do a simple search using an SQL string that's something like this:
"SELECT address FROM DBName WHERE Name='" & strName & "';"
You should be able to use that to stick the address value in a text box pretty easily. I've only ever used SQL with ASP/VBScript so I'm not sure how you use it with VB. Your help files should tell you how.
If you can't/don't want to use SQL then you can use code like this:
Dim strName strAddress as String
Data1.Recordset.Movefirst
Do until EOF
If Data1.Recordset("Name")=strName then
strAddress=Data1.Recordset("Address")
exit do
Else
Data1.Recordset.Movenext
End If
Loop
Text1.Text=strAddress
I think that should do it. So long as you enter a valid name. If you need anything more specific, post again.
lychew
Jan 13th, 2000, 11:46 AM
this is the sample codes i have done:
Dim rsForum As ADODB.Recordset
Dim strSQL As String
Dim Customer as string
strSQL = "SELECT * FROM tblBoardCat WHERE BCID=" & BCID
Set rsForum = New ADODB.Recordset
rsForum.Open strSQL, DB_CONNECT
Customer=rsForum("Customer")
Set rsForum = Nothing
You'll need to include Microsoft ADO 2.0 or 2.1 in the reference of your project.
FirstKnight
Jan 13th, 2000, 02:24 PM
Here is the simplest way to do it. Create a form with two text boxes and a command button
Then put this code in the command button's click event.
code:
Dim MyDB As DAO.Database
Dim MyRecSet As DAO.Recordset
Set MyDB = opendatabase("Database Path here")
Set MyRecSet = MyDB.OpenRecordset("Table in Database", dbOpenDynaset)
With MyRecSet
If .EOF = True And .BOF = True Then
MsgBox "No records in database!"
Else
.FindFirst ("NameField = '" & Text1.Text & "'")
If .NoMatch Then
MsgBox "Record not found!"
Else
Text2.Text = !AddressField
End If
End If
End With
Now when you click the button it will try to find the name in Text1.
The above example uses DAO 3.6 so you will have to set a reference to it. To do this select project from the menubar and then Rerefrence. Now scroll down till you get to Microsift DAO 3.6 Object Library and check it.
Hope it helps.
------------------
Today is the last day of your past :)
[This message has been edited by FirstKnight (edited 01-14-2000).]
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.