Link to home
Start Free TrialLog in
Avatar of cssc1
cssc1Flag for United States of America

asked on

How to Disable a command button?

PROBLEM
I am trying to Disable Command8 until the user clicks (Selects) an AHA on the form “frmAHA_Selection_Form_VER1”.

See image and DB
Disable.jpg
EE-TEST-8-3-2010-AHA-Builder-201.zip
Avatar of jnbkze
jnbkze
Flag of Afghanistan image

I do recall in VB there is a toolbar where you can select the command button and say visible= false

I recall the same for delphi, but I have no idea what program you are using.. so maybe a command like commandbutton1.visible=false?
I think that I took care of the issue.  Have a look at your form and make sure that it is doing what you requested.

Good looking app by the way, nicely designed form.

TLH
EE-TEST-8-3-2010-AHA-Builder-201.zip
Avatar of Dale Fye
In the forms load event, set the enabled property of the command button to false.

Then, I would put a hidden textbox in the forms footer with a control Source like: = Sum([Selected]).  You will need to change that field name as appropriate for your checkbox

Then, In the AfterUpdate event of your Select checkbox, do something like:

me.cmdNext.Enabled = (me.txtSumSelected <> 0)
I trired that approch, problem was there is a a sub that loops through to ensure that the user can only have one record selected at a time. If you use a Sum, it will negate the single record selecte function.
Avatar of cssc1

ASKER

t_hungate:
   I trid the db and the NEXT button is disabled, however, even when the user selects an AHA it stays disabled. After the user selects an AHA the NEXT button should become activated.
A couple of points.

1.  you really should use a naming convention for your controls.  When someone other than you looks at this forms code module, they have to figure out what command8, command10, and command66 and command67 are all about.  Wouldn't it make more sense to label this cmdNext, cmdClose, cmdPrevious

2.  As I mentioned in my previous post, I would set set command8's Enabled property to False.  You can do that in design view, but if you inadvertantly save the form with it enabled, it will retain the enabled status.  Instead, use the form_Load event to set the property to False.

3.  I've added just a few lines to your code for the Select_Click event.  See the code below.  Basically, I added a check to see whether the value of Select = True.  If so, then reset the other values and enable Command8.  If False, then don't worry the other values of Select, but you do need to disable Command8.
Private Sub select_click()

Dim lngID As Long

    If Not Me.NewRecord Then
        'added this test
        If Me.Select = True Then
            lngID = Me.AHAID
            
            With Me.Recordset
                  .MoveFirst
                 Do Until .EOF
                    
                    If Me.AHAID <> lngID Then
                      .Edit
                      ![Select] = 0
                      .Update
                    End If
                      .MoveNext
                 Loop
                 .FindFirst "[AHAID]=" & lngID
            End With
            'added next 3 line
            Me.Command8.Enabled = True
        Else
            Me.Command8.Enabled = False
        End If
    Else
        'MsgBox "New  record"
    End If

End Sub

Open in new window

Avatar of cssc1

ASKER

fyed:
  I must be doing something incorrect. Where do I put the code you wrote?

Here is the code I currently have for the on click event for command8:
Private Sub Command8_Click()
On Error GoTo Err_Command8_Click

    Dim stDocName As String

    stDocName = "Macro_Close_AHASelect_Open_frmAHA_VER1"
    DoCmd.RunMacro stDocName

Exit_Command8_Click:
    Exit Sub

Err_Command8_Click:
    MsgBox Err.Description
    Resume Exit_Command8_Click
   
End Sub
False-8-14-2010.jpg
cssc1

It is working perfectly for me.  When you open the form the Next> button is disabled.  Upon AHA record selection the button is enabled. If you switch between records it will remain enabled.  If you select a record, and then de-select it, it will go back to a disabled state. I am pasting the entire forms code here, check it against the one you are using and see if it matches.

TLH
Option Compare Database

Private Sub Check63_Click()
'Me.Check63 = ""
End Sub

Private Sub Command8_Click()
On Error GoTo Err_Command8_Click

    Dim stDocName As String

    stDocName = "Macro_Close_AHASelect_Open_frmAHA_VER1"
    DoCmd.RunMacro stDocName

Exit_Command8_Click:
    Exit Sub

Err_Command8_Click:
    MsgBox Err.Description
    Resume Exit_Command8_Click
    
End Sub
Private Sub Command10_Click()
On Error GoTo Err_Command10_Click

    Dim stDocName As String

    stDocName = "Macro_Close_AHA_Selection_Open_Splash"
    DoCmd.RunMacro stDocName

