Results 1 to 4 of 4

Thread: Easy question.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Location
    Åsa, -, Sweden
    Posts
    14

    Post

    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 :-)

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    Post

    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.

  3. #3
    Lively Member
    Join Date
    Aug 1999
    Posts
    89

    Post

    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.

  4. #4
    Lively Member FirstKnight's Avatar
    Join Date
    Jul 1999
    Location
    Johannesburg, South Africa
    Posts
    95

    Post

    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).]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width