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

How to detect all required fields of a form?

I want to indicate requiredness by setting the background color of the
control. Ideally I would call a sub in Form_Load to just do its thing.

Below is what I have so far, but I realize this is of limited use
because both Autonumber fields as well as Primary Key fields not
necessarily have their Required property set, while they are required
for the purpose of the Insert action.

It seems to find out if a field is an Autonumber or PK field can be
complicated.
Does anyone have a brilliant solution?

-Tom.
Code follows:

In the Form_Load event of a form:
SetRequiredFields Me
Public Sub SetRequiredFields(frm As Form)
Dim ctl As Control
Dim rs As DAO.Recordset

Set rs = frm.RecordsetClone

For Each ctl In frm.Controls
If HasProperty(ctl, "ControlSource") Then
if rs.Fields(ctl.ControlSource).Required then
ctl.BackColor = vbRed
End If
Next ctl
Set ctl = Nothing
Set rs = Nothing
End Sub
Private Function HasProperty(ctl As Control, ByVal strPropName As
String)
Dim prop As Property

On Error Resume Next
Set prop = ctl.Properties(strPropName)
HasProperty = (Err.Number = 0)
Set prop = Nothing
End Function
Mar 20 '07 #1
7 3379
Hi Tom,

I never thought of doing it that way.

The method I use is a little different, but does address the 'not
required pk' (Autonumbers don't count-they fill themselves in) as well
as enforcing other form controls to be supplied whether or not the
table requires them. I use the tag property of the control to contain
specifically formated string to store any info I need about the
control. The format looks like
VARIABLE=VALUE;VARIABLE=VALUE;VARIABLE=VALUE (caps not required). I've
created one function that finds a variable and returns the value, and
another that modifies the value or adds if not found. Set one of the
variables to REQUIRED=TRUE and before updating the record(s) cycle
through all controls and see if the ones marked required are filled
in. Any required control not containing a value is added to a string
and the user is prompted for all the ones they missed.

Yours warns them before hand which is nice, but I found this way to be
a little more flexible.

Mar 20 '07 #2
On Mon, 19 Mar 2007 18:26:32 -0700, Tom van Stiphout
<no*************@cox.netwrote:

I just realized the Autonumber portion of my question is no big deal:

blnRequiredField = rs.Fields(ctl.ControlSource).Required Or
(rs.Fields(ctl.ControlSource).Attributes And dbAutoIncrField)

Still struggling with the PK fields though.

-Tom.

>I want to indicate requiredness by setting the background color of the
control. Ideally I would call a sub in Form_Load to just do its thing.

Below is what I have so far, but I realize this is of limited use
because both Autonumber fields as well as Primary Key fields not
necessarily have their Required property set, while they are required
for the purpose of the Insert action.

It seems to find out if a field is an Autonumber or PK field can be
complicated.
Does anyone have a brilliant solution?

-Tom.
Code follows:

In the Form_Load event of a form:
SetRequiredFields Me
Public Sub SetRequiredFields(frm As Form)
Dim ctl As Control
Dim rs As DAO.Recordset

Set rs = frm.RecordsetClone

For Each ctl In frm.Controls
If HasProperty(ctl, "ControlSource") Then
if rs.Fields(ctl.ControlSource).Required then
ctl.BackColor = vbRed
End If
Next ctl
Set ctl = Nothing
Set rs = Nothing
End Sub
Private Function HasProperty(ctl As Control, ByVal strPropName As
String)
Dim prop As Property

On Error Resume Next
Set prop = ctl.Properties(strPropName)
HasProperty = (Err.Number = 0)
Set prop = Nothing
End Function
Mar 20 '07 #3
On 19 Mar 2007 18:50:25 -0700, "storrboy" <st******@sympatico.ca>
wrote:

I have considered such methods, but they require the developer to be
dilligent about fixing up the code (tags) if the database design
changes. My code avoids the possible discrepancies altogether.

Btw, rather than your format, I use the querystring format:
VARIABLE=VALUE&VARIABLE=VALUE&VARIABLE=VALUE
just because it's a commonly used format.

-Tom.

>Hi Tom,

I never thought of doing it that way.

The method I use is a little different, but does address the 'not
required pk' (Autonumbers don't count-they fill themselves in) as well
as enforcing other form controls to be supplied whether or not the
table requires them. I use the tag property of the control to contain
specifically formated string to store any info I need about the
control. The format looks like
VARIABLE=VALUE;VARIABLE=VALUE;VARIABLE=VALUE (caps not required). I've
created one function that finds a variable and returns the value, and
another that modifies the value or adds if not found. Set one of the
variables to REQUIRED=TRUE and before updating the record(s) cycle
through all controls and see if the ones marked required are filled
in. Any required control not containing a value is added to a string
and the user is prompted for all the ones they missed.

Yours warns them before hand which is nice, but I found this way to be
a little more flexible.
Mar 20 '07 #4
Tom van Stiphout <no*************@cox.netwrote in
news:rg********************************@4ax.com:
I want to indicate requiredness by setting the background color of the
control. Ideally I would call a sub in Form_Load to just do its thing.

Below is what I have so far, but I realize this is of limited use
because both Autonumber fields as well as Primary Key fields not
necessarily have their Required property set, while they are required
for the purpose of the Insert action.

It seems to find out if a field is an Autonumber or PK field can be
complicated.
Does anyone have a brilliant solution?
If you are using Access >=2000 and if you are talking single column
primary keys of the underlying tables these might help:

Public Function PrimaryKey$(ByVal TableName$)
PrimaryKey = CurrentProject.Connection.OpenSchema( _
adSchemaPrimaryKeys, Array(Empty, Empty, TableName)) _
..Collect("COLUMN_NAME")
End Function

Public Function IsPrimaryKey(ByVal ColumnName$, ByVal TableName$) As
Boolean
IsPrimaryKey = CurrentProject.Connection.OpenSchema( _
adSchemaPrimaryKeys, Array(Empty, Empty, TableName)) _
..Collect("COLUMN_NAME") = ColumnName
End Function

Public Sub Test()
'northwinds
Debug.Print PrimaryKey("Customers") 'CustomerID
Debug.Print IsPrimaryKey("CustomerID", "Customers") 'True
Debug.Print IsPrimaryKey("CustomerName", "Customers") 'False
End Sub

Then again I do so little in Access these days that I may be too out of
touch to understand your question.

I guess we could modify these for miltiple column primary keys if needed.
Mar 20 '07 #5
On Tue, 20 Mar 2007 02:04:07 GMT, lyle fairfield <ly******@yahoo.ca>
wrote:

Hi Lyle,
Thanks for responding.
I considered such code (perhaps I would use the Indexes collection
rather than OpenSchema), but it assumes that I know the tablename a
control is bound to. What if this is a form with a RecordSource that
is a complex query, and possbly several queries/tables in this query
have a CustomerID field?

-Tom.
>Tom van Stiphout <no*************@cox.netwrote in
news:rg********************************@4ax.com :
>I want to indicate requiredness by setting the background color of the
control. Ideally I would call a sub in Form_Load to just do its thing.

Below is what I have so far, but I realize this is of limited use
because both Autonumber fields as well as Primary Key fields not
necessarily have their Required property set, while they are required
for the purpose of the Insert action.

It seems to find out if a field is an Autonumber or PK field can be
complicated.
Does anyone have a brilliant solution?

If you are using Access >=2000 and if you are talking single column
primary keys of the underlying tables these might help:

Public Function PrimaryKey$(ByVal TableName$)
PrimaryKey = CurrentProject.Connection.OpenSchema( _
adSchemaPrimaryKeys, Array(Empty, Empty, TableName)) _
.Collect("COLUMN_NAME")
End Function

Public Function IsPrimaryKey(ByVal ColumnName$, ByVal TableName$) As
Boolean
IsPrimaryKey = CurrentProject.Connection.OpenSchema( _
adSchemaPrimaryKeys, Array(Empty, Empty, TableName)) _
.Collect("COLUMN_NAME") = ColumnName
End Function

Public Sub Test()
'northwinds
Debug.Print PrimaryKey("Customers") 'CustomerID
Debug.Print IsPrimaryKey("CustomerID", "Customers") 'True
Debug.Print IsPrimaryKey("CustomerName", "Customers") 'False
End Sub

Then again I do so little in Access these days that I may be too out of
touch to understand your question.

I guess we could modify these for miltiple column primary keys if needed.
Mar 20 '07 #6
On Mar 19, 9:12 pm, Tom van Stiphout <no.spam.tom7...@cox.netwrote:
On Tue, 20 Mar 2007 02:04:07 GMT, lyle fairfield <lylef...@yahoo.ca>
wrote:

Hi Lyle,
Thanks for responding.
I considered such code (perhaps I would use the Indexes collection
rather than OpenSchema), but it assumes that I know the tablename a
control is bound to. What if this is a form with a RecordSource that
is a complex query, and possbly several queries/tables in this query
have a CustomerID field?

The field in the recordset you are using contains a SourceTable and
Source Field property. Probably could use those to look through
indexes.

Mar 20 '07 #7
On 19 Mar 2007 19:25:26 -0700, "storrboy" <st******@sympatico.ca>
wrote:

Sweet. That seems to do it!
Thanks,
-Tom.

>On Mar 19, 9:12 pm, Tom van Stiphout <no.spam.tom7...@cox.netwrote:
>On Tue, 20 Mar 2007 02:04:07 GMT, lyle fairfield <lylef...@yahoo.ca>
wrote:

Hi Lyle,
Thanks for responding.
I considered such code (perhaps I would use the Indexes collection
rather than OpenSchema), but it assumes that I know the tablename a
control is bound to. What if this is a form with a RecordSource that
is a complex query, and possbly several queries/tables in this query
have a CustomerID field?


The field in the recordset you are using contains a SourceTable and
Source Field property. Probably could use those to look through
indexes.
Mar 20 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Spanky | last post by:
I am looking for some help with the following. I have a form with many fields. I have a table in the form that I need to make all the fields within the table required. Basic validation, just...
14
by: Oleg | last post by:
Hello there: I've been trying to create two different sets of required fields in one form and to use a radiobutton as sort of a switcher between these sets. In my HTML form there are two...
2
by: Cliff R. | last post by:
Hello, I have a form that has a few required fields and also an "agree to terms" checkbox that must be required. I have used Javascripts for both functions individually, but I need a little help...
2
by: bufbec1 | last post by:
I am pretty good with Access, but do not understand VBA. I have researched this topic and see only VBA answers, so I hope someone can help with my specific question. I have 2 fields for an...
3
by: Orchid | last post by:
Hello All, Hope someone can help me on my required field problems. I have a form base on a table for users to input new Employees. There are 4 fields that cannot be Null when entering new...
1
by: swingingming | last post by:
Hi, I made a form based on one table that has several required fields. If in the form, I leave some of them blank and close the form using 'X', I get 2 warning messages, 1st is "... field is...
1
by: sdavis1970 | last post by:
I am working on an Access 2002 database where one of the tables has five required fields making up the key. There is a form that is linked to this table which is used for adding new records. ...
7
by: samdev | last post by:
I have set up a table with a few fields that are set to required....all work fine except the fields that are Lookup from another table. What am I forgetting to set? Thanks!!
4
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or...
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
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.