|
-
Jan 9th, 2002, 06:06 AM
#1
Thread Starter
Addicted Member
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?
-
Jan 9th, 2002, 06:19 AM
#2
PowerPoster
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...
-
Jan 9th, 2002, 06:20 AM
#3
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 
-
Jan 9th, 2002, 06:42 AM
#4
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:
Private Sub SearchDatabase()
Dim adoConnection As ADODB.Connection
Dim adoRecordset As ADODB.Recordset
Dim strDataSource As String
Dim strSQL As String
Dim strMessage As String
On Error GoTo ErrorHandler
strDatabaseLocation = "C:\Woof\Dog\Weasel\Birds.MDB"
Set adoConnection = New ADODB.Connection
With adoConnection
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDataSource & " ;Persist Security Info=False"
.Open
strSQL = "SELECT * FROM Birds WHERE Name = 'Henry'"
Set adoRecordset = .Execute(strSQL)
End With
With adoRecordset
If Not .EOF Then
Do While Not .EOF
strMessage = "Bird Details: " & vbCr & vbCr
strMessage = strMessage & "Height: " & .Fields("Height") & vbCr
strMessage = strMessage & "Color: " & .Fields("Color") & vbCr
'etc
MsgBox strMessage, vbCritical, "Details"
.MoveNext
Loop
Else
MsgBox "No birds found called Henry!", vbCritical, "Birds Called Henry"
End If
.Close
End With
Set adoRecordset = Nothing
adoConnection.Close
Set adoConnection = Nothing
Exit Sub
ErrorHandler:
strMessage = "Number: " & Err.Number & vbCr
strMessage = strMessage & "Description: " & Err.Description & vbCr & vbCr
strMessage = strMessage & "Source: " & Err.Source
On Error Resume Next
adoRecordset.Close
Set adoRecordset = Nothing
adoConnection.Close
Set adoConnection = Nothing
MsgBox strMessage, vbCritical, "Error"
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?
-
Jan 9th, 2002, 06:54 AM
#5
Thread Starter
Addicted Member
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???
-
Jan 9th, 2002, 07:02 AM
#6
Thread Starter
Addicted Member
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.
-
Jan 9th, 2002, 07:02 AM
#7
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
-
Jan 9th, 2002, 07:06 AM
#8
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?
-
Jan 9th, 2002, 07:10 AM
#9
Thread Starter
Addicted Member
-
Jan 9th, 2002, 07:36 AM
#10
A rabbit is for dinner, not for Xmas!
I will be on until 5pm, when I finish work 
You could always email it to me at:
[email protected]
Speak to you soon.
-
Jan 9th, 2002, 10:47 AM
#11
Thread Starter
Addicted Member
-
Jan 9th, 2002, 11:04 AM
#12
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?
-
Jan 9th, 2002, 11:22 AM
#13
Thread Starter
Addicted Member
yeah, thanks a lot , i'll e-mail u the code when i'm done ok? Shall i attatch my form?
-
Jan 9th, 2002, 11:40 AM
#14
-
Jan 9th, 2002, 12:18 PM
#15
If you're using Access, compose the query you require and then view it in SQL mode. It'll teach you a great deal...
-
Jan 15th, 2002, 03:27 PM
#16
Thread Starter
Addicted Member
This is the code i've got so far, i'll post a screeny of it now.
VB Code:
Private Sub cmdAdd_Click()
Dim iReply As Integer
'let the user add a new record
'confirm addition first
iReply = MsgBox("Do you want to add a new entry?", vbYesNo + vbQuestion, "Add Entry")
If iReply = vbNo Then
Exit Sub
End If
'disable the add, delete and edit buttons
cmdAdd.Enabled = False
cmdDelete.Enabled = False
cmdEdit.Enabled = False
'enable the cancel and save buttons
cmdCancel.Enabled = True
cmdSave.Enabled = True
'disable the navigation
DisableNavigation
'first unlock the text boxes
UnlockFields
'now add a new record to the table
Adodc1.Recordset.AddNew
'move to the owner text box for data input
txtOwner.SetFocus
End Sub
Private Sub cmdCancel_Click()
Dim iReply As Integer
Dim IRecordPos As Long
'confirm cancel before commiting
iReply = MsgBox("Cancel changes made?", vbYesNo + vbQuestion, "Cancel edits")
If iReply = vbNo Then
Exit Sub
End If
'update the table
Adodc1.Recordset.CancelUpdate
'enable add, delete and edit
cmdAdd.Enabled = True
cmdDelete.Enabled = True
cmdEdit.Enabled = True
'disable the save and cancel buttons, as we've finished with them
cmdSave.Enabled = False
cmdCancel.Enabled = False
'lock the textboxes
LockFields
'enable the navigation
EnableNavigation
'refresh
IRecordPos = Adodc1.Recordset.AbsolutePosition - 1
Adodc1.Refresh
Adodc1.Recordset.Move IRecordPosition, adBookmarkFirst
End Sub
Private Sub cmdDelete_Click()
Dim iReply As Integer
Dim ITitles As Long
'first confirm deletion
iReply = MsgBox("Delete selected entry?", vbYesNo + vbQuestion, "Delete Entry?")
If iReply = vbNo Then
Exit Sub
End If
'if we get here, delete the entry
Adodc1.Recordset.Delete
End Sub
Private Sub cmdEdit_Click()
'Disable the Add, Edit and Delete buttons
cmdAdd.Enabled = False
cmdEdit.Enabled = False
cmdDelete.Enabled = False
'enable save and cancel buttons
cmdSave.Enabled = True
cmdSave.Enabled = True
cmdCancel.Enabled = True
'disable the navigation
DisableNavigation
'unlock the owner, class name, number and entry number txtboxes
UnlockFields
'move to the owner txtbox
txtOwner.SetFocus
End Sub
Private Sub cmdFirst_Click()
'move to the first record in the table
Adodc1.Recordset.MoveFirst
End Sub
Private Sub cmdLast_Click()
'move to the last record
Adodc1.Recordset.MoveLast
End Sub
Private Sub cmdNext_Click()
'check for the end of the file first
If Adodc1.Recordset.EOF = True Then
Adodc1.Recordset.MoveFirst
Else
Adodc1.Recordset.MoveNext
End If
End Sub
Private Sub cmdPrevious_Click()
'check for beginning of the file
If Adodc1.Recordset.BOF = True Then
Adodc1.Recordset.MoveLast
Else
Adodc1.Recordset.MovePrevious
End If
End Sub
Public Sub DisableNavigation()
'to disable the navigation
cmdFirst.Enabled = False
cmdPrevious.Enabled = False
cmdNext.Enabled = False
cmdLast.Enabled = False
End Sub
Public Sub EnableNavigation()
cmdPrevious.Enabled = True
cmdNext.Enabled = True
cmdLast.Enabled = True
cmdFirst.Enabled = True
End Sub
Private Sub cmdSave_Click()
Dim iReply As Integer
'confirm updates before saving
iReply = MsgBox("Save changes made?", vbYesNo + vbQuestion, "save details")
If iReply = vbNo Then
Exit Sub
End If
'update the tables
Adodc1.Recordset.Update
'enable add, delete and edit
cmdAdd.Enabled = True
cmdEdit.Enabled = True
cmdDelete.Enabled = True
'disable cancel and save, cos we've finished with those
cmdCancel.Enabled = flase
cmdSave.Enabled = False
'lock the text boxes
LockFields
'enable navigation
EnableNavigation
End Sub
Private Sub Form_Load()
'disable the save details and cancel edit buttons
cmdSave.Enabled = False
cmdCancel.Enabled = False
LockFields
End Sub
Public Sub LockFields()
'lock all of the fields, so none of the information will be edited by accident
txtOwner.Locked = True
txtClass.Locked = True
txtClassNumber.Locked = True
txtClassName.Locked = True
txtEntryNumber.Locked = True
txtprizes.Locked = True
End Sub
Public Sub UnlockFields()
'unlock the fields
txtOwner.Locked = False
txtClass.Locked = False
txtClassNumber.Locked = False
txtClassName.Locked = False
txtEntryNumber.Locked = False
txtprizes.Locked = False
End Sub
Last edited by rinoaheartilly; Jan 15th, 2002 at 04:06 PM.
-
Jan 15th, 2002, 04:15 PM
#17
Thread Starter
Addicted Member
or maybe not. Anyway, I need something to search for class numbers and entry numbers
-
Jan 16th, 2002, 07:00 AM
#18
Thread Starter
Addicted Member
sorry, the screeny won't load
-
Jan 17th, 2002, 09:29 AM
#19
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|