IF Function is successful then.......

B

Bre-x

Hi

How do I know if a function has been susscessfully executed?

Sub doit()
Responce = myfunction("my msg")
if Responce = "OK" then msgbox("OK")
end sub

Function myfunction(mymsg as string)
msgbox(mymsq)
End Function

Thank you all
 
J

John W. Vinson

How do I know if a function has been susscessfully executed?

Rewrite it just a bit so that the value of the function itself tells you:

Sub doit()
myfunction("my msg")
if Responce = "OK" then msgbox("OK")
end sub

Function myfunction(mymsg as string) As String
Dim iAns As Integer
iAns = msgbox(mymsq, vbOKCancel)
If iAns = vbOK Then
MyFunction = "OK"
End If
End Function


Step back a bit though. What are you trying to accomplish? You're calling
Msgbox in order to... call Msgbox again? Have you studied the VBA help for
Msgbox? It has a number of options which might let you do what you want with
only a single Msgbox call.
 
B

Bre-x

The msg function was just an example.
I suppose to know if a function was "successful" depends on what the
function actually does right?
There is no a universal way to know if the function was successful?

What I am trying to do is a Switchboard for my many ms access apps, some
excel and intranet websites
I was thinking that it would be usefull to keep track how many times a app
is use it. So if a app was suscessfully open increase the counter.

Here is my function and how i call it.

Thanks for you help!!!!


'SUB---------------------------------------------------------------------------------------
Sub List2_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo Err_List2_KeyDown
Dim uname As String
Dim sql As String
Dim thepath As String
If KeyCode = 13 Then
uname = VBA.Environ("USERNAME")
sql = "select count(*) as CC from sys_swsub where sys_id = " & Me.SYS_ID & "
AND sys_user = '" & uname & "'"
CurrentDb.QueryDefs("INTRANET").sql = sql

Select Case DLookup("[CC]", "INTRANET")
Case 0
Responce = MsgBox("Access Denied. Please contact your Sys Admin. ",
vbCritical, " CORP")
Exit Sub
Case Else
Responce = open_app(Me.sys_app, Me.sys_path, Me.sys_ext)
'MsgBox thepath
End Select
End If

Exit_List2_KeyDown:
Exit Sub
Err_List2_KeyDown:
If Err.Number = 3146 Then
Responce = MsgBox("Appliction Error. Please contact your sys
admin. ", vbCritical, " CORP")
Exit Sub
End If
Resume Exit_List2_KeyDown
End Sub

'FUNCTION----------------------------------------------------------------------------------------
Public Function open_app(theapp As String, thepath As String, theext As
String)
On Error GoTo Err_open_app
Dim ie As Object
Dim uname As String
Dim thepathapp As String
'----------------------------------------------
' Open Application
'----------------------------------------------
uname = VBA.Environ("USERNAME")
thepathapp = thepath & uname & theext

Select Case theapp
Case "MSACCESS"
Call Shell("msaccess.exe """ & thepathapp & """", 1)
DoCmd.Quit
Case "MSEXCEL"
Call Shell("excel.exe """ & thepathapp & """", 1)
DoCmd.Quit
Case "MSWORD"
Call Shell("word.exe """ & thepathapp & """", 1)
DoCmd.Quit
Case "IE"
Set ie = CreateObject("InternetExplorer.Application")
ie.AddressBar = False
ie.MenuBar = True
ie.Toolbar = True
ie.Width = 1024
ie.Height = 768
ie.Left = 0
ie.Top = 0
ie.navigate thepath
ie.resizable = True
ie.Visible = True
Set ie = Nothing
DoCmd.Quit
Case Else
Responce = MsgBox("Application Error.", vbCritical, " UMCORP")
Exit Function
End Select

Exit_open_app:
Exit Function
Err_open_app:
MsgBox Err.Description
Resume Exit_open_app
End Function
 
J

John W. Vinson

The msg function was just an example.
I suppose to know if a function was "successful" depends on what the
function actually does right?

Of course.
There is no a universal way to know if the function was successful?

Ummm... No.

Can you define "successful" in a way that applies to every imaginable
function?
What I am trying to do is a Switchboard for my many ms access apps, some
excel and intranet websites
I was thinking that it would be usefull to keep track how many times a app
is use it. So if a app was suscessfully open increase the counter.

Here is my function and how i call it.

Without digging through all your code, you can certainly define the function
to return a Boolean value:

Public Function MyFunc(<arguments>) As Boolean
<a bunch of code>
<you decide it was successful>
MyFunc = True
<you decide that it failed>
MyFunc = False
<perhaps other code>
End Function

You can then - in the calling environment - simply check the value returned by
the function:

If MyFunc(this, that, theother) Then
<it was successful>
Else
<do something else, it failed>
End If
 

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