473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

conditional fields

I've got a form that is basically like a survey. I've got a yes/no field that i'll call "condition1"

if someone does not check condition1, there are several other fields that don't apply to them. how can i make it so that these fields remain locked / invisible until condition1 is checked? Ideally these locked fields would be skipped over by tab order as well.

I know that I can use conditional formatting, but this doesn't exactly solve my problem. Thanks!

(I'm new to this forum and to access, so sorry if this question has already been posted)
Aug 2 '07 #1
12 2478
missinglinq
3,532 Expert 2GB
Exactly how many fields are we talking about here? And do you want them invisible (which may make the form look unbalanced) or simply greyed out? (Go into Design View, select a textbox, goto Properties - Data and change Enabled to No. Run the form to see what I mean by this)

Welcome to TheScripts!

Linq ;0)>
Aug 2 '07 #2
greyed out would be great. can i start with them disabled and then set up a macro to enable them if conditional1 is checked?

i think there are about 30 or so fields that i'd like to disable.
Aug 2 '07 #3
damonreid
114 Expert 100+
Disabling them as discribed above is the frist step. The next thing to do is go to the onchange part of your yes/no tick box and enter some code to enable them once it is selected.

Expand|Select|Wrap|Line Numbers
  1. Private Sub TickBox_Click()
  2. 'Once TickBox chagnes check to see if it is true.
  3. 'If it is enable details, else disable details.
  4.     If TickBox= True Then
  5.         Me.Field1.Enabled = True
  6.         Me.Field2.Enabled = True
  7. .
  8. .
  9. .
  10.         Me.Fieldx.Enabled = True
  11.     End If
  12.     If TickBox= False Then
  13.         Me.Field1.Enabled = False
  14.         Me.Field2.Enabled = False
  15. .
  16. .
  17. .
  18.         Me.FieldX.Enabled = False
  19.     End If
  20. End Sub
Aug 2 '07 #4
great! that looks like it should solve my problem, thanks!
Aug 2 '07 #5
missinglinq
3,532 Expert 2GB
Damon's code has the right idea, but it requires you set the enabled/disabled property for each of your many textboxes, one at a time. It also won't set the enabled/disabled property correctly as you move from record to record. This needs to be done in the Form_Current event.

This may sound and look complicated at first, but it really isn't, especially compared with the alternative, which is to deal with the status of each and every one of your 30 textboxes separately! In order to do this you need to use the Tag Property of your textboxes to tell Access which ones to make enabled /greyed out depending on the status of Condition1.

Note: You have referred to your checkbox as Condition1 in one of your posts and Conditional1 in another! I went with Condition1 for this code; just make sure the the actual name of the checkbox matches the references in the code.

To set the Tag property of a textboxes:

Select all the textboxes you want to be affected by holding down <Shift> and clicking on each one.
Click on Properties
Click on Other

In the Tag Property box enter Cond1 (no quotation marks!)

While you’ve got them all selected, you can also set Enabled to No for all of them, so when the form opens they’ll start out disabled.

Now what this routine does, basically, is check to see whether Condition1 is checked or not, loop thru your textboxes, seeing if they have the Tag Property set to Cond1 and setting them to Enabled = True or Enabled = False, dependng on the status of the checkbox. If the user makes a mistake and has to change Condition1 , the textboxes will change accordingly.

Select the Condition1 checkbox
Right click and click on Properties
Click on Events
Click to the right of the box for the OnClick Property
Click on the ellipsis (...)
Click on Code Builder

You’re now in the VBA Editor! You should see this sub:

Private Sub Condition1_Click()

End Sub

Copy and paste this code between these two lines:

'This sets the textboxes to Enabled or not Enabled immediately

Expand|Select|Wrap|Line Numbers
  1. Dim ctrl As Control
  2.  
  3. If Condition1 Then  'If the box is checked
  4.  For Each ctrl In Me.Controls
  5.    If ctrl.ControlType = acTextBox Then
  6.     If ctrl.Tag = "Cond1" Then
  7.      ctrl.Enabled = True
  8.     End If
  9.    End If
  10.  Next
  11.  
  12. Else   'If the box is not checked
  13.  For Each ctrl In Me.Controls
  14.    If ctrl.ControlType = acTextBox Then
  15.     If ctrl.Tag = "Cond1" Then
  16.      ctrl.Enabled = False
  17.     End If
  18.    End If
  19.  Next
  20. End If
  21.  
While you’re in the VBA Editor, copy and paste this code also. It does the same check as the previous code, but it does it each time you move from one record to another, so that the Enabled status of each textbox is set properly.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. Dim ctrl As Control
  3.  
  4. If Condition1 Then
  5.  For Each ctrl In Me.Controls
  6.    If ctrl.ControlType = acTextBox Then
  7.     If ctrl.Tag = "Cond1" Then
  8.      ctrl.Enabled = True
  9.     End If
  10.    End If
  11.  Next
  12.  
  13. Else
  14.  For Each ctrl In Me.Controls
  15.    If ctrl.ControlType = acTextBox Then
  16.     If ctrl.Tag = "Cond1" Then
  17.      ctrl.Enabled = False
  18.     End If
  19.    End If
  20.  Next
  21. End If
  22. End Sub
  23.  
