473,397 Members | 2,084 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,397 software developers and data experts.

Change Required property of textbox via VBA

Hello,

I have a form that has some controls whose properties are already listed as required in the table, as they are always enabled. The form also has a combo box labeled "Outcome" that changes other textboxes to be enabled depending on what is chosen in that combo box. What I would like to do, is make sure that whatever textboxes are enabled are also required. Is this possible? Here is my code for the Outcome combo box

Expand|Select|Wrap|Line Numbers
  1. Private Sub Outcome_Change()
  2. If [Outcome] = "Pending" Then
  3.     [Date Provided].Enabled = False
  4.     [Date Provided] = Null
  5.     [Providers].Enabled = False
  6.     [Providers] = Null
  7.     [Number of Pages].Enabled = False
  8.     [Number of Pages] = Null
  9.     [Time].Enabled = False
  10.     [Time] = Null
  11.     [Cost].Enabled = False
  12.     [Cost] = Null
  13.     [Comments].Enabled = True
  14. Else
  15.         If [Outcome] = "Interpretation" Then
  16.             [Date Provided].Enabled = True
  17.             [Providers].Enabled = True
  18.             [Number of Pages].Enabled = False
  19.             [Number of Pages] = Null
  20.             [Time].Enabled = True
  21.             [Comments].Enabled = True
  22.         Else
  23.                 If [Outcome] = "Translation" Then
  24.                     [Date Provided].Enabled = True
  25.                     [Providers].Enabled = True
  26.                     [Number of Pages].Enabled = True
  27.                     [Time].Enabled = True
  28.                     [Comments].Enabled = True
  29.                 Else
  30.                     [Date Provided].Enabled = False
  31.                     [Date Provided] = Null
  32.                     [Providers].Enabled = False
  33.                     [Providers] = Null
  34.                     [Number of Pages].Enabled = False
  35.                     [Number of Pages] = Null
  36.                     [Time].Enabled = False
  37.                     [Time] = Null
  38.                     [Cost].Enabled = False
  39.                     [Cost] = Null
  40.                     [Comments].Enabled = True
  41.                 End If
  42.         End If
  43. End If
  44. End Sub
Jun 13 '14 #1
10 1506
twinnyfo
3,653 Expert Mod 2GB
Elaine,

I would use a Select Case Statement for your Combo Box, which prevents the eternal nesting of If...Then statements. This would also make it easier to add additional values to your combo box.:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Outcome_Change()
  2.     Select Case Outcome
  3.         Case "Pending"
  4.             [Date Provided].Enabled = False
  5.             [Date Provided] = Null
  6.             [Providers].Enabled = False
  7.             [Providers] = Null
  8.             [Number of Pages].Enabled = False
  9.             [Number of Pages] = Null
  10.             [Time].Enabled = False
  11.             [Time] = Null
  12.             [Cost].Enabled = False
  13.             [Cost] = Null
  14.             [Comments].Enabled = True
  15.         Case "Interpretation"
  16.             [Date Provided].Enabled = True
  17.             [Providers].Enabled = True
  18.             [Number of Pages].Enabled = False
  19.             [Number of Pages] = Null
  20.             [Time].Enabled = True
  21.             [Comments].Enabled = True
  22.         Case "Translation"
  23.             [Date Provided].Enabled = True
  24.             [Providers].Enabled = True
  25.             [Number of Pages].Enabled = True
  26.             [Time].Enabled = True
  27.             [Comments].Enabled = True
  28.         Case Else
  29.             [Date Provided].Enabled = False
  30.             [Date Provided] = Null
  31.             [Providers].Enabled = False
  32.             [Providers] = Null
  33.             [Number of Pages].Enabled = False
  34.             [Number of Pages] = Null
  35.             [Time].Enabled = False
  36.             [Time] = Null
  37.             [Cost].Enabled = False
  38.             [Cost] = Null
  39.             [Comments].Enabled = True
  40.     End Select
  41. End Sub
What confuses your post is your question: "What I would like to do, is make sure that whatever textboxes are enabled are also required. Is this possible?"

