if there are no records, do nothing. Error 2427

MushroomKing

Registered User.
Local time
Today, 01:27
Joined
Jun 7, 2018
Messages
100
Hello :)
Can anyone explain to me how this works please?
I can't avoid this error and i think its pretty basic.
I'm only a rookie and cant find it on google.


I have a timer event (please dont ask why)
When the field value is "something" it needs to play a sound.
But if there are no records (in my recordsource query), it should just do nothing.

But for some reason, i get the error: 2427 entered an expression that has no value when there are no records available on refresh.

How can i avoid this error?? :banghead::banghead:

Code:
Private Sub Form_Timer()

DoCmd.Requery
Me.Refresh

If perpack.Value = 1 Then
API_PlaySound "C:\database\assets\sound\honk.wav"
Else
End If
End Sub

Sometime theres just no records, so perpack value wont exist. :p
My recordsource is a query btw.
 
Error 2427 means the object referenced does not exist.
You could check whether the file exists and if not exit the sub
Or add error handling to manage that and any other error.
Or do both

Here's one solution

Code:
Private Sub Form_Timer()

On Error GoTo Err_Handler 

DoCmd.Requery
Me.Refresh

If Nz(perpack,0) = 1 Then
     API_PlaySound "C:\database\assets\sound\honk.wav"
Else
End If

Exit_Handler:
   Exit Sub

Err_Handler:
If err=2427 Then 
   Exit Sub
Else
   MsgBox "Error " & err.number & " " & err.description & " in Form_Timer procedure"
End If

End Sub

And...why is this on a timer?
 
Last edited:
To check if a form has records you can if it is both at the beginning and end of file.
if Not (Me.Recordset.BOF And Me.Recordset.EOF) then do something
 
Thanks MajP!!!!! That worked like a charm :)

To answer the question, im refeshing this form to display results on a monitor.

The final code i used is:

Code:
Private Sub Form_Timer()

DoCmd.Requery
Me.Refresh
Text19.SetFocus

If Not (Me.Recordset.BOF And Me.Recordset.EOF) Then
If Me.perpack= 1 Then
API_PlaySound "C:database\assets\sound\alert.wav"
Else
End If
End If
End Sub

It works perfect. Its also right yeah? :)
 
I would still add error handling. See my previous reply.
 

Users who are viewing this thread

Back
Top Bottom