Results 1 to 8 of 8

Thread: Easiest way to search access database?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Easiest way to search access database?

    Hey, I need some help with this. Here's what I'm trying to do. I have a table in an access database called "Poolpass". This table has the following columns: "Lastname", "FirstName", etc.

    This what I want to do:

    User selects what they want to search by in a combobox on my form (ie. Lastname, Pass #) Then in a textbox they enter the corresponding information (ie. If they chose to search by Lastname then they would type "Johnson" In the textbox.) When the user clicks the search button, I want to search the column of my table that corresponds to the "Search by" category they selected. When a match is found I want to add all the information from that record to a row in my Listview. Basically, this is a project for me to practice database stuff on. I'm doing this for fun and to help us out at work. What would be the best method to do this by? This is a really basic program so I don't need anything fancy. Here's a look at what the interface looks like below.
    Attached Images Attached Images  

  2. #2
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Something like the following (I didint test but should give you the idea):

    VB Code:
    1. dim rst as new ado.recordset
    2.  
    3. strSQL = "SELECT * FROM PoolPass WHERE " & strSearchField & "=" & strSearchString
    4.  
    5. cnn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=c:\test.mdb;jet oledb:database"
    6.  
    7. rst.Open strSQL ,cnn, adOpenStatic, adLockOptimistic
    8. rst.Update
    9. ' record is now stored in rst
    10. rst.Close
    11. set rst = nothing
    Last edited by Muddy; Nov 5th, 2002 at 08:47 PM.

  3. #3
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    Or try something like this:
    VB Code:
    1. Dim sql as string
    2. sql = "SELECT * FROM [PoolPass] WHERE "
    3.  
    4. Select Case Combo1.Text
    5.     Case "Lastname"
    6.         sql = sql & "[Lastname] = '" & Text1.Text & "'"
    7.     Case "Firstname"
    8.         sql = sql & "[Firstname] = '" & Text1.Text & "'"
    9. End Select
    10. '
    11. ' Now create a recordset using the sql string
    12. '
    13. Set rs = db.OpenRecordset(sql, dbOpenDynaSet)
    Hope this helps

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Having some trouble...

    Ok, I'm a little confused now. Do I not need to connect to the database when my application launches? Also, is SQL the easiest way? Will the search find multiple matches if more then one person has a lastname of say "Johnson"? The code previously posted makes a little sense, but I haven't worked with DB in a long time. Basically, once I have the recordset stored, how can I load each match to the listview? I know how to add items to a listview, but not from a recordset? Can anyone give me any clearer of any example? Any help is extremely appreciated. Thanks

    -Jeremy

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    pnish...

    That code you posted makes perfect sense to me except where you say to create the recordset using the SQL string. How do I do that?

  6. #6
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: pnish...

    Originally posted by hipopony66
    That code you posted makes perfect sense to me except where you say to create the recordset using the SQL string. How do I do that?
    VB Code:
    1. cnn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=c:\test.mdb;jet oledb:database"
    2.  
    3. rst.Open sql ,cnn, adOpenStatic, adLockOptimistic
    4. rst.Update

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Am I getting Warmer?

    VB Code:
    1. Private Sub cmdSearch_Click()
    2.  
    3. Dim sql As String
    4.  
    5. cnn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=c:\poolpass.mdb;jet oledb:database"
    6.  
    7. sql = "SELECT * FROM [PoolPass] WHERE "
    8.  
    9. Select Case cboSearchBy.Text
    10.     Case "Last Name"
    11.         sql = sql & "[Lastname] = '" & txtSearchString.Text & "'"
    12.     Case "First Name"
    13.         sql = sql & "[Firstname] = '" & txtSearchString.Text & "'"
    14.     Case "Address"
    15.         sql = sql & "[Address] = '" & txtSearchString.Text & "'"
    16.     Case "City"
    17.         sql = sql & "[City] = '" & txtSearchString.Text & "'"
    18.     Case "Zip"
    19.         sql = sql & "[Zip] = val(txtSearchString.Text)"
    20.     Case "Phone No."
    21.         sql = sql & "[Phone] = '" & txtSearchString.Text & "'"
    22.     Case "Pass No."
    23.         sql = sql & "[Pass No.] = val(txtSearchString.Text)"
    24. End Select
    25.  
    26. 'Now I need to create recordsets for the matches I got I'm assuming?
    27.  
    28. 'And now I need to add them to the listview

  8. #8
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    VB Code:
    1. Private Sub cmdSearch_Click()
    2.  
    3. Dim sql As String
    4. Dim rst As ADODB.Recordset
    5.  
    6. cnn = "Provider=Microsoft.Jet.OLEDB.4.0; _
    7. Persist Security Info=False;Data Source=c:\poolpass.mdb; _
    8. jet oledb:database"
    9.  
    10. sql = "SELECT * FROM [PoolPass] WHERE "
    11.  
    12. Select Case cboSearchBy.Text
    13.     Case "Last Name"
    14.         sql = sql & "[Lastname] = '" & txtSearchString.Text & "'"
    15.     Case "First Name"
    16.         sql = sql & "[Firstname] = '" & txtSearchString.Text & "'"
    17.     Case "Address"
    18.         sql = sql & "[Address] = '" & txtSearchString.Text & "'"
    19.     Case "City"
    20.         sql = sql & "[City] = '" & txtSearchString.Text & "'"
    21.     Case "Zip"
    22.         sql = sql & "[Zip] = " & val(txtSearchString.Text)
    23.     Case "Phone No."
    24.         sql = sql & "[Phone] = '" & txtSearchString.Text & "'"
    25.     Case "Pass No."
    26.         sql = sql & "[Pass No.] = " & val(txtSearchString.Text)
    27. End Select
    28. '
    29. '    Create the recordset
    30. '
    31. Set rst = New ADODB.RecordSet
    32. rst.Open sql ,cnn, adOpenStatic, adLockOptimistic
    33. '
    34. '    Loop through each record and do whatever
    35. '
    36. Do While Not rst.Eof
    37.     Debug.Print rst![LastName], rst![FirstName]
    38. Loop
    39. '
    40. '    Close the recordset & release its resources
    41. '
    42. rst.Close
    43. Set rst = Nothing
    44.  
    45. End Sub
    This code will return multiple records if there's more than one match. I haven't used Listviews much, so I'll leave that bit to you.

    I've changed the lines above where you used Val(), they were incorrect before.
    Last edited by pnish; Nov 6th, 2002 at 05:12 PM.

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