473,394 Members | 1,866 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,394 software developers and data experts.

How to indicate a field is required

PhilOfWalton
1,430 Expert 1GB
I have a continuous subform, listing the fields in a table. I want to indicate whether the field is required or not.

So I need a visual indication on the subform itself without actually trying to enter data. Here is a mock-up



Stuck on this one. Have messed around with the IsRequired property, but no joy

Thanks,

Phil
Attached Images
File Type: png RequiredField.png (13.1 KB, 1194 views)
Oct 16 '18 #1
12 1723
twinnyfo
3,653 Expert Mod 2GB
Hmmmmmmmm.....

I was just thinking to myself, "Self (I always call myself, 'Self'), this oughta be really simple!"

But, then I got to thinking, Phil wants to change a property of a Control based upon a property of a Field in a Table--for each Record in that Table.

I think the rub is that Field properties are not available at the Control level. If it was a Single Form, you could do it with VBA, but that doesn't work so nicely with Continuous Forms.

Let me chew on this one a bit longer....
Oct 17 '18 #2
twinnyfo
3,653 Expert Mod 2GB
By the way, how do you determine if a field is required or not? That might be a better starting place. If I am thinking Table level, if a field is required, the field is required in the table--but I know that is not what you are asking.
Oct 17 '18 #3
PhilOfWalton
1,430 Expert 1GB
Ah, Twinnyfo, if it was simple, I hope I wouldn't be asking. I am just trying to keep the little grey cells working.

No I don't want to change any properties of the control, just the format.

Yes, the Field is determined in the table design along with "Allow Zero length" & "Indexed" etc.

I have tried
Expand|Select|Wrap|Line Numbers
  1. Private Sub CboTables_AfterUpdate()
  2.  
  3.     Dim objFC As FormatCondition
  4.  
  5.     Me!PFrmSubFields.Form.InputField.FormatConditions.Delete
  6.     Set objFC = Me!PFrmSubFields.Form.InputField.FormatConditions.Add(acExpression, , _
  7.         "Currentdb.TableDefs(CurrentDb.TableDefs(Me!PFrmSubFields.Form![TableName]).Name).Fields(Me!PFrmSubFields.Form!InputField).Properties(15) = True")
  8.     Me!PFrmSubFields.Form.InputField.FormatConditions(0).ForeColor = vbRed
  9.  
  10. End Sub
which runs without error, but doesn't change the colour of the field.

It may help to refer to https://bytes.com/topic/access/answe...lised-database

To explain the code, this runs when I update "CboTables" next to "Please Select Tables at Level: 3:"

That selects the "TableName"

The subform is called "PFrmSubFields"

The Control under the pale blue Field Name column is "InputField"

Phil
Oct 17 '18 #4
twinnyfo
3,653 Expert Mod 2GB
Phil,

Could it be this:

Expand|Select|Wrap|Line Numbers
  1. Me!PFrmSubFields.Form.InputField
might should be:

Expand|Select|Wrap|Line Numbers
  1. Me!PFrmSubFields.Form.txtInputField
The former is ambiguously referring to either the underlying Field OR a Text Box Control; the latter is explicitly referring to a Text Box Control.
Oct 17 '18 #5
NeoPa
32,556 Expert Mod 16PB
I guess you only have the property of the Field to work from in your explanation of the scenario. I'm not aware of any way of handling this using Conditional Formatting without setting up a function procedure to return the value.

On the other hand, is this something that could be handled at the time of loading or opening? Code in one of those event procedures could determine the field and its properties from the control.

It would help to have a clear idea of exactly what it is you're after. Bear in mind that items on a form, though they may be associated with fields, are controls. It is these that somehow need to be set as required. The fields already are, or not.
Oct 17 '18 #6
PhilOfWalton
1,430 Expert 1GB
@Twinnyfo

Won't even compile so no luck there I'm afraid

Neopa

The mock up in post no 1 is exactly what I a after.

Here is the table structure:=-



This is an image of the code running:-



When I halt the code on the line beginning "Set", in the Debug Window, I correctly get 0 (Black ForeColor)

Move on to the end and I get 255 Red which is what is wanted.

Trouble is the Field Names don't change colour

Phil
Attached Images
File Type: png CostType.png (15.3 KB, 393 views)
File Type: jpg FormatCode.jpg (97.5 KB, 350 views)
Oct 17 '18 #7
NeoPa
32,556 Expert Mod 16PB
So, other than the result you don't care about any other details. Got it. In that case both suggestions I included in my previous post should work for you. Have you considered which, if any, seems the more appropriate? I'd be looking at setting up the Controls within the Load or Open event procedures personally.

I would just point out that colouring the text of Controls, which I expect will start off empty, may not be a very reliable way to indicate that a value's required. Typically I'd expect that the associated Label field would make more sense to use. That's just an idea of course.
Oct 17 '18 #8
PhilOfWalton
1,430 Expert 1GB
Yes the label option is a good one, I'll go with that, but bearing in mind that we are talking continuous forms, we will still need Conditional Formatting?????

