Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make a form field Required

Status
Not open for further replies.

MitchJP

MIS
Feb 17, 2004
43
US
Hello,

I have an Access form (developed with Access 2000) and I need to make certain fields Required based upon what choices the user makes in another field. So, they choose via a series of radio buttons, and depending on which option they choose, certain fields need to be made required. How is this done?

Thanks!
MitchJP
 
The way I would accomplish this is thru code.

On the after update event of the radio buttons I would set a variable for the field.

Example:

Option Explicit

Dim intFieldReq as Integer

intFieldReq = 0

Private Sub RadioButton_AfterUpdate()
intFieldReq = 1
End sub


Then when the user tries to close the form (using a close command button) have the form check to see if the field required has data

Example:

Private Sub CommandButton_OnClick()

If (intFieldReq = 1 And IsNull(me.FieldReq)) or (intFieldReq=1 and me.FieldReq = "") Then
'the field has no data in it.
'put your code to force user to put data in field
Else
DoCmd.Close
End if

End Sub


HTH

Mike
 
How are ya MitchJP . . . .

Since you have an variable result of required fields, you can't simply set the Required Property for each field of interest, as one secnario may be require some fields, while another may not.

Somewhere before the record is changed, you need to have code that checks the fields for the current secnario. The code could be run after the user hits a submit button for instance, or moves to a new record.

TheAceMan [wiggle]

 
Thanks all!

Yeah, there is no submit button on the form, they just page thru the records so-to-speak. So I can add some code to check which radio button thery selected, and then to check if the required fields are completed? As you said the code would run when they try to change records or close the form/database.

How do I check to see if they changed records or close the DB? (srry, I'm new to this)

I'll give it a try.

Thanks again!
MitchJP
 
MitchJP . . . . .

Have meetings this afternoon so won't be able to post till later this evening or tomorrow.

Question: I seem to detect you would perfer the form was data entry only! Am I correct in this?

Will get back with a full critique as soon as I can . . .

TheAceMan [wiggle]

 
No, the form as it is is only data entry, but the users want some business rules incoporated into it, such as required fields. BUT, the required fields are only required if they change another field, say, to YES.

I think I could add code so that the form cannot be closed or the record cannot be changed if one of the required fields is not NULL or an empty string. But how do I do this?

Thanks and TTYL,
MitchJP
 
One event that will always fire when there's changes to a record, is the before update event of the form (this will fire if one tryes to close the form with unsaved date, move to another record with unsaved data...). It is also possible to cancel that event, so that the user can either cancel the changes or amend the record. Here's a little sample testing for the value in an option group (selected option), and performing isnull checks on different controls.

[tt]Private Sub Form_BeforeUpdate(Cancel as Integer)
Select Case Me!MyFrame.Value
Case 1
if isnull(Me!txt1.Value) or isnull(Me!txt2.Value) then
msgbox "bla bla bla"
cancel=true
end if
Case 2
if isnull(Me!txt3Value) or isnull(Me!txt4.Value) then
msgbox "bla bla bla"
cancel=true
end if
' Continue testing...
End Select
End Sub[/tt]

- typed not tested, when closing, you'd probably get an "unable to save" Yes/no box (which perhaps might also be avoided, but do check out if this migh bring you any closer...

Roy-Vidar
 
Well I tried some code and it prompts me when the conditions are met ("blah blah blah") but even after I meet the conditions (fill out of the required fields) it still generates the same prompt and won't let me get past the record...

What should "MyFrame" be in my code? The name of the Form, or Field that triggers this code? I assume the field...

TY,
MitchJP
 
From your initial post So, they choose via a series of radio buttons, and depending on which option they choose, this is what triggers my reply Here's a little sample testing for the value in an option group (selected option)

Usual way of using sevaral option buttons, is using an option group, which is often referred to as a frame (my name Me!MyFrame.Value), where the value of the frame (option group) reveals the "user choice".

And yes, I used the frame thingie as the criteria thingie.

In your last reply you speak of a field, but you haven't provided any control names (and my crystall ball is a little clouded at the moment;-)), so my snippet is just an example you'll have to tweak a little to make it work.

If you have only two options, you might use a if statement, the select case is a good option if there are more than two possible values. So try and replace it with your criteria control, using your values and see how it goes.

Posting your actual code, control names, what you want to check might be an option, if you're still having problems (as you perhaps might see, we're all just guessing and assuming...).

