Solved

creating a variable for a control and setting a property

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

943 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

4 Experts available now in Live!

Get 1:1 Help Now