Error Passing Arguments, Entry Required Function

G

Guest

I have created a function that is supposed to let the user know that they
must enter data in a field before they move on to the next field. It is a
global function which I have put in a standard Module because I want to use
this on many forms in my database. However, when I try to execute, I get
"Compile Error: Expected: =". Can someone tell me what I am doing wrong?

Function Call:

Private Sub CS_Exit(Cancel As Integer)

func_FieldRequired (Me!CS.Text, Me!CS.Name)

End Sub


Function Code:

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

If IsNull(FieldX) Then
'If it is empty popup appears
msgbox "You must fill in the " & FieldXName & "box.", , "Entry
Required"
'set focus back to Accounting_Reference_No field
Cancel = True
Me.FieldX.SetFocus

End If

End Function


Thank you!
 
G

Guest

I don't know where the compile error is happening, but there are other
problems with your code.
'set focus back to Accounting_Reference_No field
The line below will do nothing or cause an error depending on whether you
have Option Explicit in the module. The Cancel is in the calling procedure
and needs to be assigned there
Cancel = True
This line will fail. FieldX was passed as a string not a control. It is
not necessary to do this here. If you set the cancel in the Before Update
procedure, the focus will not move.
Me.FieldX.SetFocus

Here is a rewrite that may improve things a bit.

Function Call:

Private Sub CS_Exit(Cancel As Integer)

Cancel = func_FieldRequired (Nz(Me!CS,""), Me!CS.Name)
'I removed .Text. It is not necessary and only has value when the control
has the focus. Remember, this is VBA, not VB.

End Sub

Public Function func_FieldRequired(FieldX As String, 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"
'set focus back to Accounting_Reference_No field
Function func_FieldRequired = True
Else
Function func_FieldRequired = False
End If

End Function
 
G

Guest

Very Nice!!! Thank you so much. I am new to VBA (and VB) and I was more or
less guessing at the code. Just one thing. I had to remove the word
"Function" in the If statement where it is set to true or false. I was
getting a compile error with it. Can you tell me why?

Thank you,
 
G

Guest

It could be because some idiot in Texas put it there doing a copy/paste
without watching what he was doing :)

Pretty good guessing, by the way. Stay in these news groups and you will
learn a lot. I learn more than I teach.
 

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