Phil
Oct 17 '18 #9
PhilOfWalton
1,430 Expert 1GB
Cracked it

Created a Function
Expand|Select|Wrap|Line Numbers
  1. Public Function RequiredField(TableName As String, FieldName As String) As Boolean
  2.  
  3.     Dim MyDb As Database
  4.     Set MyDb = CurrentDb
  5.  
  6.     On Error Resume Next
  7.  
  8.     RequiredField = MyDb.TableDefs(TableName).Fields(FieldName).Properties("Required")
  9.  
  10. End Function
The RecordSource of the subform now has an additional field "IsRequired" set to the above function.

I then do standard conditional formatting depending on whether "IsRequired" is true or false.

Phil
Oct 18 '18 #10
NeoPa
32,556 Expert Mod 16PB
Hi Phil.

Does it make sense to you to use ConditionalFormatting?

Surely the Controls that are for required Fields don't change between records. Hence the suggestion to handle it more simply, and once only, when the Form is opened or loaded. That seems a far simpler and more straightforward approach to me.
Oct 19 '18 #11
PhilOfWalton
1,430 Expert 1GB
I see what you are getting at and have tried
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.     Dim MyDb As Database
  4.     Set MyDb = CurrentDb
  5.  
  6.     On Error Resume Next
  7.  
  8.     If MyDb.TableDefs(TableName).Fields(FieldName).Properties("Required") = True Then
  9.         InputField.BackColor = vbRed
  10.     Else
  11.         InputField.BackColor = vbYellow
  12.     End If
  13.  
  14. End Sub
All the fields are yellow.

The routine just seems to "Look" at the first control on the subform

Thanks

Phil
Oct 19 '18 #12
NeoPa
32,556 Expert Mod 16PB
Phil of Walton:
The routine just seems to "Look" at the first control on the subform
That sort of makes sense really. I see the term FieldName in your code has no visible definition. I'm guessing that you don't have Require Variable Declaration set and that FieldName is therefore automagically declared for you as a Variant with nothing in it. That roughly translates the Fields(FieldName) reference to Fields(0) which would indeed be the first field.

What you need is to loop through all the controls while ignoring any that are not of the types you're using. That would certainly include TextBoxes, and probably ComboBoxes and CheckBoxes, but not sure what else. This would further be restricted to exclude any such control without an associated Label.

Something like :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Dim lngVar As Long
  3.     Dim strField As String
  4.     Dim ctlLbl As Control
  5.  
  6.     For Each ctlLbl In Me.Detail.Controls
  7.         With ctlLbl
  8.             If .ControlType = acLabel Then
  9.                 lngVar = 0
  10.                 On Error Resume Next
  11.                 With .Parent
  12.                     lngVar = .ControlType
  13.                     strField = .ControlSource
  14.                 End With
  15.                 On Error GoTo 0
  16.                 If strField > "" _
  17.                 And (lngType = acCheckBox _
  18.                 Or lngType = acTextBox _
  19.                 Or lngType = acComboBox) Then
  20.                     'We have a valid Control pair.
  21.                     lngColour = IIf(Me.Recordset.Fields(strField).Required _
  22.                                   , vbRed, vbYellow)
  23.                     .BackColor = lngColour
  24.                     .Parent.BackColor = lngColour
  25.                 End If
  26.             End If
  27.         End With
  28.     Next ctlLbl
  29. End Sub
In the object structure any associated Label has the associated Control as its .Parent. The associated Control has a .Controls() collection that contains the Label as its only entry.
Oct 19 '18 #13

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

Similar topics

6
by: Walt | last post by:
It's easy to make a field required for inserts, just set it to not null and don't give it a default. But how does one make a field required for updates? For instance, we have a table with a...
1
by: Deborah V. Gardner | last post by:
I have a form frmViolations with a subform sfrmViolations. There is a one-to-many relationship between the two. On the subform I have a date field and a duration field. Both can be blank but if...
2
by: kufre | last post by:
I want to make some field required if a condition is true. Example, If txtSubNumber is equal to 2 or greater, I want txtOriginalSubmissionDate to be required.
3
by: rainwood | last post by:
I've been trying for quite some time. I have a datagrid with a dropdown working perfect. But, now I want to make the field required. (default value is set to 'pick a type' and I want to make sure...
0
kaptaineaux
by: kaptaineaux | last post by:
Hi, What I am trying to do is update a field in list programmatically using a webpart. What I do is I get the context of the site, then all lists of type Events. For each End field that is...
8
by: Jeanette White | last post by:
I am trying to create a custom validator to validate that if "Other" is selected from a drop downdown list then a textbox is displayed and a comment is required. The display textbox piece is...
8
by: mjvm | last post by:
Hi, I have had a similar question answered but don't understand code sufficiently to adapt it myself. Can someone please help and write the code I need? I have a date field called Received, and...
3
by: sureshl | last post by:
Am facing a problem, with this script... I want two fields to be written by star , in order to hide . and this script does it , but , wat actually is my page opens directly with star letter ,...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.