Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2003
    Location
    Europe
    Posts
    369

    Question toggling enable/disable of a field using vba?

    hi, could anyone please give an example of how to switch between two fields using a sub that toggles the one to be enabled when the other is disabled, and vice versa?

    (enable this, disable that - and one more click will do the opposite)

    A toggle button seems obvious, but can also be done by just clicking on the text fields ?

    I know theer is a vb trick that sets the opposite state of what is currently the state of a control. Maybe that could be used here, but I do not know how it is done.

  2. #2
    Join Date
    Oct 2001
    Location
    Chicago
    Posts
    440
    you have two text boxes...

    the user click on textbox1
    your code disables texbox2

    Because textbox2 is now disable - it can not have the focus set to it by either tabbing or clicking on it because it is disabled.

    So you can disable one control by clicking on the other - but now you are stuck unless you enable it by other methods/events. A disabled control will not receive its events.


    Matt

  3. #3
    Join Date
    Nov 2003
    Location
    Europe
    Posts
    369
    Originally posted by Rockey
    you have two text boxes...

    the user click on textbox1
    your code disables texbox2

    Because textbox2 is now disable - it can not have the focus set to it by either tabbing or clicking on it because it is disabled.

    So you can disable one control by clicking on the other - but now you are stuck unless you enable it by other methods/events. A disabled control will not receive its events.


    Matt
    OK. sure.
    I want to disable user input of one field while the check mark beside that field is off (standard), BUT then the value should be calculated from a percentage given in a different field... so it cannot be locked either. When placing a check mark beside the field, the calculation from the latter should be disabled, and the field should change its background color, set the focus there and be editable from the keyboard...

    Here is some code:
    Private Sub chkcomm_manual_AfterUpdate()
    If Me.chkcomm_manual = vbOK Then
    Me.txtCalcSelOurComm.BackColor = 15000804
    Me.txtCalcSelOurComm.TabStop = True
    Me.txtour_sales_commission.BackColor = 12632256
    Me.txtour_sales_commission.TabStop = False
    Me.txtCalcSelOurComm.SetFocus
    Else
    Me.txtCalcSelOurComm.BackColor = 12632256
    Me.txtCalcSelOurComm.TabStop = False
    Me.txtour_sales_commission.BackColor = 8454143
    Me.txtour_sales_commission.TabStop = True
    Me.txtour_sales_commission.SetFocus
    End If

    End Sub

    I think the if logic and the "= vbOK" is incorrect, lacking something, since it only sets the focus on txtour_sales_commission whether I check or uncheck the checkbox. Seems like it is performing the Else part no matter what.

  4. #4
    Join Date
    Oct 2001
    Location
    Chicago
    Posts
    440
    vbOK is a constant with a value of 1
    a ckeck box is either True/False -1/0 - so you were allways performing the false side of the if statement.


    Private Sub chkcomm_manual_AfterUpdate()
    Me.txtCalcSelOurComm.Enabled = Not Me.txtCalcSelOurComm.Enabled
    Me.txtour_sales_commission.Enabled = Not Me.txtour_sales_commission.Enabled
    If Me.chkcomm_manual = True Then
    Me.txtCalcSelOurComm.BackColor = 15000804
    Me.txtour_sales_commission.BackColor = 12632256
    Me.txtCalcSelOurComm.SetFocus
    Else
    Me.txtCalcSelOurComm.BackColor = 12632256
    Me.txtour_sales_commission.BackColor = 8454143
    Me.txtour_sales_commission.SetFocus
    End If

    ........

    Default Assumptions:
    chkcomm_manual value is true
    txtCalcSelOurComm.Enabled status is true txtour_sales_commission.Enabled status is false

  5. #5
    Join Date
    Nov 2003
    Location
    Europe
    Posts
    369
    Originally posted by Rockey
    Default Assumptions:
    chkcomm_manual value is true
    txtCalcSelOurComm.Enabled status is true txtour_sales_commission.Enabled status is false
    Thanks! This seems like what I am looking for, BUT: the logic is a bit strange to me, and in fact the default values are exactly the OPPOSITE of your assumptions. I have tried to figure out how the code then should look like, but had to give up, what is really the "trick" here? I "see" what the code does, but still does not "see" how the code should be altered to do this the other way around...

  6. #6
    Join Date
    Oct 2001
    Location
    Chicago
    Posts
    440
    The logic does also work for the exact opposite:
    chkcomm_manual value is false (not checked)
    txtCalcSelOurComm.Enabled status is False
    txtour_sales_commission.Enabled status is True


    ' Here's our swtich of the control's enabled status
    Me.txtCalcSelOurComm.Enabled = Not Me.txtCalcSelOurComm.Enabled
    Me.txtour_sales_commission.Enabled = Not Me.txtour_sales_commission.Enabled

    ' What you are saying here is that if the checkbox is checked then set the focus to txtCalcSelOurComm (it has already been enabled above)
    If Me.chkcomm_manual = True Then
    Me.txtCalcSelOurComm.BackColor = 15000804
    Me.txtour_sales_commission.BackColor = 12632256
    Me.txtCalcSelOurComm.SetFocus
    Else
    ' The checkbox is not checked -so set the focus to txtour_sales_commission
    Me.txtCalcSelOurComm.BackColor = 12632256
    Me.txtour_sales_commission.BackColor = 8454143
    Me.txtour_sales_commission.SetFocus
    End If

  7. #7
    Join Date
    Nov 2003
    Posts
    1,487
    See your previous post:

    toggle text field: enabled/disabled

    Hope it helps

  8. #8
    Join Date
    Nov 2003
    Location
    Europe
    Posts
    369

    Arrow

    ok, thanks!

    See further comments about having to ensure one of the fields is updateable...:

    http://www.dbforums.com/t979958.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •