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
12 1723
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....
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.
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 -
Private Sub CboTables_AfterUpdate()
-
-
Dim objFC As FormatCondition
-
-
Me!PFrmSubFields.Form.InputField.FormatConditions.Delete
-
Set objFC = Me!PFrmSubFields.Form.InputField.FormatConditions.Add(acExpression, , _
-
"Currentdb.TableDefs(CurrentDb.TableDefs(Me!PFrmSubFields.Form![TableName]).Name).Fields(Me!PFrmSubFields.Form!InputField).Properties(15) = True")
-
Me!PFrmSubFields.Form.InputField.FormatConditions(0).ForeColor = vbRed
-
-
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
Phil,
Could it be this: - Me!PFrmSubFields.Form.InputField
might should be: - 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.
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.
@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
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.
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
Cracked it
Created a Function - Public Function RequiredField(TableName As String, FieldName As String) As Boolean
-
-
Dim MyDb As Database
-
Set MyDb = CurrentDb
-
-
On Error Resume Next
-
-
RequiredField = MyDb.TableDefs(TableName).Fields(FieldName).Properties("Required")
-
-
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
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.
I see what you are getting at and have tried - Private Sub Form_Load()
-
-
Dim MyDb As Database
-
Set MyDb = CurrentDb
-
-
On Error Resume Next
-
-
If MyDb.TableDefs(TableName).Fields(FieldName).Properties("Required") = True Then
-
InputField.BackColor = vbRed
-
Else
-
InputField.BackColor = vbYellow
-
End If
-
-
End Sub
All the fields are yellow.
The routine just seems to "Look" at the first control on the subform
Thanks
Phil
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 : - Private Sub Form_Load()
-
Dim lngVar As Long
-
Dim strField As String
-
Dim ctlLbl As Control
-
-
For Each ctlLbl In Me.Detail.Controls
-
With ctlLbl
-
If .ControlType = acLabel Then
-
lngVar = 0
-
On Error Resume Next
-
With .Parent
-
lngVar = .ControlType
-
strField = .ControlSource
-
End With
-
On Error GoTo 0
-
If strField > "" _
-
And (lngType = acCheckBox _
-
Or lngType = acTextBox _
-
Or lngType = acComboBox) Then
-
'We have a valid Control pair.
-
lngColour = IIf(Me.Recordset.Fields(strField).Required _
-
, vbRed, vbYellow)
-
.BackColor = lngColour
-
.Parent.BackColor = lngColour
-
End If
-
End If
-
End With
-
Next ctlLbl
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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 ,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |