Solved

creating a variable for a control and setting a property

Posted on 2013-11-05
4
273 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
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks guys, this is what I needed!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now