Based on your code, if you have run through enabling or disabling all of the available text boxes (based on the value of Outcome), then only those required text boxes would be enabled--unless there are some controls on your form that are not listed above.

But, it your really had to, you could query the Enabled Property of any other controls on your form.

As for some additional guidance and mentoring on building databases, it appears that your DB is in need of normalization. This may be a complete overhaul, but I would recommend it. You can find some guidance here: Database Normalization.

Additionally, with normalization, your combo box would not longer have text values, but numbers. Integers are a lot easier to work with, take up fewer resources, and it a standard in database development. In the long term, such changes will help you create robust and sound projects.

I cope the code above answered your question or at least hepped a bit.
Jun 13 '14 #2
zmbd
5,501 Expert Mod 4TB
additionally:
---
-It is best practice when naming fields, tables, and files to avoid the use of anything other than alphanumeric characters and the underscore (spaces although allowed are problematic from a programing point of view and best avoided) and it is VERY importaint to avoid all reserved words and tokens (such as "Time" or "%" etc):
Access 2007 reserved words and symbols
AllenBrowne- Problem names and reserved words in Access
Jun 13 '14 #3
Hello,

Thanks for the reply. Indeed I have a lot to learn. I just started teaching myself Access with this project mid May and have to have it complete by next Friday, so unfortunately, a complete overhaul is not gonna happen. However, I will definitely look at that link. I have been trying to read/watch as many tutorials as possible (even got a coding for dummies).

Good to know about the Select Case...indeed all the If...Then was getting annoying.

In regards to my question, I apologize it wasn't very clear. This is for my main form. The top half of the form has 9 controls that are always enabled and must be entered before the form can close. These fields are all set to required in the table. To get an idea, this form is for clients who wish to have an interpreter/translator provided to them and the top half is treated like an intake, where we enter the client information, the date they need the service, if they want an interpreter/translator, the language they want, etc. So when the case is first entered, the outcome will be "Pending" unless the service happened the same day as the request.

After the service is provided, we pull up the case again and change the "outcome" to interpretation or translation. Upon the outcome change, other controls become enabled, such as who provided the service, the date it actually was provided, the amount of time it took, the cost, etc. Once these fields are enabled, they should also be required before the form can be saved. However, right now, the form can be saved and closed even if they aren't filled out. I would like to prevent the users from just changing the "outcome" status (which would close the case) and not filling in the other fields.

I hope this clears up what I would like to do. Thanks!
Jun 13 '14 #4
Im a dunce and thought I had something going with the setproperty on the macro but I was wrong.
Jun 14 '14 #5
I thought I could try to use the Validation Rule, but it isn't working, so maybe I am doing it wrong.

I thought I could do:
Expand|Select|Wrap|Line Numbers
  1. If [Outcome]="Pending" OR [Outcome]="No Service" Then [Control]=Not Null
I also tried:
Expand|Select|Wrap|Line Numbers
  1. IIf([Outcome]="Pending" Or [Outcome]="No Service",[Providers]=Null,[Providers]=Not Null)
I also thought I could put in my code above, after each control is enabled to also have it =Not Null, but that didn't seem to work either.
Jun 14 '14 #6
Ok, I think I figured it out for the most part ;D

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. Select Case Outcome
  3.     Case "Interpretation"
  4.         If IsNull(Me.[Date Provided]) Then
  5.             Cancel = True
  6.             MsgBox "Enter the date the service was provided!"
  7.         End If
  8.         If IsNull(Me.Providers) Then
  9.             Cancel = True
  10.             MsgBox "Enter a Provider!"
  11.         End If
  12.         If IsNull(Me.Time) Then
  13.             Cancel = True
  14.             MsgBox "Enter the time in minutes spent interpreting/translating"
  15.         End If
  16.         If [Providers] = "Outsider" And IsNull(Me.Cost) Then
  17.             Cancel = True
  18.             MsgBox "Enter the cost it took for service to be provided!"
  19.         End If
  20.     Case "Translation"
  21.         If IsNull(Me.Date_Provided) Then
  22.             Cancel = True
  23.             MsgBox "Enter the date the service was provided!"
  24.         End If
  25.         If IsNull(Me.Providers) Then
  26.             Cancel = True
  27.             MsgBox "Enter a Provider!"
  28.         End If
  29.         If IsNull(Me.Time) Then
  30.             Cancel = True
  31.             MsgBox "Enter the time in minutes spent interpreting/translating"
  32.         End If
  33.         If IsNull(Me.Number_of_Pages) Then
  34.             Cancel = True
  35.             MsgBox "Enter the number of pages that were translated!"
  36.         End If
  37.         If [Providers] = "Outsider" And IsNull(Me.Cost) Then
  38.             Cancel = True
  39.             MsgBox "Enter the cost it took for service to be provided!"
  40.         End If
  41.     Case "No Service"
  42.         If IsNull(Me.Comments) Then
  43.             Cancel = True
  44.             MsgBox "Enter a reason for why service was not provided!"
  45.         End If
  46. End Select
  47. End Sub
