3 Replies - 10050 Views - Last Post: 17 August 2010 - 05:18 AM Rate Topic: -----

#1 jerry4prince   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 24-December 09

Binary Search

Posted 13 August 2010 - 02:21 AM

Attached File  Binary Search.zip (11.28K)
Number of downloads: 428I get the error message:
Type mismatch: array or user-defined type expected.
I don't know what to do now. Please assist me.
I will attach the program below, its written with vb6.0. Thanks







Option Explicit

Private Sub cmdAbout_Click()
'
' Show what binary search is all about
'
frmAbout.Show

End Sub

Private Sub cmdAdd_Click()
'
' Add new record to database
'
adoStudentsRecords.Recordset.AddNew
txtRegNO = ""
txtContactAddress = ""
txtStudentName = ""
txtPhoneNO = ""
txtDOB = ""
txtSex = ""
adoStudentsRecords.Recordset.MoveLast

End Sub

Private Sub cmdClear_Click()
txtRegNO = ""
txtContactAddress = ""
txtStudentName = ""
txtPhoneNO = ""
txtDOB = ""
txtSex = ""

End Sub

Private Sub cmdDelete_Click()
'
' To delete a record from database
'
adoStudentsRecords.Recordset.Delete

End Sub

Private Sub cmdExit_Click()
'
' Terminate the Program
'
End

End Sub

Private Sub cmdSearch_Click()
Dim lngMatch As Long
'
' Search the Database for a record
'
lngMatch = BinarySearch(txtNO)
'
' If found, display the record
'
If Match Then
Call adoStudentsRecords.RecordSource
Else
Call ClearTextBoxes
End If

End Sub

Private Function BinarySearch(strItem() As String, strSearch As String) As Long
Dim lngIndex As Long
Dim lngFirst As Long
Dim lngLast As Long
Dim lngMiddle As Long
Dim blnDone As Boolean

lngFirst = LBound(strItem)
lngLast = UBound(strItem)

blnDone = (strItem(lngFirst) > strItem(lngLast))

BinarySearch = lngFirst - 1
Do
'
' Set the pointer to the middle record
'
lngMiddle = (lngFirst + lngLast) \ 2
If strItem(lngMiddle) = strSearch Then
BinarySearch = lngMiddle
Exit Do
ElseIf ((strItem(lngMiddle) < strSearch) Xor blnDone) Then
lngFirst = lngMiddle + 1
Else
lngLast = lngMiddle - 1
End If
Loop Until lngFirst > lngLast

End Function

Private Sub ClearTextBoxes()
'
' Clear the text boxes
'
txtRegNO = ""
txtContactAddress = ""
txtStudentName = ""
txtPhone = ""
txtDOB = ""
txtSex = ""
mgsbox ("Please check the Registration Number")

End Sub

Private Sub Form_Load()
'
' This is to clear the textboxes
'
txtRegNO = ""
txtContactAddress = ""
txtStudentName = ""
txtPhoneNO = ""
txtDOB = ""
txtSex = ""

End Sub

Is This A Good Question/Topic? 0
  • +

Replies To: Binary Search

#2 raziel_   User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,281
  • Joined: 25-March 09

Re: Binary Search

Posted 13 August 2010 - 02:42 AM

lets examen your code
Private Sub cmdSearch_Click()
Dim lngMatch As Long
'
' Search the Database for a record
'
lngMatch = BinarySearch(txtNO)
'
' If found, display the record
'
If Match Then
Call adoStudentsRecords.RecordSource
Else
Call ClearTextBoxes
End If

End Sub

Private Function BinarySearch(strItem() As String, strSearch As String) As Long
Dim lngIndex As Long
Dim lngFirst As Long
Dim lngLast As Long
Dim lngMiddle As Long
Dim blnDone As Boolean

lngFirst = LBound(strItem)
lngLast = UBound(strItem)

blnDone = (strItem(lngFirst) > strItem(lngLast))

BinarySearch = lngFirst - 1
Do
'
' Set the pointer to the middle record
'
lngMiddle = (lngFirst + lngLast) \ 2
If strItem(lngMiddle) = strSearch Then
BinarySearch = lngMiddle
Exit Do
ElseIf ((strItem(lngMiddle) < strSearch) Xor blnDone) Then
lngFirst = lngMiddle + 1
Else
lngLast = lngMiddle - 1
End If
Loop Until lngFirst > lngLast

End Function



so as we see the function BinarySearch() have the following parameters:
strItem() As String witch is an array of string
strSearch As String witch is a string variable

now your calling the function like this:
lngMatch = BinarySearch(txtNO)

first you get the error you show: Type mismatch: array or user-defined type expected.
and the error is because your sending a string to an array. you may change this from strItem() As String to strItem As String

the second error will be that you dont pass all the parameters. as you can see you have 2 variables that must be passed and you pass only one. you may change the second one to Optional if you dont need to send the variable to it all the time :)

good luck dude :)
Was This Post Helpful? 0
  • +
  • -

#3 jerry4prince   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 24-December 09

Re: Binary Search

Posted 17 August 2010 - 04:13 AM

Please i want to leave that array the way it is.
strSearch() As String



Please can you help me on how to call the function. That is the code to use when calling it to operation.
Was This Post Helpful? 0
  • +
  • -

#4 raziel_   User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,281
  • Joined: 25-March 09

Re: Binary Search

Posted 17 August 2010 - 05:18 AM

well then make the variable that you pass to the function array and pass it:
    Dim arrToSend() As String
    ReDim arrToSend(0)
    arrToSend(0) = txtNO.Text
    lngMatch = BinarySearch(arrToSend())



or even better use ParamArray
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1