Avatar of KnowledgeWare
KnowledgeWare

asked on 

How do I pass a reference to textbox control?

Hi,

I'm trying to do something which I feel should be very simple: pass a reference to a textbox control into function. I want to do something very much like this:

------------------------------------------------------------------------
Public Sub DisplayText(txtBox As Textbox, text As String)
      txtBox.Text = text
End Sub

Public Sub Button1_Click()
     DisplayText(Me.TextBox1, "Hello World")
End Sub
------------------------------------------------------------------------

As you'd imagine, I'd expect that to make TextBox1 say "Hello World' when I click Button1, but it's doesn't. The problem is that Me.TextBox1 returns the default value of the textbox, not the textbox itself, so I'm passing in two strings to DisplayTextBox - whatever the user entered in Textbox1 and "Hello World", and this results in an error.

I've been searching and searching but I can't find a way to pass a reference to the actual textbox control. I've tried:

-Me![TextBox1]
-Forms![MyForm]![TextBox1]
-Me.Controls.Item("TextBox1")

and a bunch of other stuff. Is there a way?
Microsoft AccessMicrosoft Applications

Avatar of undefined
Last Comment
shambalad
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

'calling code
SomeValue = fnYourFunction(Me.TextboxName)

'The function
Public Sub fnYourFunction(sTextBoxName as String)
'do your stuff
End Sub

It is not necessary to pass the TextBox object, just it's value as a string.
Avatar of KnowledgeWare
KnowledgeWare

ASKER

Except that "fnYourFunction" is in a code module and not on the same form. Sorry, should've made that clear.

Anyways, I solved that by having my function take in the name of the form and the name of the textbox as separate parameters and referencing it that way.

Now I have another problem.... I need an unbound textbox that could display as many as 15000 lines of text. Basically infinite.
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>Except that "fnYourFunction" is in a code module and not on the same form.
No problem.  Declare a function/sub with Public, it can be used anywhere in your Access app, including forms.

>I need an unbound textbox that could display as many as 15000 lines of text.
No way, no how.  Even a RichTextBox control would not be able to go beyond 8000 (?) characters.
Come up with another design.
Avatar of KnowledgeWare
KnowledgeWare

ASKER

Yeah, I wasn't thinking that through... I'm printing values from an Excel spreadsheet as part of this and I shortened my test one to about 100 rows. Then I looked back at the clients and they've got 13000... so yeah, new plan is needed.

So in my module "modCustom" I declare a function called "fnDisplayText(txt As String, msg As String)"... then what? I pass in the string "TextBox1" and that doesn't help me at all because I don't know what form it's on. So I changed my function to "fnDisplayText(frm as Form, txt As String, msg As String)" and inside wrote:

frm.Controls.item(txt).Text = frm.Controls.item(txt).Text & msg

and that worked. Is there a better way?
ASKER CERTIFIED SOLUTION
Avatar of shambalad
shambalad
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo