Results 1 to 19 of 19

Thread: Search?

  1. #1

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204

    Question Search?

    I'm designing a program to be used in bird shows. (If u don't kno, don't ask, it's a loooong story )
    The entry information is saved in an access database, and i've linked it up. But now I need to search through the data for certain names/class numbers. How can I do this?



  2. #2
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    Either

    a) SQL

    b) Write some code to loop through each record individually scanning the data for the search string.

    The latter is the more flexible, but more hassle to write.
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking Fly away home my pet bricks...

    Ok, say you wanted to select all the types of birds in the show who are over 17 feet tall. The Select statement would be something like:

    Code:
    SELECT Birds.Type FROM Birds WHERE Birds.Height > 17
    Where Birds is the table which contains details of the birds. Although the above statement would select the same type twice, so if we add the Distinct command into the above statement this will only return one entry for the type of bird, even if there are many birds of that type over 17 feet:

    Code:
    SELECT DISTINCT Birds.Type FROM Birds WHERE Birds.Height > 17
    You could search for all all birds with the name Henry:

    Code:
    SELECT * FROM Birds WHERE Name = 'Henry'
    Does this answer your question, or are you confused, I know I am


  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking Large wobbley grey squirrels on the loose!

    Here's an example of selecting some birds from the database. Obviously I have no idea what your fields or table names are, so I made my own up:

    VB Code:
    1. Private Sub SearchDatabase()
    2. Dim adoConnection       As ADODB.Connection
    3. Dim adoRecordset        As ADODB.Recordset
    4. Dim strDataSource       As String
    5. Dim strSQL              As String
    6. Dim strMessage          As String
    7. On Error GoTo ErrorHandler
    8.     strDatabaseLocation = "C:\Woof\Dog\Weasel\Birds.MDB"
    9.     Set adoConnection = New ADODB.Connection
    10.     With adoConnection
    11.         .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDataSource & " ;Persist Security Info=False"
    12.         .Open
    13.         strSQL = "SELECT * FROM Birds WHERE Name = 'Henry'"
    14.         Set adoRecordset = .Execute(strSQL)
    15.     End With
    16.     With adoRecordset
    17.         If Not .EOF Then
    18.             Do While Not .EOF
    19.                 strMessage = "Bird Details: " & vbCr & vbCr
    20.                 strMessage = strMessage & "Height: " & .Fields("Height") & vbCr
    21.                 strMessage = strMessage & "Color: " & .Fields("Color") & vbCr
    22.                 'etc
    23.                 MsgBox strMessage, vbCritical, "Details"
    24.                 .MoveNext
    25.             Loop
    26.         Else
    27.             MsgBox "No birds found called Henry!", vbCritical, "Birds Called Henry"
    28.         End If
    29.         .Close
    30.     End With
    31.     Set adoRecordset = Nothing
    32.     adoConnection.Close
    33.     Set adoConnection = Nothing
    34.     Exit Sub
    35. ErrorHandler:
    36.     strMessage = "Number: " & Err.Number & vbCr
    37.     strMessage = strMessage & "Description: " & Err.Description & vbCr & vbCr
    38.     strMessage = strMessage & "Source: " & Err.Source
    39. On Error Resume Next
    40.     adoRecordset.Close
    41.     Set adoRecordset = Nothing
    42.     adoConnection.Close
    43.     Set adoConnection = Nothing
    44.     MsgBox strMessage, vbCritical, "Error"
    45. End Sub

    This uses Microsoft ActiveX Data Objects 2.x

    It basically connects to the database, queries it on birds called Henry then displays their details in a msgbox one by one. If no birds are found then a msgbox is display to notify the user.

    Does this make sense?

  5. #5

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204
    SQL??? I'm only a newbie don't forget. I don't think u understand, my fault. I'll upload what it looks like so far, if i can. I need to be able to search through the database. How???



  6. #6

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204
    damn, it won't upload. I'll put it on my home page, gimme a few hours to upload it, lol, i'm using the skool's slow connection.

    It's starting to make sense. The thing is, i missed a lot of work, cos i was ill, so i'm finshing it difficult to grasp some things. But I'm getting there slowly.



  7. #7
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    It's a man, on a bus!!! WOW

    If you post your database here I can see what the structure is then I can write a small bit of code to help you understand how to search the database for exactally what you want.

    A-Level computing is like a bad fortnight in a hot air ballon

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking It's black, white, green and made of sausages!

    When you add a post, like this one, just click on the browse button below this text box and select your MDB database file. That should upload it withy your next post...Nice web site by the way. What are those 2 awards all about?

  9. #9

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204
    tell me about it.

    I'm having major problems here, u should have brought my lappy in. Whenever i try to open the file, the blutty (don't ask) thing crashes The damn file won't upload, to angelfire cos they're having technical problems. I don't get home til 2.15, so i'll hafta post it up then Damn this college, the machines r hopeless I'll post it up later ok? There's too much for me to ty0pe out, besides, i can't remember all of it. Will u b on later?



  10. #10

  11. #11

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204

    Angry

    I've gotta redo it!!! I'm so mad The coll computer network picked up a virus, and it's damaged all of my work!!! I'm P!$$ed off! I can't believe it!!!



  12. #12
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking Off to Sunderland!!! Wowowowowowowo...

    I am going to write a V small database and code that will give you an idea of how to search for things using a database...
    I'll post it here in about 30 minutes. Is that OK?

  13. #13

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204
    yeah, thanks a lot , i'll e-mail u the code when i'm done ok? Shall i attatch my form?



  14. #14
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking Woof says Mr Fox

    Here it is...Just open and run the project file.
    There is a small database there too. I know the database is completely wrong and the stuff is made up, but it will show you how to search it...
    Attached Files Attached Files

  15. #15
    Behemoth
    Guest
    If you're using Access, compose the query you require and then view it in SQL mode. It'll teach you a great deal...

  16. #16

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204
    This is the code i've got so far, i'll post a screeny of it now.
    VB Code:
    1. Private Sub cmdAdd_Click()
    2.     Dim iReply As Integer
    3.     'let the user add a new record
    4.  
    5.     'confirm addition first
    6.     iReply = MsgBox("Do you want to add a new entry?", vbYesNo + vbQuestion, "Add Entry")
    7.     If iReply = vbNo Then
    8.         Exit Sub
    9.     End If
    10.  
    11.     'disable the add, delete and edit buttons
    12.     cmdAdd.Enabled = False
    13.     cmdDelete.Enabled = False
    14.     cmdEdit.Enabled = False
    15.  
    16.     'enable the cancel and save buttons
    17.     cmdCancel.Enabled = True
    18.     cmdSave.Enabled = True
    19.  
    20.     'disable the navigation
    21.     DisableNavigation
    22.  
    23.     'first unlock the text boxes
    24.     UnlockFields
    25.  
    26.     'now add a new record to the table
    27.     Adodc1.Recordset.AddNew
    28.  
    29.     'move to the owner text box for data input
    30.     txtOwner.SetFocus
    31.  
    32. End Sub
    33.  
    34. Private Sub cmdCancel_Click()
    35.     Dim iReply As Integer
    36.     Dim IRecordPos As Long
    37.  
    38.     'confirm cancel before commiting
    39.     iReply = MsgBox("Cancel changes made?", vbYesNo + vbQuestion, "Cancel edits")
    40.     If iReply = vbNo Then
    41.         Exit Sub
    42.     End If
    43.  
    44.     'update the table
    45.     Adodc1.Recordset.CancelUpdate
    46.  
    47.     'enable add, delete and edit
    48.     cmdAdd.Enabled = True
    49.     cmdDelete.Enabled = True
    50.     cmdEdit.Enabled = True
    51.  
    52.     'disable the save and cancel buttons, as we've finished with them
    53.     cmdSave.Enabled = False
    54.     cmdCancel.Enabled = False
    55.    
    56.     'lock the textboxes
    57.     LockFields
    58.    
    59.     'enable the navigation
    60.     EnableNavigation
    61.    
    62.     'refresh
    63.     IRecordPos = Adodc1.Recordset.AbsolutePosition - 1
    64.     Adodc1.Refresh
    65.     Adodc1.Recordset.Move IRecordPosition, adBookmarkFirst
    66.  
    67. End Sub
    68.  
    69. Private Sub cmdDelete_Click()
    70.     Dim iReply As Integer
    71.     Dim ITitles As Long
    72.    
    73.     'first confirm deletion
    74.     iReply = MsgBox("Delete selected entry?", vbYesNo + vbQuestion, "Delete Entry?")
    75.     If iReply = vbNo Then
    76.         Exit Sub
    77.     End If
    78.    
    79.     'if we get here, delete the entry
    80.     Adodc1.Recordset.Delete
    81.  
    82. End Sub
    83.  
    84. Private Sub cmdEdit_Click()
    85.     'Disable the Add, Edit and Delete buttons
    86.     cmdAdd.Enabled = False
    87.     cmdEdit.Enabled = False
    88.     cmdDelete.Enabled = False
    89.    
    90.     'enable save and cancel buttons
    91.     cmdSave.Enabled = True
    92.     cmdSave.Enabled = True
    93.     cmdCancel.Enabled = True
    94.    
    95.     'disable the navigation
    96.     DisableNavigation
    97.    
    98.     'unlock the owner, class name, number and entry number txtboxes
    99.     UnlockFields
    100.    
    101.     'move to the owner txtbox
    102.     txtOwner.SetFocus
    103. End Sub
    104.  
    105. Private Sub cmdFirst_Click()
    106.     'move to the first record in the table
    107.     Adodc1.Recordset.MoveFirst
    108. End Sub
    109.  
    110. Private Sub cmdLast_Click()
    111.     'move to the last record
    112.     Adodc1.Recordset.MoveLast
    113. End Sub
    114.  
    115. Private Sub cmdNext_Click()
    116.     'check for the end of the file first
    117.     If Adodc1.Recordset.EOF = True Then
    118.         Adodc1.Recordset.MoveFirst
    119.     Else
    120.         Adodc1.Recordset.MoveNext
    121.     End If
    122. End Sub
    123.  
    124. Private Sub cmdPrevious_Click()
    125.     'check for beginning of the file
    126.     If Adodc1.Recordset.BOF = True Then
    127.         Adodc1.Recordset.MoveLast
    128.     Else
    129.         Adodc1.Recordset.MovePrevious
    130.     End If
    131. End Sub
    132.  
    133. Public Sub DisableNavigation()
    134.     'to disable the navigation
    135.     cmdFirst.Enabled = False
    136.     cmdPrevious.Enabled = False
    137.     cmdNext.Enabled = False
    138.     cmdLast.Enabled = False
    139. End Sub
    140.  
    141. Public Sub EnableNavigation()
    142.     cmdPrevious.Enabled = True
    143.     cmdNext.Enabled = True
    144.     cmdLast.Enabled = True
    145.     cmdFirst.Enabled = True
    146. End Sub
    147.  
    148. Private Sub cmdSave_Click()
    149.     Dim iReply As Integer
    150.  
    151.     'confirm updates before saving
    152.     iReply = MsgBox("Save changes made?", vbYesNo + vbQuestion, "save details")
    153.     If iReply = vbNo Then
    154.         Exit Sub
    155.     End If
    156.    
    157.     'update the tables
    158.     Adodc1.Recordset.Update
    159.    
    160.     'enable add, delete and edit
    161.     cmdAdd.Enabled = True
    162.     cmdEdit.Enabled = True
    163.     cmdDelete.Enabled = True
    164.    
    165.     'disable cancel and save, cos we've finished with those
    166.     cmdCancel.Enabled = flase
    167.     cmdSave.Enabled = False
    168.    
    169.     'lock the text boxes
    170.     LockFields
    171.     'enable navigation
    172.     EnableNavigation
    173.  
    174. End Sub
    175.  
    176. Private Sub Form_Load()
    177.     'disable the save details and cancel edit buttons
    178.     cmdSave.Enabled = False
    179.     cmdCancel.Enabled = False
    180.     LockFields
    181. End Sub
    182.  
    183. Public Sub LockFields()
    184.     'lock all of the fields, so none of the information will be edited by accident
    185.     txtOwner.Locked = True
    186.     txtClass.Locked = True
    187.     txtClassNumber.Locked = True
    188.     txtClassName.Locked = True
    189.     txtEntryNumber.Locked = True
    190.     txtprizes.Locked = True
    191. End Sub
    192.  
    193. Public Sub UnlockFields()
    194.     'unlock the fields
    195.     txtOwner.Locked = False
    196.     txtClass.Locked = False
    197.     txtClassNumber.Locked = False
    198.     txtClassName.Locked = False
    199.     txtEntryNumber.Locked = False
    200.     txtprizes.Locked = False
    201. End Sub
    Last edited by rinoaheartilly; Jan 15th, 2002 at 04:06 PM.



  17. #17

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204
    or maybe not. Anyway, I need something to search for class numbers and entry numbers



  18. #18

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204
    sorry, the screeny won't load



  19. #19

    Thread Starter
    Addicted Member rinoaheartilly's Avatar
    Join Date
    Oct 2001
    Location
    Cymru
    Posts
    204

    Re: Fly away home my pet bricks...

    Originally posted by Wokawidget
    Ok, say you wanted to select all the types of birds in the show who are over 17 feet tall. The Select statement would be something like:

    Code:
    SELECT Birds.Type FROM Birds WHERE Birds.Height > 17
    Where Birds is the table which contains details of the birds. Although the above statement would select the same type twice, so if we add the Distinct command into the above statement this will only return one entry for the type of bird, even if there are many birds of that type over 17 feet:

    Code:
    SELECT DISTINCT Birds.Type FROM Birds WHERE Birds.Height > 17
    You could search for all all birds with the name Henry:

    Code:
    SELECT * FROM Birds WHERE Name = 'Henry'
    Does this answer your question, or are you confused, I know I am

    Yup, I'm confused too, your pet bricks???



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