Referencing textbox objects with strings

  • Thread starter Thread starter RadarUK
  • Start date Start date
R

RadarUK

Guest
I am trying to loop through textboxes so that I can fill the textboxes with values from a recordset. The loop isn't the problem, the problem is assigning the textbox name to a contol. Here is the code:

Dim QtyBrk As TextBox
Dim sTempString As String

Do While Not m_rsQtyDet.EOF
sTempString = "txtQtyBrk" & QtyCount 'Textboxes called txtQtyBrk1, txtQtyBrk2, etc.. Maximum 5
Set QtyBrk = sTempString
QtyBrk.Enabled = True 'Qty_Details!QtyBrkNum
QtyBrk = m_rsQtyDet!QtyBrkNum

I need to know how to Set the textbox name property using the string variable.

Thanks in advance
 
Hi,

Set QtyBrk = Forms("YourFormName").Controls(sTempString)

Hope this helps.
Mary.h

P.S.
I would do it the other way round. Loop through the textboxes and assign the corresponding value from the recordset, like this:

dim m_rsQtyDet as dao.recordset 'or ADO what you prefer
dim QtyBrk as control

set m_rsQtyDet = currentdb.openrecordset( ....)
if m_rsQtyDet.recordcount = 0 then exit sub

for each QtyBrk in forms!YourFormName 'must be an open form
if left(QtyBrk.name)=txtQtyBrk then 'or If QtyBrk.ControlType = acTextBox
with m_rsQtyDet
.findfirst = "QtyCount=" & right(QtyBrk.name,1)
if .nomatch = false then
QtyBrk.value = .QtyBrkNum
end if
end with
end if
next
 
It Works

:) Thanks for all your help... I got it working
 

Users who are viewing this thread

Back
Top Bottom