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!

Error trapping

Status
Not open for further replies.

Vec

IS-IT--Management
Jan 29, 2002
418
US
How do I stop the error "no current record" when I use the search method below. If the user enters a valid search string, the record is displayed no problem, but if they search for a non existant record, there is a visual basic created msgbox "No Current Record". Is there a way to stop this so that nothing happens?

With frmMain!Data1.Recordset
varBookmark = .Bookmark
.Index = "LastName"
.Seek "=", txtSearchLast.Text
frmSearchPop.Hide
Unload frmSearchPop
frmMain.Enabled = True
frmMain!txtFirstName.SetFocus

If .NoMatch Then
.Bookmark = varBookmark
frmSearchPop.Show
End If

End With -
radium.gif

Looking Toward The Future
 
I should note I read and tried the Error Handling thread submitted by Craig, all these methods do is cause 2 msgboxs to appear, first one is "No Current Record" second one is whatever I specify as an error (msgbox etc)

-
radium.gif

Looking Toward The Future
 
You need to trap or handle this to do what you want. If you don't care about the error then try this:
[tt]
With frmMain!Data1.Recordset
On Error Resume Next[color]
varBookmark = .Bookmark
.Index = "LastName"
.Seek "=", txtSearchLast.Text
frmSearchPop.Hide
Unload frmSearchPop
frmMain.Enabled = True
frmMain!txtFirstName.SetFocus

If .NoMatch Then
.Bookmark = varBookmark
frmSearchPop.Show
End If

End With
[/tt] Craig, mailto:sander@cogeco.ca
"Procrastination is the art of keeping up with yesterday."
I hope my post was helpful!!!
 
Craig, I tried that already, it still has a "No Current Record" error.

It is a msgbox that has Microsoft Visual Basic in the title bar, a vbcritical icon, and a message that reads "No Current Record"

Is there a way to suppress pop up msgboxs during that event maybe?

-
radium.gif

Looking Toward The Future
 
Rather than help you supress the error message, though i think you might always see this in design mode. I'll help you avert the error.

Try adding this line
[tt]
If frmMain!Data1.Recordset.RecordCount = 0 then Exit Function
With frmMain!Data1.Recordset
varBookmark = .Bookmark
.Index = "LastName"
.Seek "=", txtSearchLast.Text
frmSearchPop.Hide
Unload frmSearchPop
frmMain.Enabled = True
frmMain!txtFirstName.SetFocus

If .NoMatch Then
.Bookmark = varBookmark
frmSearchPop.Show
End If

End With
[/tt] Craig, mailto:sander@cogeco.ca
"Procrastination is the art of keeping up with yesterday."
I hope my post was helpful!!!
 
Tried adding that line, here is the error it gives:
"Exit Function Not Allowed In Sub Or Property"

Here is the entire code from the click event of the cmdButton in hopes you can better see what may be wrong.

Private Sub cmdInvSearch_Click()
If frmMain!Data1.Recordset.EOF = True Then
frmMain!Data1.Recordset.MoveFirst
End If
If frmMain!Data1.Recordset.BOF = True Then
frmMain!Data1.Recordset.MoveLast
End If

With frmMain!Data1.Recordset
varBookmark = .Bookmark
.Index = "LastName"
.Seek "=", txtSearchLast.Text
frmSearchPop.Hide
Unload frmSearchPop
frmMain.Enabled = True
frmMain!txtFirstName.SetFocus

If .NoMatch Then
.Bookmark = varBookmark
frmSearchPop.Show
End If

End With
End Sub -
radium.gif

Looking Toward The Future
 
Sorry, I should have let you know... what your doing by that line is exiting the function if there is no data to do the search on.

Change it to Exit Sub.

Have you determined what line of code was kicking up the error. You could do a step through the code, to see where it fails.

Instead of clicking the Start Button, or pressing F5 to run the code, click F8, thsi well let you step through the code line by line. Then you just have to see what the last line executed was before the error happened. Another really cool thing you can do while running in this debug mode is to see what values of certain variables are by mousing over them.

For instance if you run in debug mode through F8, and mouse over the word varBookmark from the line:
[tt] varBookmark = .Bookmark[/tt]
You might see that the mouse tip that pops up is <Variable not defined> This will let you know that you forgot to declare a variable.

Hope this helps.

If you want you can send me the code again and I can take a peek.
Craig, mailto:sander@cogeco.ca
&quot;Procrastination is the art of keeping up with yesterday.&quot;
I hope my post was helpful!!!
 
Still gives the &quot;No Current record&quot; Pop up

Can't we use some sort of direct error trap? Like &quot;if error = 455 then&quot;

Thing is I don't know the error #, also shouldn't our code be going in the &quot;If .NoMatch Then&quot; area? As far as I can tell the &quot;exit sub process comes before the search, and all it is really doing is seeing if the db contains records, when in fact we should be trying to supress the error that happens when you searchg and it can't find a record that contains that string. I know exactly what is happening as far as what is causing the error, and that is simply that this search method is telling vb to look for the string exactly as entered in the txt box, if it finds it, then bookmark (display) that record, it then goes on to simply tell it that if it doesn't find it (If .NoMatch Then) to simply stay where it is. The problem is that vb still displays the &quot;No Current Record&quot; error. And not just in design time, it does it when compiled and installed on a machine. If I am misunderstanding what is happening, or you can suggest a better search method please fell free, I am connected to the db using a simple data control from the basic toolbox.

If frmMain!Data1.Recordset.RecordCount = 0 Then Exit Sub

With frmMain!Data1.Recordset
varBookmark = .Bookmark
.Index = &quot;LastName&quot;
.Seek &quot;=&quot;, txtSearchLast.Text
frmSearchPop.Hide
Unload frmSearchPop
frmMain.Enabled = True
frmMain!txtFirstName.SetFocus

If .NoMatch Then
.Bookmark = varBookmark
End If

End With

-
radium.gif

Looking Toward The Future
 
Now that you explained it I can tell you exactly why the error happens. When you search a DB, it goes to the end of the DB, where there is no record. See how I changed your code below. But you really should be adding in error handling, I can help you add in error handling this weekend if you want.
[tt]
Private Sub cmdInvSearch_Click()
Dim varBookMark as Variant
If frmMain!Data1.Recordset.EOF = True Then
frmMain!Data1.Recordset.MoveFirst
End If
If frmMain!Data1.Recordset.BOF = True Then
frmMain!Data1.Recordset.MoveLast
End If
If frmMain!Data1.Recordset.BOF = True And _
frmMain!Data1.Recordset.EOF = True Then
Exit Sub
'If there are no records exit
End If


With frmMain!Data1.Recordset
varBookmark = .Bookmark
.Index = &quot;LastName&quot;
.Seek &quot;=&quot;, txtSearchLast.Text
frmSearchPop.Hide
Unload frmSearchPop
frmMain.Enabled = True
frmMain!txtFirstName.SetFocus

If .NoMatch Then
If varBookmark <> 0 then
.Bookmark = varBookmark
Else
.MoveFirst
End If

frmSearchPop.Show
End If

End With
End Sub Craig, mailto:sander@cogeco.ca
&quot;Procrastination is the art of keeping up with yesterday.&quot;
I hope my post was helpful!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top