?
Solved

creating a variable for a control and setting a property

Posted on 2013-11-05
4
Medium Priority
?
282 Views
Last Modified: 2013-11-06
I can do this from a module:
Forms![Sales_Contract_Review]![01-TextBox].BackColor = 967423

i am trying to create a loop where i change the 01 above based on my textbox names.

sTextBox = "Forms![Sales_Contract_Review]![" & Mid(Ctl.Name, 1, 2) & "-TextBox]"

Forms![Sales_Contract_Review].Controls("sTextBox").BackColor = 967423

when it hits my code I get "Sales Contract Review can't find the field 'sTextBox' referred to in your expression.

if i display sTextbox it is: "Forms![Sales_Contract_Review]![01-TextBox]."

i assume i am not doing something correct with my .controls

thank you in advance
0
Comment
Question by:BFanguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 1000 total points
ID: 39625805
Try this...

sTexbox =  Mid(Ctl.Name, 1, 2) & "-TextBox"
' And drop the quotes around sTextbox in this:
Forms![Sales_Contract_Review].Controls(sTextBox).BackColor = 967423

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 39625812
My above post assumes that somewhere in your unposted code you are looping through form controls... but In a simpler loop, 1 through 10 your code would be something like this:


Dim I as Integer
For I = 1 to 10
   sTexbox =  Format(I, "00") &  "-TextBox"
   Forms![Sales_Contract_Review].Controls(sTextBox).BackColor = 967423
Next I 

Open in new window

0
 
LVL 21

Assisted Solution

by:Boyd (HiTechCoach) Trimmell, Microsoft Access MVP
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 1000 total points
ID: 39625825
Try:

Dim I as integer
Dim iControlCount as Integer

 iControlCount = 10 ' set tot eh number of textboxes

For I = 1 to  iControlCount 

   sTextBox = Format(I ,"00") & "-TextBox"
   Forms![Sales_Contract_Review].Controls(sTextBox).BackColor = 967423

Next 

Open in new window


EDIT: mbizup types faster. Need more caffeine!

Or you could use the Tag property for the controls. It will make it easy to add control and not depend on the name.

example:

    For Each cCnt In Me.Controls
        
 
            If Left(cCnt.Tag, 3) = "req" Then
                cCnt.BackColor = 967423
            End If

        End If
    Next cCnt
              

Open in new window


Change this part Left(cCnt.Tag, 3) = "req" to match whatever value you place in the tag property
0
 

Author Closing Comment

by:BFanguy
ID: 39627273
thanks guys, this is what I needed!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question