Option Selection Required

E

Emma Aumack

I am trying to require that users make a selection from an option frame by
using the following code in the On Exit event of the option frame. If an
option is selected it put either a 1 or 2 inthe LOC_IndIDN field:

Private Sub Frame_IndorIDN_Exit(Cancel As Integer)

Cancel = func_FieldRequired(Nz(Me!LOC_IndIDN, 0), Me!LOC_IndIDN.Value)

End Sub


In Module1 is the following code for func_FieldRequired:


Public Function func_FieldRequired(FieldX As Integer, FieldXName As String)
As Boolean

If Len(FieldX) = 0 Then
'If it is empty popup appears
msgbox "You must fill in the " & FieldXName & " box.", , "Entry
Required"

func_FieldRequired = True
Else
func_FieldRequired = False
End If

End Function

I keep getting "Invalid use of null" error.

What is wrong with my code?
 
K

Klatuu

First, move the code to the Before Update event. And, you want the name of
the control not the value according to your function. One way would be:
Me!LOC_IndIDN.Name, but why not just use "LOC_IndIDN"?
Private Sub Frame_IndorIDN_BeforeUpdate(Cancel As Integer)

Cancel = func_FieldRequired(Nz(Me!LOC_IndIDN, 0), "LOC_IndIDN")

End Sub

Now in this function there is a problem. FieldX is an integer. You can't
check the length of a numeric value. You need to see if it is in range:

Public Function func_FieldRequired(FieldX As Integer, FieldXName As String)
As Boolean

If FieldX < 1 Or FieldX > 2 Then
'If it is empty popup appears
msgbox "You must fill in the " & FieldXName & " box.", , "Entry
Required"

func_FieldRequired = True
Else
func_FieldRequired = False
End If

End Function

And the last issue. To ensure a user actually selects a value, you will
need to set the Default Value property to either Null or 0. But, in the
final analysis, this still won't protect you, because if the user never
enters the field or changes the value, it wont trap the error. You should
actually be using the Form's Before Update event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top