|
-
Oct 21st, 2000, 10:00 PM
#1
Thread Starter
New Member
hi, I hope someone can help me with my program.
I've written a program to search through a database table and display results according to 3 types of criteria.
For each combination of criteria, i have written a sub function to find matches, store the 1st 5 matches in an array called strname, and sub function to diplay the results. My question is, how do get the diplay result function to work within each sub function for finding matches? I think it has something to do with passing arguments but i don't know how to do it. Any advice would be great. The code is listed below..please igonre the lblsearchresult.caption commands within each criteria searching function as I only put them there to check if the sub function worked.
Thanks very much.
'Blanche Ling, October 2000
'Find the name of a pet in the lost pets database,
'according to criteria set by the program user
'A maximum of of 5 pet names can be diplayed by the
'program
'Ensure VB accepts only processed variables
Option Explicit
'define a global variable
Const IntMaxName As Integer = 5
Private Sub Form1_Load()
'Set default values
TxtType.Text = ""
TxtSize.Text = ""
TxtColour.Text = ""
LblSearch_Result = ""
'declare variables
Public StrType As String 'search criteria for type
Public StrSize As String 'search criteria for size
Public StrColor As String 'search criteria for colour
Public IntIndex As Integer 'index for filling the array
Public IntIndex2 As Integer 'total no. of matching animals found
Public strname(0 To 4) As String 'define array to hold matches
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrType = TxtType.Text
StrSize = TxtSize.Text
StrColor = TxtSize.Text
End Sub
'when only Type criteria have been specified
Private Function FindType()
'declare variables
Dim StrType As String 'search criteria for type
Dim IntIndex As Integer 'index for filling the array
Dim IntIndex2 As Integer 'total no. of matching animals found
Dim strname(0 To 4) As String 'define array to hold matches
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrType = TxtType.Text
'condition if only type criteria have been entered
DatName.Recordset.MoveFirst
Do
If StrType = DatName.Recordset!Type Then 'if criteria matches _
'record type
If IntIndex = IntMaxName Then 'do this if all array spaces have been filled _
to get total no. of animals found
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
Else
strname(IntIndex) = DatName.Recordset!Name
IntIndex = IntIndex + 1
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext 'if match was not made, _
'program moves to next record
End If
Loop Until DatName.Recordset.EOF ' continue until end _
' of database
LblSearchResult.Caption = IntIndex2 & " animals found" & IntIndex
'If both pet type and size have been specified
If IntIndex = 0 Then
LblSearchResult.Caption = "No Animals found"
End If
End Function
Private Sub CmdSearch_Click()
Dim StrType As String 'search criteria for type
Dim StrSize As String 'search criteria for size
Dim StrColor As String 'search criteria for colour
Dim IntIndex As Integer 'index for filling the array
Dim IntIndex2 As Integer 'total no. of matching animals found
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrType = TxtType.Text
StrSize = TxtSize.Text
StrColor = TxtColor.Text
If StrType = "" And StrSize = "" And StrColor = "" Then
LblSearchResult.Caption = "No Search Criteria Entered"
ElseIf StrType <> "" And (StrSize = "" And StrColor = "") Then
FindType
ElseIf StrSize <> "" And (StrType = "" And StrColor = "") Then
FindSize
ElseIf StrColor <> "" And (StrType = "" And StrSize = "") Then
FindColor
ElseIf StrType = "" And (StrSize <> "" And StrColor <> "") Then
FindSizeColor
ElseIf StrColor = "" And (StrType <> "" And StrSize <> "") Then
FindTypeSize
ElseIf StrSize = "" And (StrType <> "" And StrColor <> "") Then
FindTypeColor
ElseIf StrType <> "" And (StrSize <> "" And StrColor <> "") Then
FindTypeSizeColor
End If
End Sub
'when only size criteria have been specified
Private Function FindSize()
Dim StrSize As String 'search criteria for size
Dim IntIndex As Integer 'index for filling the array
Dim IntIndex2 As Integer 'total no. of matching animals found
Dim strname(0 To 4) As String 'define array to hold matches
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrSize = TxtSize.Text
DatName.Recordset.MoveFirst
Do
If StrSize = DatName.Recordset!Size Then
If IntIndex = IntMaxName Then 'do this if all array spaces have been filled _
to get total no. of animals found
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
Else
strname(IntIndex) = DatName.Recordset!Name
IntIndex = IntIndex + 1
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Loop Until DatName.Recordset.EOF
DisplayResult
End Function
'when only color criteria have been specified
Private Function FindColor()
Dim StrColor As String 'search criteria for colour
Dim IntIndex As Integer 'index for filling the array
Dim IntIndex2 As Integer 'total no. of matching animals found
Dim strname(0 To 4) As String 'define array to hold matches
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrColor = TxtColor.Text
DatName.Recordset.MoveFirst
Do
If StrColor = DatName.Recordset!Color Then
If IntIndex = IntMaxName Then 'do this if all array spaces have been filled _
to get total no. of animals found
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
Else
strname(IntIndex) = DatName.Recordset!Name
IntIndex = IntIndex + 1
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Loop Until DatName.Recordset.EOF
LblSearchResult.Caption = "yes" & IntIndex & IntIndex2
End Function
'check for no criteria entered..
Private Function FindCriteria()
Dim StrType As String
Dim StrSize As String
Dim StrColor As String
Dim BlnCriteria As Boolean
StrType = TxtType.Text
StrSize = TxtSize.Text
StrColor = TxtColor.Text
BlnCriteria = True
'check for no criteria entered
If StrType = "" And StrSize = "" And StrColor = "" Then
LblSearchResult.Caption = "No Search Criteria Entered"
BlnCriteria = False
End If
End With
End Function
'check for matches according to size and color criteria
Private Function FindSizeColor()
Dim StrSize As String 'search criteria for size
Dim StrColor As String 'search criteria for colour
Dim IntIndex As Integer 'index for filling the array
Dim IntIndex2 As Integer 'total no. of matching animals found
Dim strname(0 To 4) As String
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrSize = TxtSize.Text
StrColor = TxtColor.Text
DatName.Recordset.MoveFirst
Do
If StrSize = DatName.Recordset!Size Then
If StrColor = DatName.Recordset!Color Then
If IntIndex = IntMaxName Then 'do this if all array spaces have been filled _
to get total no. of animals found
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
Else
strname(IntIndex) = DatName.Recordset!Name
IntIndex = IntIndex + 1
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Loop Until DatName.Recordset.EOF
LblSearchResult.Caption = strname(0)
End Function
'check for matches according to type and color criteria
Private Function FindTypeColor()
Dim StrType As String 'search criteria for size
Dim StrColor As String 'search criteria for colour
Dim IntIndex As Integer 'index for filling the array
Dim IntIndex2 As Integer 'total no. of matching animals found
Dim strname(0 To 4) As String
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrType = TxtType.Text
StrColor = TxtColor.Text
DatName.Recordset.MoveFirst
Do
If StrType = DatName.Recordset!Type Then
If StrColor = DatName.Recordset!Color Then
If IntIndex = IntMaxName Then 'do this if all array spaces have been filled _
to get total no. of animals found
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
Else
strname(IntIndex) = DatName.Recordset!Name
IntIndex = IntIndex + 1
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Loop Until DatName.Recordset.EOF
LblSearchResult.Caption = strname(0)
End Function
'check for matches according to type and size criteria
Private Function FindTypeSize()
Dim StrSize As String 'search criteria for size
Dim StrType As String 'search criteria for colour
Dim IntIndex As Integer 'index for filling the array
Dim IntIndex2 As Integer 'total no. of matching animals found
Dim strname(0 To 4) As String
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrSize = TxtSize.Text
StrType = TxtType.Text
DatName.Recordset.MoveFirst
Do
If StrSize = DatName.Recordset!Size Then
If StrType = DatName.Recordset!Type Then
If IntIndex = IntMaxName Then 'do this if all array spaces have been filled _
to get total no. of animals found
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
Else
strname(IntIndex) = DatName.Recordset!Name
IntIndex = IntIndex + 1
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Loop Until DatName.Recordset.EOF
LblSearchResult.Caption = strname(0) & strname(1)
End Function
'check for type, size and color criteria
Private Function FindTypeSizeColor()
Dim StrType As String
Dim StrSize As String 'search criteria for size
Dim StrColor As String 'search criteria for colour
Dim IntIndex As Integer 'index for filling the array
Dim IntIndex2 As Integer 'total no. of matching animals found
Dim strname(0 To 4) As String
'Assign values to variables
IntIndex = 0
IntIndex2 = 0
StrType = TxtType.Text
StrSize = TxtSize.Text
StrColor = TxtColor.Text
DatName.Recordset.MoveFirst
Do
If StrSize = DatName.Recordset!Size Then
If StrColor = DatName.Recordset!Color Then
If StrType = DatName.Recordset!Type Then
If IntIndex = IntMaxName Then 'do this if all array spaces have been filled _
to get total no. of animals found
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
Else
strname(IntIndex) = DatName.Recordset!Name
IntIndex = IntIndex + 1
IntIndex2 = IntIndex2 + 1
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Else
DatName.Recordset.MoveNext
End If
Loop Until DatName.Recordset.EOF
LblSearchResult.Caption = strname(0) & strname(1)
End Function
Private Sub DisplayResult()
If IntIndex = 0 Then
LblSearchResult.Caption = "No Animals Match The Criteria"
ElseIf IntIndex = 1 Then
LblSearchResult.Caption = "Found " & strname(0)
ElseIf IntIndex = 2 Then
LblSearchResult.Caption = "Found " & IntIndex & " animals with names " _
& strname(0) & " ," & strname(1)
ElseIf IntIndex = 3 Then
LblSearchResult.Caption = "Found " & IntIndex & " animals with names " _
& strname(0) & " ," & strname(1) & " ," & strname(2)
ElseIf IntIndex = 4 Then
LblSearchResult.Caption = "Found " & IntIndex & " animals with names " _
& strname(0) & ", " & strname(1) & ", " & strname(2) & ", " & strname(3)
ElseIf IntIndex = 5 Then
LblSearchResult.Caption = "Found " & IntIndex2 & " animals with names " _
& strname(0) & ", " & strname(1) & ", " & strname(2) & ", " & strname(3) _
& strname(4) & ", ..."
End If
End Sub
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
|