Functions and Subs

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

Why does this work when placed directly behind the form as an "Expression",
but fails as a "Function"???? The point of failure is the response =
acDataErrContinue, which turns of access system messages.

I currently use this same piece of code several times in the DB, but thought
it would be better calling it as a function from within a module?????

Does it need to be specified as a public function or something, basicly
trying to reference same message several times?? Have search Microsoft and
other sites on the net but no help or luck so far.


----------------------------------------------------------------------------
----

Function getNotInList()

Dim strMsg As String
Dim strTitle As String
Dim intStyle As Integer

strMsg = "You Must Select an Option from the List Available!" &
vbCrLf & "Please See Admin Department for Additions to This Listing."
intStyle = vbOKOnly + vbExclamation
strTitle = "WinZues - Invalid Data Input"
MsgBox strMsg, intStyle, strTitle

response = acDataErrContinue 'Disables system default error
messages
' Response = acDataErrDisplay 'Enables system default error
messages

End Function


----------------------------------------------------------------------------
----

this is same code that sit on the "OnNotInList" property of form control -
this works but used too often


----------------------------------------------------------------------------
----


Private Sub StaffStatus_NotInList(newdata As String, response As Integer)

Dim strMsg As String, strTitle As String
Dim intStyle As Integer
strMsg = "You Must Select a Status from the List Available!" &
vbCrLf & "Please See Admin Department for Additions to This Listing."
intStyle = vbOKOnly + vbExclamation
strTitle = "WinZeus - Invalid Data Input"
MsgBox strMsg, intStyle, strTitle

response = acDataErrContinue 'Disables system default error
messages
' Response = acDataErrDisplay 'Enables system default error
messages

End Sub
 
Response is a By Ref argument of the NotInList event
procedure, it doesn't exist outside that procedure.

You can get what you want by having your function pass the
desired response value back to the NotInList procedure where
it can then be assigned to the Response argument. The code
might be structure along these lines:

Sub somecombo_NotInList(newdata, response)
. . .
Resonse = getNotInList()
. . .
End Sub

Function getNotInList()
. . .
getNotInList = acDataErrContinue 'tell caller what to do
. . .
End Function
 
Hello,

Thanks for your post. According to your description, I understand you want
to know why the function cannot work when it is placed directly behind the
form as a "function". Such as,

Private Sub Form_Click()
Function getNotInList()
MsgBox "test"
End Function
End Sub

If I have misunderstood, please feel free to let me know.

So far as I know, sub procedure is a section of code that carries an
assignment but doesn't give back a result. A function is an assignment that
a piece of code can take care for the functionality of a database. The main
difference between a sub procedure and a function procedure is that a
function can return a value.

Based on my experience, you cannot embed a function definition directly in
an event procedure as your sample code. This action is by design. You can
define a function outside the event procedure and call it in this
procedure.

Thanks for using MSDN newsgroup.

Regards,

Michael Shao
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top