If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > toggling enable/disable of a field using vba?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
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.
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Nov 2003
Location: Europe
Posts: 369
Quote:
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.
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Nov 2003
Location: Europe
Posts: 369
Quote:
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...
Reply With Quote
  #6 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #7 (permalink)  
Old
Stuck on my opinions...
 
Join Date: Nov 2003
Posts: 1,487
See your previous post:

toggle text field: enabled/disabled

Hope it helps
Reply With Quote
  #8 (permalink)  
Old
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On