Only problem here is that it won't work for my combo box "Providers". I'm not sure why. It keeps popping up the message box after I select someone from the combo, and now it seems to have broken my code as it is still acting as if the code is there even after I deleted that portion of the code and restarted the program :(

Also, I am sure this doesn't look the best and could probably be written a different way, but it is what I figured out from multiple Google searches :D
Jun 15 '14 #7
jimatqsi
1,271 Expert 1GB
Elaine,
Please explain the problem clearly. You say "the messagebox for Providers" but in fact, there are multiple references to the Providers combo box and each has a different message. So which message are you seeing?

Jim
Jun 15 '14 #8
Sorry, I meant the one with If IsNull([Providers]) Then...where the message states to enter a provider. I just saw something to try to add the Nz, so am going to try that.

Edit: This didn't work
Expand|Select|Wrap|Line Numbers
  1. If Nz(Me.Providers, "") = "" Then
  2.             Cancel = True
  3.             MsgBox "Enter a Provider!"
  4.         End If
Jun 15 '14 #9
Hmm, I pulled up a backup saved file and redid the code with the Nz and it works. So I don't know what happened to my other version but I think the problem of my combo is fixed. Thanks for the reply.

BTW, is there a way to mark this as solved?
Jun 15 '14 #10
jimatqsi
1,271 Expert 1GB
So glad you got a good result. Congratulations. :)

There should be a link for you to mark the best answer.
Jun 15 '14 #11

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

Similar topics

3
by: Mark | last post by:
Access97 --- I set the Required property for a field at the table level and I have a form that contains that field. When I click the Close button at the top left of the screen, I get an error...
0
by: Marcin Grzębski | last post by:
Hi, I want do create control (based on UserControl) that contains collection MyItemCollection (inherited from ICollection) of objects MyItem. I spent so much time to force this collection save...
3
by: Kejpa | last post by:
Hi, I have a datatable which I try to update but the update fails because a required property of one of the fields is set to True. Where can I find this property? On the Column there's only a...
6
by: Aaron Smith | last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child tables, I have a column that I added to the DataSet (Not in the DataSource). This column does not need to be stored in the...
0
by: Just D. | last post by:
All, I created a tool to setup our web apps, it does almost everything now, but how can we change the property of one of the subdirectories? One of the subdirectories should be made Writeable. ...
1
by: Jim M | last post by:
After learning that the 'subdatasheet name' property set to "" can adversely effect database speed, I wanted to send a small code snippent to my end users to reset the SubdatasheetName Property so...
2
by: Billy | last post by:
Hopefully a simple one, what is the syntax to force a property to be required in a user control? I thought it would below, but it's not: Public Required Property propName() As String
8
by: Randoz | last post by:
I am trying to write an if/then statement to disable the "Required Property" and then to enable it after a record is saved. Almost like the visible and locked code.
8
by: shruthiKKKK | last post by:
change appearence for textbox by using combolist
1
by: Andy Sauer | last post by:
Hello. I have a database in Access 2002. I have a table where the field "Name" is set as a required field. I have a form that populates that table. When somebody forgets to fill in the Name...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.