Function not being called (1 Viewer)

AJJJR

Registered User.
Local time
Today, 06:16
Joined
Mar 19, 2018
Messages
56
I have a function call that does not execute in some instances. The function and the variable defining it are on a standard module:

Public strTitle as String
_____________________________________________________________

Public Function Title()
Title = strTitle
End Function

In the onload event of several forms I have a line of code that sets the value of a textbox to Title

Me.txtTitle = Title

The value of strTitle is set before arriving at the form.

Sometimes it works fine. When I step through the code, at the line Me.txtTitle = Title, the code jumps to the Function on the standard module and sets the value of title to the value stored in strTitle, It comes back and sets Me.txtTitle to that value which then displays the title of the form.

The problem is that in other instances the code just jumps over me.txtTitle=Title and doesn't call the function.

It would be greatly appreciated if someone could explain why this would happen.

Thanks for your time.
 

Dreamweaver

Well-known member
Local time
Today, 13:16
Joined
Nov 28, 2005
Messages
2,466
The strTitle would need to be form level or global in scope I.E

Add below Option Explicit in the form for form level of same place in a module for global
 

AJJJR

Registered User.
Local time
Today, 06:16
Joined
Mar 19, 2018
Messages
56
Hi MickJav

I'm a self-taught in access person so I'm having trouble following your answer due to the fact that I don't get a lot of the jargon.

I have a separate standard module where I declare the variable and the function. So, below Option Explicit I have the line:

Public strTitle as String

and then below the line under Option Explicit I have the function:

Public Function Title() as String
Title = strtitle
End Function

Works fine sometimes but, like i said other times it just does not get called
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:16
Joined
Sep 21, 2011
Messages
14,041
Shouldn't that be

Code:
Me.txtTitle = Title()

However I am confused, as the same issue was in your other thread.
If you have a public variable strTitle, why not just use that.? :confused:

Code:
Me.txtTitle = strTitle
 

Minty

AWF VIP
Local time
Today, 13:16
Joined
Jul 26, 2013
Messages
10,354
Which form events are you using? Ideally, it should (99% of the time) always be On Load, not on Open, as the open event happens before the form data is loaded, and can give some weird results.
 

Dreamweaver

Well-known member
Local time
Today, 13:16
Joined
Nov 28, 2005
Messages
2,466
Hi MickJav

I'm a self-taught in access person so I'm having trouble following your answer due to the fact that I don't get a lot of the jargon.

I have a separate standard module where I declare the variable and the function. So, below Option Explicit I have the line:

Public strTitle as String

and then below the line under Option Explicit I have the function:

Public Function Title() as String
Title = strtitle
End Function

Works fine sometimes but, like i said other times it just does not get called


You need to check you are assignng a value to strTitle as once set it should remember it upless you get an error where are you setting it?
 

AJJJR

Registered User.
Local time
Today, 06:16
Joined
Mar 19, 2018
Messages
56
Hi GasMan

In the other thread I was trying to ensure that I was writing the function correctly and that was resolved.

Me.Title=Title does work, but like I said sometimes the function does not get called, that's my problem.

As for using Me.Title = strTitle .......Duh! I somehow didn't think of that. I'll try that now and see what happens.

Thanks for the reply



There's nothing you can do about yesterday
But you can really screw up today
By worrying about tomorrow
 

AJJJR

Registered User.
Local time
Today, 06:16
Joined
Mar 19, 2018
Messages
56
Using Me.Title = strTitle works! I got sidetracked because I'm also using Title() in a query and just sort of got stuck on it.

However, I'm still curious as to why sometimes the line

Code:
Me.Title = Title

does not get called and sometime it does.

Thanks to all of you who helped and especially Gasman who came up with the answer.

A/R
 

AJJJR

Registered User.
Local time
Today, 06:16
Joined
Mar 19, 2018
Messages
56
OnOpen was a mistake the call to the function is in OnLoad.
 

AJJJR

Registered User.
Local time
Today, 06:16
Joined
Mar 19, 2018
Messages
56
MickJav

That's the wierd thing about it. I am setting strTitle. In fact the code for all the forms is exactly the same as it's each form is a copy of a template. Sometimes the line:
Me.txtTitle = Title

calls the function and sometimes it does not
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:16
Joined
Feb 28, 2001
Messages
26,998
The issue you describe in post #11 of this thread is USUALLY (not always) a matter of scope. For instance, in the situation where the function does NOT get called, see if there is a variable or control named Title that is within the same class module. The rule is that when you name something, Access VBA will try to find it.

1st, locally within the same code entity (sub or function)
2nd, anywhere else in the same class module
3rd, in some other module

Your desire for executing your Title() function is always 3rd on the list. Be sure that there is no chance for #1 or #2 to kick in.
 

Users who are viewing this thread

Top Bottom