You may have any number of reasons to find a particular recordset in a database. Sometimes finding what you want is easier said than done. This article walks you through the different search functions (also known as Find functions) and shows you when you should use them. This is the seventh part of a thirteen part series that focuses on database forms with HTML.
document.getElementById('RowPosition').value = index + 1;
}
//enable all navigation buttons
enableButtons('SFA1','SP1','SN1','SL1');
}
}
}
}
}
}
When the Find button on the form is clinked, the JavaScript find() function is called. The first thing it does is issue a prompt dialog box. I have explained above what the dialog box takes as input. The line for the code is:
var titleAndItem = prompt("Enter the Title and Item separated by one Space (example shown)", "Name Smith");
The first argument for the prompt function is an instruction to the user on what he has to do. The second is the default value that goes into the input text control. In this case it serves as an example of what the user should type. The prompt() function returns a string made up of the title of a column and a cell value of the column. The returned string is held by the variable, titleAndItem.
The find() function only continues if titleAndItem is not null and is not an empty string. When it continues, it first of all validates the string (titleAndItem) returned by the prompt() function. Well, it does not do a lot, it simply checks to see if the titleAndItem is made up of two words. If it isn't, the error message "You did not type correctly" is issued. Otherwise the find() function uses the two words. The cell value is called the key.
The find() function then goes on to get the column index for the title by calling the getColumnIndex() function. This function takes the title of the column as an argument. If it sees the index, it returns it, which is a number between 0 and 5. If it returns 6, the total number of visible columns, it means the index was not found. In this case the find() function issues the error massage "You did not type the title correctly."
It then goes on to check whether the column for the title is sorted. If the column is sorted, it calls the binarySearch() function, passing the cell value (key) as the first argument and passing the column index as the second argument. If the column is not sorted, it calls the ordinarySearch() function, passing the same arguments as for the binary search. If either of the functions called returns the maximum number of rows in the recordset, it means that the key was not found. In this case the function issues the error message "Item was not found."
If the item was found, that is the index returned is in the range from 0 to (numberOfRows - 1), the find function has three final options:
If the index is the first, that is, 0, the showFirstRow() function is called to display the first record.
If the index is the last, that is, (numberOfRows - 1), the showLastRow() function is called to display the last row.
If the index is between the first and last, the function displays the corresponding record and enables all the navigation buttons.
We shall continue in the next part of the series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.