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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm Sure I just missed something??

Status
Not open for further replies.

NewCoder2

Programmer
Apr 12, 2004
25
0
0
US
Hey everyone, I know I should know this, but I'me either having a brain cramp or I've missed something. So..... I have a first function that calls a second function and in the second function I set a return value and send it back to the first function. <whew>.... So I know if the second function worked or errored out. In my second function I was doing this:DoCmd.TransferSpreadsheet import, then DoCmd.TransferText export, then returnvalue = true or false depending. This actually worked well, however I had new requirements to meet, so my import now consist of basic ADO stuff, and my export is basic FSO stuff. All of which work, but I now get an auotmation error 440 to be exact, that says my call failed, but in reality it actually works. I guess this would be a good place to show the call from function 1 to function 2 , it is: returnvalue = function2.Run("functionname", parameter, parameter). So in writing this, I guess I really have 2 questions, 1)how do you set and return a result from one function to another. 2) how do you call one function from another function?
I know this was long winded, any thought's would be mighty welcome. Thanks, tons!!
 
Call FunctionName(parameters) calls a function from within a function

Returning values example

Function SomeFunction() as Boolean

on error goto err_h
'run code

'code worked
SomeFunction=True
Exit Function

err_h:
'didn't work
SomeFunction=False

End Function
Look at Function in the help file for better explanation

 
Would it still work if I'm calling from one database to another?

Also you don't think it has anything to do with the automation call?

Thanks, this is very helpful.
 
Hi,

if your calling from one db to another do you have it set up (the other) as a reference in the calling db? then you can prefix the function call with the referenced db name if there is a function with the same name in the calling db - i.e. refdatabasename.function2(parameters)

HTH, Jamie
 
Hey Jamie, yes, I think I'm already doing that , this is my call:
function2.Run("functionname", parameter, parameter).

function2 <-->here represents my new access application with path.

So is it correct to use the Run command, or should I be using something else?

Thanks for all the comments.
 
Hi,

what I have done is set a database to reference another database (Tools\References\Browse\select *.mdb). Once this is done I use it like any other DLL, for example...
Code:
Sub DummySub
dim bDummy as boolean

bDummy = OtherDBName.OtherModuleName.ForeignFunction(paramters)
End Sub
is this what your after? Jamie
 
Ok, That's not how I was doing it, but that works just as nicely, however Now I'm back to the original issue of being able to produce a result back in function 1 of database 1?

Thanks , this is getting pretty good!!
 
Hi, you say that you now have to include some ADO stuff... Are you using something like set cn = currentProject.Connection? then it will be setting it to db1 not db2 (referenced project) which may be causing the errors... HTH, Jamie
 
Ok, I was incorrect in saying that. I'm not having errors, just not able to tell function 1 in db1 that function 2 in db2 has completed successfully or failed.

How would I send some type of return result back to function1 in db1?

Thanks
 
Hi, if the function in db2 has the same structure as vbajock outlined above then it will return true when it has completed successfully or false if unsuccessful. Using the syntax from my previous post would set bDummy to the result of the function in db2...

To demonstrate - in db2 paste into a module
Code:
Public Function TestResult() As Boolean
Dim nRetVal As Long

nRetVal = MsgBox("Press a button to return a result", vbOKCancel)

If nRetVal = vbOK Then
    TestResult = True
Else
    TestResult = False
End If

End Function
in db1 create a reference to db2 and paste
Code:
Sub TestDB2Function()
Dim b As Boolean

b = db2.TestResult

Debug.Print "db2 function returned " & b

End Sub
running this code in db1 this shows that db1 is waiting for you to press a button from the db2 macro in order to continue running... is this helping? perhaps if you posted your code? Jamie
 
Based on what vbajock started this is what I have:

Function1DB1()
Dim ReturnValue As Boolean
Set db2= New Access.Application
db2.OpenCurrentDatabase (strDBName)
on error goto err_h
ReturnValue = db2.Run("function2DB2",parameters1&2)
(I never get here) Just goes to the error

If ReturnValue= true
msgbox("It worked")
exit Function
Else
goto err_h
End If

err_h:
'didn't work
SomeFunction=False
Exit Function
End Function


function2DB2(parameters1&2)
Do some ADO stuff <-- Works great
Do some FSO stuff <-- Works great
I close things down, then I
End Function

Then I get Error 440 method 'RUN' of object '_Application' failed.

Which leads me to believe this is an automation error in my call the the 2nd db & function. However I'm new at this and unsure.

Sorry for the length of this. I really appreciate the help.
 
Never Mind, I figured it out.
Basically I'm not only new to coding, I'm new to typing as well. Plain & simple, typo on my part......Thanks for the help and putting up with my questions.
 
oh well, glad you got it sorted anyway...

HTH, Jamie
[deejay]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top