Command button error (1 Viewer)

FlagDay1987

New member
Local time
Today, 20:54
Joined
May 27, 2020
Messages
20
Hello,

I have also posted this in Command button error (accessforums.net)

(I've attached the database that I'm talking about below)

I have added a command button to the form ‘frm_PatientPlan’ called ‘Disable/Enable Invoice Code’, which works on a field called ‘Invoice Code’ that is on its subform ‘frm_PatientSchAll_subform’. It seems to work ok when I open up ‘frm_PatientPlan’ and click the button.

1629360447521.png


However, in practice the user will start with the form ‘frm_PatientList’, click on the Patient’s ID which opens up the ‘frm_PatientPlan’ for that patient. But when the ‘Disable/Enable Invoice Code’ is clicked I get the error.

1629360511207.png


1629360668359.png


Could anyone help please?

Thankyou.
 

Attachments

  • Option2_10 - Master.zip
    1,007.5 KB · Views: 274
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 20:54
Joined
Feb 19, 2013
Messages
16,553
Not at my computer right now - when you hit debug what line of code is highlighted?
 

FlagDay1987

New member
Local time
Today, 20:54
Joined
May 27, 2020
Messages
20
Hi there,

It is :

If Forms(frm_PatientPlan).frm_PatientSchAll_subform.Form.InvoiceCode.Enabled Then

1629367491850.png



Thankyou!
 

Minty

AWF VIP
Local time
Today, 20:54
Joined
Jul 26, 2013
Messages
10,355
Add option explicit to the top of all your code modules.
Then compile it.

Your code should be something like
Code:
Private Sub Command252_Click()
    If Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled Then
        Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled = False
    Else
        Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled = True
    End If
End Sub

However you can shorten it to

Code:
Private Sub Command252_Click()

        Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled = Not Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled
        
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:54
Joined
May 7, 2009
Messages
19,169
make it shorter, it will always point on same subform?

Code:
Private Sub Command252_Click()
    dim ctl As Control
    Set ctl = Forms("frm_PatientPlan")!frm_PatientSchAll_subform.Form!InvoiceCode
    ctl.Enabled = Not ctl.Enabled
End Sub
 

Minty

AWF VIP
Local time
Today, 20:54
Joined
Jul 26, 2013
Messages
10,355
Hmm @arnelgp is 20 characters shorter still.

I don't like exclamation marks though, that's my excuse.
 

FlagDay1987

New member
Local time
Today, 20:54
Joined
May 27, 2020
Messages
20
Thankyou Minty,

That worked. I don't understand why though if I'm honest. What does option explicit do?
Add option explicit to the top of all your code modules.
Then compile it.

Your code should be something like
Code:
Private Sub Command252_Click()
    If Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled Then
        Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled = False
    Else
        Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled = True
    End If
End Sub

However you can shorten it to

Code:
Private Sub Command252_Click()

        Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled = Not Forms("frm_PatientPlan").frm_PatientSchAll_subform.Form.InvoiceCode.Enabled
       
End Sub
 

FlagDay1987

New member
Local time
Today, 20:54
Joined
May 27, 2020
Messages
20
make it shorter, it will always point on same subform?

Code:
Private Sub Command252_Click()
    dim ctl As Control
    Set ctl = Forms("frm_PatientPlan")!frm_PatientSchAll_subform.Form!InvoiceCode
    ctl.Enabled = Not ctl.Enabled
End Sub
Thank you arnelgp,

It's good to see two different methods!
 

FlagDay1987

New member
Local time
Today, 20:54
Joined
May 27, 2020
Messages
20
PS do either of you know how i could put a password on the button so that only certain users can use it?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:54
Joined
May 7, 2009
Messages
19,169
you can use an Inputbox() and check the result
against your password.
you need to put the code on the Click event of the button.

you might even have a Login form and determine in advance
if that user can use the button (enabled=True).
 

FlagDay1987

New member
Local time
Today, 20:54
Joined
May 27, 2020
Messages
20
you can use an Inputbox() and check the result
against your password.
you need to put the code on the Click event of the button.

you might even have a Login form and determine in advance
if that user can use the button (enabled=True).
Thankyou
 

Users who are viewing this thread

Top Bottom