Exit_Command10_Click:
    Exit Sub

Err_Command10_Click:
    MsgBox Err.Description
    Resume Exit_Command10_Click
    
End Sub

Private Sub Frame48_DblClick(Cancel As Integer)
'Me.Frame48 = ""
End Sub
Private Sub Combo57_AfterUpdate()
    ' Find the record that matches the control.
    Dim rs As Object

    Set rs = Me.Recordset.Clone
    rs.FindFirst "[AHAID] = " & Str(Nz(Me![Combo57], 0))
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub

Private Sub Command65_Click()
On Error GoTo Err_Command65_Click

    DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

Exit_Command65_Click:
    Exit Sub

Err_Command65_Click:
    MsgBox Err.Description
    Resume Exit_Command65_Click
    
End Sub

Private Sub Form_Open(Cancel As Integer)

CurrentDb.Execute "update [3-tblAHA_VER1] set [select]=0"
'Me.Command8.Enabled = False
End Sub

Private Sub Select_AfterUpdate()
If Me.Select.Value = -1 Then Me.Command8.Enabled = True
If Me.Select.Value = 0 Then Me.Command8.Enabled = False
DoCmd.RefreshRecord
End Sub

Private Sub select_click()

Dim lngID As Long

If Not Me.NewRecord Then
lngID = Me.AHAID

With Me.Recordset
      .MoveFirst
     Do Until .EOF
        
        If Me.AHAID <> lngID Then
          .Edit
          ![Select] = 0
          .Update
        End If
          .MoveNext
    Loop
     .FindFirst "[AHAID]=" & lngID
End With
Else
    'MsgBox "New  record"
End If


End Sub

Private Sub Command66_Click()
On Error GoTo Err_Command66_Click


    DoCmd.GoToRecord , , acPrevious

Exit_Command66_Click:
    Exit Sub

Err_Command66_Click:
    MsgBox Err.Description
    Resume Exit_Command66_Click
    
End Sub
Private Sub Command67_Click()
On Error GoTo Err_Command67_Click

    DoCmd.GoToRecord , , acNext

Exit_Command67_Click:
    Exit Sub

Err_Command67_Click:
    MsgBox Err.Description
    Resume Exit_Command67_Click
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Tony Hungate
Tony Hungate
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cssc1

ASKER

t_hungate:
  Same problem, please see attached image.
Thanks,
CSSC1
SameProblem.jpg
That is very odd, it works just fine on my end.

TLH
Your-form.jpg
You got the idea correct about disabling the NEXT button as the default.  The other code replaces the code you currently have for the SELECT_CLICK event.  
Avatar of cssc1

ASKER

fyed:\
  Sorry, i don't understand your explaination. What do you mean by:
The other code replaces the code you currently have for the SELECT_CLICK event?
I am really baffled by this.  What version of Access are you running?  This thing is working perfectly on my end.

I am attaching a new db that I only copied the needed tbl, from, macro and modules.

Let me know if this one works for you.  It is rock solid on my end.

TLH
Database1.accdb
This one is in MDB format, just in case you are using 2003.
Database11.mdb
Avatar of cssc1

ASKER

t_hungate:
  Please see new ERROR message in image when I click the for:
frmAHA_VER1.


NewError-8-14-2010.png
EE-TEST-8-3-2010-AHA-Builder-201.zip
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me know what you name your other question.  I may have found the issue.

TLH
css1,

I'm not sure what you are unclear about, but am sending you that form, with my edits back in a new accdb.  Copy this form into your application and see if it meets your needs.
AHA-BuilderSampleForm.accdb
Avatar of cssc1

ASKER

fyed:
   Same problem. I attached the DB with the SampleForm so you can see what it is doing.
8-14-2010-AHA-Builder-2010-.zip
If you are talking about the same problem that you mention to t_hungate about the popup form after clicking Next, I would agree.  We have both provided you solutions to your original stated problem.  Assign point, then post another question.  

However, since we are unable to review all of your code, I would ask that you only provide those forms, tables, and queries that are pertinent to the problem.  Personally, I have real trepidation about downloading someone elses code and running it on my computer unless I can review ALL of the code before opening a form in Form view and allowing the code to run freely.
Avatar of cssc1

ASKER

fyed:
Sorry, I should of explained better. By the same problem I mean the same as in post:

08/14/10 04:54 PM, ID: 33438391
Then you have a problem, because it worked perfectly for me.