Roy-Vidar
 
MitchJP ? . . . . . . .

Sorry I could'nt get back earlier. Not only were the meetings a bore, but my evenings are taken up by my children (TheAceMan is a single father of 3).

Code:
The following synopsis is based on the following criteria:
**********************************************************
   1) Since your form is already setup for Data Entry, we will lock the form to that
      function only. (No skiming of added records. I don't believe you want users
      fooling around with previously saved data!).
   2) Provide the schema for switching between Data Entry Only and Viewing All Records.
      This provides full access to the administrator.
   3) Provide the schema for detection of the radio buttons.
   4) Provide the schema for detection of required fields.


Locking The Form In Data Entry. Assume the Data Entry Property is already set to yes.
If not, make it so.
-------------------------------------------------------------------------------------
   a) Open the form in design view and open the Properties window. You can always do
      this by double-clicking the small black box, just to the left of the ruler. This
      always brings up the Form Properties.
   b) On the Format tab, set Navigation Buttons to "No". This prevents skiming added
      records.
   c) On the Other tab, set Cycle to Current Record. This prevents the focus from 
      spilling over to the previous or next record. Data entry is now locked! Moving to       an additional new record will be up to the Submit command button, or any schema of       your choice.

Switching between Data Entry Only and Viewing All Records:
----------------------------------------------------------
   a) In a module in the module window, add the following code:
              
      Public Function IsOpenFrm(frmName As String) As Boolean
         Dim cp As CurrentProject, Frms As Object
         Set cp = CurrentProject()
         Set Frms = cp.AllForms
         If Frms.Item(frmName).IsLoaded Then IsOpenFrm = True
         Set Frms = Nothing
         Set cp = Nothing
      End Function

      Public Function TglViewMode()
         Dim frm As Form
   
         If IsOpenFrm("YourFormName") Then
            Set frm = Forms!YourFormName
      
            If frm.DataEntry Then
               frm.DataEntry = False
               frm.NavigationButtons = True
            Else
               frm.DataEntry = True
               frm.NavigationButtons = False
            End If
       
            Set frm = Nothing
         End If
   
      End Function
      
      Note: Fill-in YourFormName as required in the function TglViewMode.

   b) In the Macros window, open new. In the Macro Name column, add the following:
         ^+V
      Thats a caret, plus symbol, and capital V. These are the hotkeys (Ctrl+Shift+V)
      for toggling between Data Entry & All View. In the Action column select RunCode.       In the Function Name on the bottom left, enter TglViewMode(). On the next Action       line select StopMacro. Close the macro and name it AutoKeys. Open the form and       test the keys! You can of course call the TglViewMode function by any schema you       like. Just remember its a toggle.

Detection of the radio buttons
------------------------------
   a) The Idea here is to pack a byte with the status of each radio button (each button
      represents a bit in the byte), resulting in a count that you can easily step thru       and detect Required Fields accordingly. Lets say for the moment we have three           radio buttons named RB1, RB2, RB3. The code would go a little like this:
         
         Dim rbStatus as Integer
         
         If RB1 then rbStatus = rbStatus Or 1
         If RB2 then rbStatus = rbStatus Or 2
         If RB3 then rbStatus = rbStatus Or 4

      Now you can increment through all the possible combinations.

   b) There's a point Id like to make here. You might want to consider adding a field to          your table to contain this byte. You have no way of knowing how the radio buttons
      were set in a previously saved record! Saving this byte you could preset the 
      buttons accordingly!
 
Detection of required fields
----------------------------
   From here I think you have the picture. An If-ElseIf-EndIf statement is used to pick
   rbStatus apart, and checks Required Fields accordingly. I would add message prompts for    any fields that fail.

Last, is the incorporation of a Submit button. Here the code is run. If required fields
are ok, then you save the current record and move to a new one, else you set the focus
to the first Required Field that fails after the message prompt.

Hope this helps . . . . . . . . . .

TheAceMan [wiggle]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top