Click on the “Save” button and exit the Editor. Run the form and try the checkbox out.

Note: If the lines for the code appear when you copy and paste this into the code editor, be sure to delete them, along with the following period.

Good Luck!

Linq ;0)>
Aug 2 '07 #6
thanks a lot linq, that sounds a lot easier than handling each of the thirty individually!
Aug 2 '07 #7
missinglinq
3,532 Expert 2GB
The Tag property is great for grouping controls so that you can do something to a bunch of them at the same time, such a the case here! The only real quirk that I like to remind people of is that the Tag property, which can be any string, has to be entered in the Property Box like

Tag1

without quotes, but referred to in code as

"Tag1"

with quotes.

Let us know if you need more help.

Good Luck!

Linq ;0)>
Aug 2 '07 #8
The code doesn't seem to work for some reason. Although I've got the relevant fields disabled right now, when I click nothing happens. Here's the code I have pasted in:

Private Sub Accountopened_Click()
'This sets the textboxes to Enabled or not Enabled immediately

Dim ctrl As Control

If Account_opened Then 'If the box is checked
For Each ctrl In Me.Controls
If ctrl.ControlType = acTextBox Then
If ctrl.Tag = "Cond1" Then
ctrl.Enabled = True
End If
End If
Next

Else 'If the box is not checked
For Each ctrl In Me.Controls
If ctrl.ControlType = acTextBox Then
If ctrl.Tag = "Cond1" Then
ctrl.Enabled = False
End If
End If
Next
End If
End Sub

Any ideas as to why this is?
Aug 2 '07 #9
although, actually testing on a smaller file than what i want to eventually apply this to, i have the problem that text fields respond to clicking condition1, whereas yes/no fields don't
Aug 2 '07 #10
missinglinq
3,532 Expert 2GB
Sorry, that's because the code doesn't tell them to look at the checkboxes!

In all of the code, replace each occurrence of this line

Expand|Select|Wrap|Line Numbers
  1. If ctrl.ControlType = acTextBox  Then
  2.  
with this line
Expand|Select|Wrap|Line Numbers
  1. If ctrl.ControlType = acTextBox Or ctrl.ControlType = acCheckBox Then
  2.  
That way it'll check both textboxes and checkboxes.

Linq ;0)>
Aug 2 '07 #11
I might just remove the if/endif statement altogether just to make it a little more universally applicable. Thanks again!
Aug 2 '07 #12
missinglinq
3,532 Expert 2GB
You can't do that!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

If you remove the If...End If, Access will loop thru all controls, including those such as labels, that don't have the Enabled property, try to set them to Enabled Yes or No, and the code will fail!

Before looping and attempting to change a property, you have to be sure the type of controls you're checking actually has that property!

Here's the constants for the various types:

acCheckBox
acComboBox
acCommandButton
acLabel
acTextBox
acListBox
Aug 2 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
5
by: John Baker | last post by:
Hi: I have a field that i wish to use the conditional format capability on, and for some reason it wont work. The field is a a text box: =- I would like to make it red when negative, and...
2
by: Von Bailey | last post by:
I have a form where the conditional formatting is set on some fields to bold if certain conditions are met. However, when the conditions are met some of the data that is to bold is either not...
2
by: Sara | last post by:
The problem: Conditional formatting bold, red when field Value < date() sets the field background to white - always - whether condition is met or not. I want the field unfilled and just red/bold...
3
by: Dexsquab Molutinator via AccessMonster.com | last post by:
Greetings. I have searched for an answer to my query for some time and have had no success. I'm hoping the pooled resources of all your brains can help. How do you set a field to become active...
4
by: a | last post by:
I'm having trouble testing a custom object. I've tried many different approaches. One is shown below. The XML below shows the state of the object and I'm trying to test for that state, ie there...
8
by: Typehigh | last post by:
I have many text fields with conditional formatting applied, specifically when the condition is "Field Has Focus". Without any events associated with the fields the conditional formatting works...
2
by: audespero | last post by:
Hello, I'm a student tech at my college's campus library and i'm in charge of inventory for our A/V equipment this summer. I've decided to use access to tackle this task, and so far things are...
2
by: Filips Benoit | last post by:
Dear All, Access 2003 adp on SQL_server 2005 A continious form showing 1 month based on table 'CALENDAR_MONTH_GRID' and fill with a SP. Fields: Companyname, Day1, day2, etc. The value in the...
2
by: Paul | last post by:
Can you help me with this error? string fields = string.Empty; .... fields.Length 0 ? fields += ", Name" : fields += "Name"; Error: Only assignment, call, increment, decrement, and new...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.