writing a string value to a txtBox on Form_Open event

  • Thread starter Thread starter Jason M Canady
  • Start date Start date
J

Jason M Canady

Hi all.

I am trying to write to the control source of a text control in a report
header when the user opens the report.
Here is what I have so far:

Dim strResult As String ' Stores the result of the input box
Dim strControlText As String ' Stores the string to write to the text
control
strResult = InputBox("Which Tumbler Barrel do you want to generate a field
report for?", _
"Generate Barrel Field Report") ' Asks the user for INPUT
strControlText = "Rock Polishing Database Field Worksheet - For
tumbler barrel No." _
& strResult & "." ' Sets the value of the string to write to the txt
control
' It works op to this point!
Me.txtHeader.ControlSource = strControlText ' This gives a #NAME
error

I know that I can shorten up the code a ton and do the same thing as the 4
lines above, but I was trying to find where the code was going wrong, and
the extra lines made it easier to insert a breakpoint and examine the
results.

If anyone can help I would be very appreciatave... I didn't think that this
would be difficult (silly me!)

Jasonm
 
You need to preface the string with an = sign:

Me.txtHeader.ControlSource = "=" & strControlText
 
In addition to what I've posted just a moment ago, you may find that the
Open event is "too early" to try to write to a control's control source, as
controls may not actually be in place at that point. What you may want to
use instead is the OnFormat event of the report header section.
 
Ken,
Thank you very much.
I will try the first and if I find that it is as you said , too early, I
will move the event to the format header event. It seems that this is a bit
of a round about way to update the value of a text box... what is wrong with
txtControlName.Text = ? That seems a ton easier!!!
Thanks again! I really appreciate the help.
Jason
 
Actually, in VBA for ACCESS you use .Value property, not .Text.

You can access the .Text property only when the control has the focus, which
it never does in a form. And, the .Text property is simply what is
displayed, not the value that the control has.

If all you want to do is to display the result, then use
Me.ControlName.Value = "Value"

I apologize that I didn't understand that this is what you were seeking to
do. And in this case, you'll need to use the OnFormat event, I believe.
 
I would place a text box on a form for the user to enter a value. Then add a
text box to the report with a control source like:
="Rock Polishing Database......." & Forms!frmYourForm!txtYourText
 
Back
Top