Hey all, My name is Bill and i an a uni student. i am having trouble displaying the result of a search i am trying to put together.After clicking on the search button, my msgbox comes up saying that there are no matching record, but i know there is. the question is as follows:
Implement a search function so you can look up a part number in your table. Your search operation should return the result in a messageBox.
here is my code:
Public MyValue1 As String
Dim Formlength, looking As Integer
Public MyTable As Object
Public MyValue1 As String
Dim Formlength, looking As Integer
Public MyTable As Object
Private Sub Search_Button_Click()
Set MyTable = ActiveDocument.Tables(1)
Formlength = MyTable.Rows.Count
If looking > Formlength Then
'reset looking if not first search
looking = 1
End If
If looking = 0 Then
'initialize looking for the first search
looking = 1
End If
MyValue1 = Search_String_1.Value
'get the search string
If MyValue1 = "" Then
MsgBox ("You have not entered a partnumber to search for!")
Unload UserForm1
Set MyTable = Nothing
Exit Sub
End If
Set MyTable = ActiveDocument.Tables(1)
looking = looking + 1
'increment in case the search function was exited early
Call findit
End Sub
Public Sub findit()
While looking < Formlength + 1
'setup the loop
If MyValue1 = MyTable.Cell(looking, 1) Then
Debug.Print MyTable.Cell(looking, 1)
'if the search string is found in this cell
MyTable.Cell(looking, 1).Select
partnumberprint.Value = MyTable.Cell(looking, 1)
costprint.Value = MyTable.Cell(looking, 2)
quantityprint.Value = MyTable.Cell(looking, 3)
totalprint.Value = MyTable.Cell(looking, 4)
' print result in text box
Exit Sub
End If
looking = looking + 1
Wend
If looking > Formlength Then
MsgBox ("End of records! Your searched record has not been found.")
looking = Formlength
'looking reset so it will be initialized correctly in the next search
Unload UserForm1
End If
End Sub
End Sub
|