1

So I'm attempting to change the value of a textbox on a form with VBA code.

I'm actually doing this by running a string as code so I can loop through a number of textboxes that are named similarly (RacewayOrder1, RacewayOrder2, etc...) and populate them all one at a time. So my code looks like this:

Private Sub FillRow(ByVal OrderNumber As Integer)

Dim tempstr As String
tempstr = "RacewayOrder" & OrderNumber & " = " & OrderNumber
Eval (tempstr)

End Sub

However, I am getting runtime error 2482 "Database cannot find the name 'RacewayOrder1' you entered in the expression"

I have verified that this isn't a typo, as I copied the text box's name directly into vba, but that doesn't seem to help. I even created the tempstr variable so I could inspect the code it is attempting to run, but that hasn't helped at all either.

To make matters more annoying, I debug.printed tempstr and pasted it in as a command, and it works fine.

enter image description here

Any advice?

1 Answers1

2

You can't use Eval() to assign a value to your text box. If you overcome the RacewayOrder1 not found problem, Eval() will treat your string expression as a test for equality, not as a value assignment.

Forget about Eval() for this and simply reference the text box by name in the form's Controls collection:

Me.Controls("RacewayOrder" & OrderNumber).Value = OrderNumber
HansUp
  • 95,961
  • 11
  • 77
  • 135