Variable's value doesn't store in function (1 Viewer)

born2live

Registered User.
Local time
Today, 23:28
Joined
Jul 19, 2012
Messages
26
Hi,
I defined a public variable and then for being able to filter a query results, assigned it to a public function. The problem is that function doesn't get the variable's value.:banghead:

Any helps will be appreciated
Thanks :)
 

Isskint

Slowly Developing
Local time
Today, 19:58
Joined
Apr 25, 2012
Messages
1,302
A little more detail would help us to help you. Can you post the code containing the Public variable declaration and the function code?
 

born2live

Registered User.
Local time
Today, 23:28
Joined
Jul 19, 2012
Messages
26
Dear Isskint,
the code comes as follow:

in the module:
Public PartNo As String

Public Function FilterPartNo()
FilterPartNo = PartNo
End Function
------------------
in the form:
Private Sub txtPartNo_Click()
Me.Refresh
PartNo = [Forms]![frmCompareCurrentAndPreviousCondition]![txtPartNo]
End Sub
***
In the form variable gets the value, but the function (FilterPartNo) doesn't get the stored variable.
 

JHB

Have been here a while
Local time
Today, 20:58
Joined
Jun 17, 2012
Messages
7,732
You need use a module to set the value.
 

RainLover

VIP From a land downunder
Local time
Tomorrow, 04:58
Joined
Jan 5, 2009
Messages
5,041
Dear Isskint,
the code comes as follow:

in the module:
Public PartNo As String

Public Function FilterPartNo()
FilterPartNo = PartNo
End Function
------------------
in the form:
Private Sub txtPartNo_Click()
Me.Refresh
PartNo = [Forms]![frmCompareCurrentAndPreviousCondition]![txtPartNo]
End Sub

***
In the form variable gets the value, but the function (FilterPartNo) doesn't get the stored variable.

Have you tested any of the results.

You can do this simply by placing a Message Box where you need to prove that you have a value and what that value is.
 

vbaInet

AWF VIP
Local time
Today, 19:58
Joined
Jan 22, 2010
Messages
26,374
What is the name of the field bound to txtPartNo? Look in the Control Source property for txtPartNo.
 

born2live

Registered User.
Local time
Today, 23:28
Joined
Jul 19, 2012
Messages
26
Have you tested any of the results.

You can do this simply by placing a Message Box where you need to prove that you have a value and what that value is.

You know, I have a form in which there are many records while the PartNo field is unique for each record. On the other hand there is a query with more details about PartNo. I want by clicking on a button (in the form), those mentioned details be shown only for the clicked record (which refers to only one 'PartNo'). The written function is for getting that PartNo to use in the query's criteria part.

RainLover, you're right: I had used Msgbox command to check the results. First Msgbox in the form works and shows the related PartNo, but second Msgbox in the function shows nothing and it's blank.
 

vbaInet

AWF VIP
Local time
Today, 19:58
Joined
Jan 22, 2010
Messages
26,374
You cannot use the same name "PartNo" as the name of your variable. Call it:

Public strPartNo As String
 

DavidAtWork

Registered User.
Local time
Today, 19:58
Joined
Oct 25, 2011
Messages
699
"In the form variable gets the value, but the function (FilterPartNo) doesn't get the stored variable"
But how/when are you using/calling the function FilterPartNo?

David
 

born2live

Registered User.
Local time
Today, 23:28
Joined
Jul 19, 2012
Messages
26
What is the name of the field bound to txtPartNo? Look in the Control Source property for txtPartNo.

Aha! You guessed correctly vbaInet. Yeah, the control's name is PartNo. Is it the problem?
 

born2live

Registered User.
Local time
Today, 23:28
Joined
Jul 19, 2012
Messages
26
You cannot use the same name "PartNo" as the name of your variable. Call it:

Public strPartNo As String

Yeah, it came in handy!
Thank you so much vbaInet. Your're a genius. :)
 

vbaInet

AWF VIP
Local time
Today, 19:58
Joined
Jan 22, 2010
Messages
26,374
There are 4 other greater geniuses on this thread. ;)
 

RainLover

VIP From a land downunder
Local time
Tomorrow, 04:58
Joined
Jan 5, 2009
Messages
5,041
vbaInet

You are too modest.
 

RainLover

VIP From a land downunder
Local time
Tomorrow, 04:58
Joined
Jan 5, 2009
Messages
5,041
Is there really a need for the public variable.

It looks to me that the value only gets used once.

Am I right wrong or indifferent.
 

vbaInet

AWF VIP
Local time
Today, 19:58
Joined
Jan 22, 2010
Messages
26,374
Is there really a need for the public variable.

It looks to me that the value only gets used once.

Am I right wrong or indifferent.
The poster could have done:
Code:
Public Function FilterPartNo()
    FilterPartNo = [Forms]![frmCompareCurrentAndPreviousCondition]![txtPartNo]
End Function
But I think that they wanted to keep everything within scope and perhaps that variable is going to be used elsewhere as well.
 

RainLover

VIP From a land downunder
Local time
Tomorrow, 04:58
Joined
Jan 5, 2009
Messages
5,041
This is all that is required as far as I can see.

Code:
 FilterPartNo = [Forms]![frmCompareCurrentAndPreviousCondition]!PartNo]

Why could one possibly need more.
 

Users who are viewing this thread

Top Bottom