Solved

creating a variable for a control and setting a property

Posted on 2013-11-05
4
278 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 250 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 250 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

Industry Leaders: 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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

737 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