Public Function Runtime Error 2465

sohailcdc

Registered User.
Local time
Today, 15:03
Joined
Sep 25, 2012
Messages
55
Hi there
I am totally new in VBA, just trying to learning during my free time with help you forum, I have a very basic question

I am trying to use Public Function and getting Runtime Error upon execution
Here's is my simple code

Public Function CCname()
Dim cname As String
cname = [Forms]![form1]![me.txtfield1]

End Function

To call i am using "Call CCname"

Upon execution i am getting following Error
Run-time error '2465':
MS Access can't find the field "me.txtfield1" referrer in your expression

And if simply remove "me" from txtfield1 nothing is happen even no error

Any help would be appreciate
 
And if simply remove "me" from txtfield1 nothing is happen even no error
Could it be because you code runs perfect! :D
What would you expect should happen, (because nothing in you code indicate that!)?
 
Please use the code tags (the # button in your post menu) when you post code on the forum

"me" is a reference to the CURRENT Form where this code resides, using public functions inside forms is a big nono. public functions should reside in a module and if it lives there, you cannot use ME

If you call the function, nothing will happen, try doing ?CCName
That should return the value of the text box you are trying to reference.
 
..
If you call the function, nothing will happen, try doing ?CCName
That should return the value of the text box you are trying to reference.
No it wouldn't, look at the code again, (the text control value in the form is set to the variable cname).
 
Thanks, to all but I am still not able to get what I am looking for, I am just trying to learn how to use public function and/or variable not sure I am right or wrong

What I am trying to learn, store the value from form1 and can use anywhere in other forms
Here's is my situation

I have two forms (form1 and form2)
In opening of form1 a text box where I put my name "Sohail"
What I want exactly or I am looking for, that I can display my name "Sohail" in my 2nd form2
therefore, after defining the public function in Module CCName, I am referring CCName in second form2

Private Sub Text29_GotFocus()
Call CCname
Me.Text29 = cname
End Sub

but nothing is appearing in Text29

Thanks for your help in advance
 
sohailcdc,

Here is a link to vba tutorials.

However, more than vba and even Access, I recommend you work through this tutorial on database design. It leads you through a series of steps; identifies tables, attributes and relationships. There is a solution and some design aids in the tutorial.

Good luck
 
..

Private Sub Text29_GotFocus()
Call CCname
Me.Text29 = cname
End Sub
but nothing is appearing in Text29
No - because it is totally wrong what you do.
I've made a simple example for you in the attached database. Open form 1 and form 2. In form 1 type in something in the text control + Return, then in form 2, press the button.
 

Attachments

Users who are viewing this thread

Back
Top Bottom