Populate text box control from Public variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I declare in a module

Public locationID As String

Then populate it in a subroutine

LocationID = "My string"

I can use the value of LocationID in my code just fine, but I also want to
display this value in a text box on a form. For the control source I have
tried

=[LocationID]
LocationID

Both result in an error. What's my problem please?
 
You can't use a variable directly as the source for a control on a form.
What you can do, however, is use a function that returns that value.

Public Function LocationIDvalue() as String
LocationIDvalue = "Your String"
End Function

Then you could set the control source for your control to

=LocationIDvalue()

Hope that helps.
 
richardb said:
I declare in a module

Public locationID As String

Then populate it in a subroutine

LocationID = "My string"

I can use the value of LocationID in my code just fine, but I also
want to display this value in a text box on a form. For the control
source I have tried

=[LocationID]
LocationID

Both result in an error. What's my problem please?

Variables can only be directly accessed in VBA code, not in ControlSource
expressions or queries. Create a function that returns the value of your
variable...

Function GetLocationID()
GetLocationID = LocationID
EndFunction

....and use the function in your ControlSource.
 
Thanks Randy,

Yes, that was my "Plan B". You have given me the answer, thanks...

Randy Harris said:
You can't use a variable directly as the source for a control on a form.
What you can do, however, is use a function that returns that value.

Public Function LocationIDvalue() as String
LocationIDvalue = "Your String"
End Function

Then you could set the control source for your control to

=LocationIDvalue()

Hope that helps.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


richardb said:
I declare in a module

Public locationID As String

Then populate it in a subroutine

LocationID = "My string"

I can use the value of LocationID in my code just fine, but I also want to
display this value in a text box on a form. For the control source I have
tried

=[LocationID]
LocationID

Both result in an error. What's my problem please?
 
Back
Top