473,830 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
SetRequiredFiel ds Me
Public Sub SetRequiredFiel ds(frm As Form)
Dim ctl As Control
Dim rs As DAO.Recordset

Set rs = frm.RecordsetCl one

For Each ctl In frm.Controls
If HasProperty(ctl , "ControlSource" ) Then
if rs.Fields(ctl.C ontrolSource).R equired 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 3407
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:

blnRequiredFiel d = rs.Fields(ctl.C ontrolSource).R equired 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:
SetRequiredFie lds Me
Public Sub SetRequiredFiel ds(frm As Form)
Dim ctl As Control
Dim rs As DAO.Recordset

Set rs = frm.RecordsetCl one

For Each ctl In frm.Controls
If HasProperty(ctl , "ControlSource" ) Then
if rs.Fields(ctl.C ontrolSource).R equired then
ctl.BackColo r = 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******@sympa tico.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.c om:
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$(ByV al TableName$)
PrimaryKey = CurrentProject. Connection.Open Schema( _
adSchemaPrimary Keys, Array(Empty, Empty, TableName)) _
..Collect("COLU MN_NAME")
End Function

Public Function IsPrimaryKey(By Val ColumnName$, ByVal TableName$) As
Boolean
IsPrimaryKey = CurrentProject. Connection.Open Schema( _
adSchemaPrimary Keys, Array(Empty, Empty, TableName)) _
..Collect("COLU MN_NAME") = ColumnName
End Function

Public Sub Test()
'northwinds
Debug.Print PrimaryKey("Cus tomers") 'CustomerID
Debug.Print IsPrimaryKey("C ustomerID", "Customers" ) 'True
Debug.Print IsPrimaryKey("C ustomerName", "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$(ByV al TableName$)
PrimaryKey = CurrentProject. Connection.Open Schema( _
adSchemaPrimar yKeys, Array(Empty, Empty, TableName)) _
.Collect("COLU MN_NAME")
End Function

Public Function IsPrimaryKey(By Val ColumnName$, ByVal TableName$) As
Boolean
IsPrimaryKey = CurrentProject. Connection.Open Schema( _
adSchemaPrimar yKeys, Array(Empty, Empty, TableName)) _
.Collect("COLU MN_NAME") = ColumnName
End Function

Public Sub Test()
'northwinds
Debug.Print PrimaryKey("Cus tomers") 'CustomerID
Debug.Print IsPrimaryKey("C ustomerID", "Customers" ) 'True
Debug.Print IsPrimaryKey("C ustomerName", "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******@sympa tico.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
2562
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 need to make sure they are not blank. Here is the kicker.... I have a javascript that adds rows and fields whenever the user clicks a button for add rows. So, the user clicks button and I add a row with about 5 fields. Every time a user clicks...
14
2236
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 radiobuttons with values 'Via Email' and 'Printed Brochure'.
2
1871
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 in combining them. Can someone help me combine these two scripts so they both work on the same page, with <FORM METHOD=POST onSubmit="return checkrequired(this)" etc., checking for required fields and making sure the agree-to-terms button is...
2
39125
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 end-user that must be filled in. I want an error message for each field. The forms are already partially filled in, and a user needs to select which record to go to. There are fields for them to fill in their initials and date , along with other...
3
2941
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 records. (but on the table, I have these 4 field set up as "Required = No"). I want the codes to be able to check if the 4 fields are null, if is null, tell the user to input the null field(s), after the user input the required field, then ask if...
1
1951
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 required." 2nd is "if you close, the data will be lost." How can I get rid of these or, if possible, I would like to replace them with my own. I tried Form Close event, doesn't work. Anyone could help me here? Thanks in advance. ming
1
3044
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. There is a close button on this form that has the following code in it's OnClose event: "DoCmd.close"
7
2593
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
6477
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 nearly so). I researched this and for C# (as opposed to MFC) there is no library function, and no easy way, though some code on the net suggested that you set up a thread that 'lives' for a certain time, then, if keys are pressed in that certain...
0
9640
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10768
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10475
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10196
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7739
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5614
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5774
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.