calling a function from a button

C

CES

All,
I ask some really stupid questions but this is probably the stupidest...
I am trying to call a function from a button on a form however when I try to execute the code below I receive a compiler error - syntax error. I'm really sorry for the stupid question, but if anyone could give me some advice and guidance I would appreciate it. Thanks in advance. - CES

'Form Action
Private Sub Button1_Click()
fnTMP()
End Sub

'Function Called
Public Function fnTMP()
MsgBox(Now())
End Function
 
M

missinglinq via AccessMonster.com

You need to drop the parenthesis after the function name. Change your code to:


Private Sub Button1_Click()
fnTMP
End Sub

The parenthesis tells Access that the function will return a value, and when
returning a value you have to use the structure

SomeVariable = fnTMP()

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 
C

CES

missinglinq said:
You need to drop the parenthesis after the function name. Change your code to:


Private Sub Button1_Click()
fnTMP
End Sub

The parenthesis tells Access that the function will return a value, and when
returning a value you have to use the structure

SomeVariable = fnTMP()

But what if you need to pass values into the function??... in any event I found a way to doit using Call Thanks for the help - CES

Private Sub Button1_Click()
Call fnTMP("temp", "Field1 Text")
End Sub
 
M

missinglinq via AccessMonster.com

And what if pigs fly? If you need to pass values your new code is correct,
just as I pointed out in my reply. Passing values WASN'T included in your
original post, which was why your code:

Private Sub Button1_Click()
fnTMP()
End Sub

failed! Your original question was why did it fail!

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 

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