Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Record

Status
Not open for further replies.

unvme

IS-IT--Management
Jan 20, 2003
29
US
I'm having problems with the Find button I created. When I enter a Project # to search for, the error message comes up, even if I enter a valid project #. Any suggestions on what I'm doing wrong, or if there's a better way to handle this situation? Here's my code:

Private Sub cmdFindRecord_Click()
On Error GoTo Err_cmdFindRecord_Click
Dim strLen As Integer
Dim VariableName As String

strLen = Len(VariableName)

DoCmd.GoToControl "txtProjectID"
VariableName = InputBox("Enter the project number to search for.", "Project Search")
DoCmd.FindRecord VariableName, acAnywhere, , acSearchAll, , acCurrent, True

Exit_cmdFindRecord_Click:
Exit Sub

Err_cmdFindRecord_Click:
MsgBox "A project number must be entered."
Resume Exit_cmdFindRecord_Click

End Sub

Thanks!
Ryan
 
Which line is the error occurring at? The code appears to be correct, and it should populate the fields with the record you have searched for.

Jon
 
Below is a procedure I got from another Tek-Tip member.

Notice the Dim strLen, i As Integer is different. There is the letter "i" before As Integer. This might make a difference or not:

Private Sub Command102_Click()


'Search for Specific LastName in the "LastName" field
On Error GoTo Err_Command102_Click
Dim strLen, i As Integer
Dim strTempName, strSearchName As String

strLen = Len(strSearchName)

DoCmd.GoToControl "LastName"
strSearchName = InputBox("Enter any part of the last name.", "Look Up Last Name")
DoCmd.FindRecord strSearchName, acAnywhere, , acSearchAll, , acCurrent, True
DoCmd.RunCommand acCmdFind

Exit_Command102_Click:
Exit Sub

Err_Command102_Click:
[LastName].SetFocus
'MsgBox "Cancelled"
'Resume Exit_Command102_Click

End Sub
 
The letter "i" before the integer doesn't make any difference, but I want to take this opportunity to point out that when declaraing variables, the "shortcut" way illustrated by Dim strLen, i As Integer for example, does not produce what you might expect. Only the last variable in the list is created as the indicated type. The others (strLen in this example) are created as Variant type. This sample code will demonstrate what I mean. (0 = variant type, 2 is integer and 8 is string):
Code:
Sub test()
  Dim strLen, i As Integer
  Dim strTempName, strSearchName As String
  
  Dim CRLF As String
  CRLF = Chr(13) + Chr(10)
  
  MsgBox "Var Types:" & CRLF & CRLF _
       & VarType(strLen) & " -- strLen" & CRLF _
       & VarType(i) & " -- i" & CRLF _
       & VarType(strTempName) & " -- strTempName" & CRLF _
       & VarType(strSearchName) & " -- strSearchName" & CRLF
End Sub
Not that there's anything wrong with using the Variant data type, but if there is heavy computation going on, using the appropriate type (e.g. integer) tends to be more efficient and normally results in faster execution times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top