Enable/disable Button depending on recordcount (1 Viewer)

z0001130

New member
Local time
Today, 09:12
Joined
May 16, 2011
Messages
8
Hi,

I has the On Open action for my Form set to:

Private Sub Form_Open(Cancel As Integer)
If Me.Recordset.RecordCount < 1 Then
Me.Command33.Enabled = False
Else
Me.Command33.Enabled = True
End If
End Sub

which works fine, unless I delete all the records shown on the form. Why, because the button sends and email based on the data on the form and it cannot be allowed to send an empty email. I can add code to the on_click action that checks and displays a msgbox if there are no records but that looked a bit untidy, far better to disable the button if the are no records.
So I added and after_update event similar to the code shown above.
All works fine as I said unless I delete all the records (the button does disable) and then when I click on my close button :

Private Sub Close_Button_Click()
On Error GoTo Err_Close_Button_Click

DoCmd.Close
DoCmd.OpenForm "Master"
Exit_Close_Button_Click:
Exit Sub
Err_Close_Button_Click:
MsgBox Err.Description
Resume Exit_Close_Button_Click

End Sub

I get a 'No Current Record' error box, when I hit OK the form closes. If I remove the On_Open event it doesn't error when closing.

Where am I going wrong.
Thanks,
John
 

stopher

AWF VIP
Local time
Today, 17:12
Joined
Feb 1, 2006
Messages
2,396
I think the code only needs to go in the On Current event of the form. Then it is not needed in the On Open or After Update events.

hth
Chris
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 17:12
Joined
Sep 12, 2006
Messages
13,892
but all you need to do is error trap the email button anyway

Code:
sub email_button_click
dim id as long [COLOR="Red"] 'used to detect an error below[/COLOR]

on error goto fail

[COLOR="Red"]'if there is no current record this next line will error
'it doesn't have to be this - if you are call the email function with an argument , you don't need this. 'you just need something that will detect a blank form and not call the email [/COLOR]

id = recordid  
 
send email
exit sub

fail:
msgbox("no active record", & vbcrlf & vbcrlf & _
"error: " & err & "  Desc: " & err.description)
[COLOR="red"]'I always like to show the error code[/COLOR]
end sub
 

z0001130

New member
Local time
Today, 09:12
Joined
May 16, 2011
Messages
8
I think the code only needs to go in the On Current event of the form. Then it is not needed in the On Open or After Update events.

hth
Chris
Thanks for that. Whilst this wasn't the soluction, it still errored on closing the form, it was the start of the solution. It made me rethink the logic for enabling the button.

I have now disabled the button by default, and if there are any records I activate it, rather than the other way round.

Thanks again,
John
 

z0001130

New member
Local time
Today, 09:12
Joined
May 16, 2011
Messages
8
but all you need to do is error trap the email button anyway

Code:
sub email_button_click
dim id as long [COLOR=red]'used to detect an error below[/COLOR]
 
on error goto fail
 
[COLOR=red]'if there is no current record this next line will error[/COLOR]
[COLOR=red]'it doesn't have to be this - if you are call the email function with an argument , you don't need this. 'you just need something that will detect a blank form and not call the email [/COLOR]
 
id = recordid  
 
send email
exit sub
 
fail:
msgbox("no active record", & vbcrlf & vbcrlf & _
"error: " & err & "  Desc: " & err.description)
[COLOR=red]'I always like to show the error code[/COLOR]
end sub
Thanks for the code, I had already done code to error trap the button, but I prefered to grey out button if there were no records.

Thanks
John
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom