473,238 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

How to change control properties programmatically?

I'm trying to find a way to set form/control properties programmatically.

In a nut shell:

1. Open a database
2. Open a form in design view
3. Do something like this:
For Each prp In frm.Properties
If prp.Name = rst!PropName Then
prp = rst!ChangeTo
End If
Next

(this actually works for Form properties)

BUT .. I'm having a hard time with the Control properties...

How to access the properties of controls programmatically?

here is more code....

'first, open the database in question

Dim frm As Form
Dim prp as Property
Dim acc As Access.Application
Set acc = CreateObject("Access.Application")
acc.OpenCurrentDatabase varPath

''''''''''then open the form in design view

acc.DoCmd.OpenForm rst!ObjectName, View:=acDesign, WindowMode:=acHidden

'''''''''''now access the forms collection (I think that's what this
does...)

Set frm = acc.Forms(strFormName)

''''''''''''''''this is where I get lost...

For Each ctl In frm.Controls

'''''''''''''do I need For ... Each if I know what I'm looking for?

If ctl = acCommandButton Then 'error: This property isn't available in
design view
For Each prp In ctl.Properties
Debug.Print prp.Name & " = " & prp.Value
prp = rst!ChangeTo <<== rst has all the ChangeTo values
Debug.Print prp.Name & " = " & prp.Value
Debug.Print ctl.Name & " changed"
Next
End If
Next
Nov 12 '05 #1
8 16585
Controls are contained in the Controls collection much the same way an open
form is contained in the Forms collection. You'll need to add the statement
Dim ctl as Control in your code if you have Option Explicit turned on. Use
"For Each ctl in frm.Controls" to loop through all controls in the form and
then another nested For...Each to loop through all the properties in
clt.Properties collection.

Be careful to trap for errors!

In some cases, a control does not have some properties or events ascotiated
to it. Looping in this manner will also include form sections, labels,
lines, ect.
Perhaps use Select Case ctl.ControlType to determine if it is the type of
control that you need to set properties on. Some properties may require
others to be set in a specific manner such as WhatsThisButton - this cannot
be set unless MinMaxButtons is set to None.

Remember to close the form saving changes.

Mike Storr
www.veraccess.com

"deko" <dj****@hotmail.com> wrote in message
news:NA*******************@newssvr27.news.prodigy. com...
I'm trying to find a way to set form/control properties programmatically.

In a nut shell:

1. Open a database
2. Open a form in design view
3. Do something like this:
For Each prp In frm.Properties
If prp.Name = rst!PropName Then
prp = rst!ChangeTo
End If
Next

(this actually works for Form properties)

BUT .. I'm having a hard time with the Control properties...

How to access the properties of controls programmatically?

here is more code....

'first, open the database in question

Dim frm As Form
Dim prp as Property
Dim acc As Access.Application
Set acc = CreateObject("Access.Application")
acc.OpenCurrentDatabase varPath

''''''''''then open the form in design view

acc.DoCmd.OpenForm rst!ObjectName, View:=acDesign, WindowMode:=acHidden

'''''''''''now access the forms collection (I think that's what this
does...)

Set frm = acc.Forms(strFormName)

''''''''''''''''this is where I get lost...

For Each ctl In frm.Controls

'''''''''''''do I need For ... Each if I know what I'm looking for?

If ctl = acCommandButton Then 'error: This property isn't available in
design view
For Each prp In ctl.Properties
Debug.Print prp.Name & " = " & prp.Value
prp = rst!ChangeTo <<== rst has all the ChangeTo values
Debug.Print prp.Name & " = " & prp.Value
Debug.Print ctl.Name & " changed"
Next
End If
Next

Nov 12 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If you know which control you want to change you can explicitly refer
to it:

Set ctl = frm!ControlName

To get the control type use this:

If ctl.ControlType = acCommandButton Then ...

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQAgi7YechKqOuFEgEQJV/wCcC+ZgBj49UpHkid1/ONixywEM0DQAnjvo
PONE59u06FgxyT4N+0Hgqxey
=2/7q
-----END PGP SIGNATURE-----

deko wrote:

< SNIP >
For Each ctl In frm.Controls

'''''''''''''do I need For ... Each if I know what I'm looking for?

If ctl = acCommandButton Then 'error: This property isn't available in
design view
For Each prp In ctl.Properties
Debug.Print prp.Name & " = " & prp.Value
prp = rst!ChangeTo <<== rst has all the ChangeTo values
Debug.Print prp.Name & " = " & prp.Value
Debug.Print ctl.Name & " changed"
Next
End If
Next


Nov 12 '05 #3
> To get the control type use this:
If ctl.ControlType = acCommandButton Then ...


I wish that worked... it's returning a number - it does not seem to want to
match the constant "acWhatever"

The reason this is important is because, as you know, the same property name
can be found in different object types. So, the code below might return
results like this in the immediate window:

********** making changes to frmMain '(the parent form)
Name = frmMain
Name ==>> changed to frmPropNew

********** making changes to frmProperties '(the subform)
Name = frmProperties
Name ==>> changed to frmPropNew

Another issue I'm having is this:
Error Number 2135: This property is read-only and can't be set.
Is there a way to get around this?

Here is the code:

'''''' first do form

Do Until rst.EOF
Select Case rst!Container
Case "Forms"
acc.DoCmd.OpenForm rst!Parent, View:=acDesign,
WindowMode:=acHidden
Set frm = acc.Forms(rst!Parent)
Debug.Print "********** making changes to " & frm.Name
For Each prp In frm.Properties 'loop through Form properties
If prp.Name = rst!PropName Then 'if we find the property in
question, change it
Debug.Print prp.Name & " = " & prp.Value
Debug.Print prp.Name & " ==>> changed to " &
prp.Value
'Debug.Print "***** set new value to " &
rst!ChangeTo
prp = rst!ChangeTo
Debug.Print prp.Name & " changed to " & prp.Value
End If
Next

''''''''''' now do controls

For Each ctl In frm.Controls

'Debug.Print ctl.ControlType 'returns a number, like
"104" rather than "acCommandButton"
'so, it seems I cannot use:
'If ctl.ControlType = rst!ObjectType Then 'to match
ControlType
With ctl
If ctl.Name = rst!ObjectName Then
Debug.Print "********** making changes to " & ctl.Name
For Each prp In ctl.Properties
If prp.Name = rst!PropName Then
Debug.Print prp.Name & " = " & prp.Value
prp = rst!ChangeTo
Debug.Print prp.Name & " ==>> changed to " & prp.Value
End If
Next
End If
End With
Next
Nov 12 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The intrinsic constant acCommandButton has the value 104, therefore
this:

If ctl.ControlType = acCommandButton Then

will evaluate to True when the control is a CommandButton.

Your rst!ObjectType should be a numeric value representing one of the
intrinsic constants for the different control types. My guess is that
rst!ObjectType = "Command Button" - correct? Change it to the
appropriate number (intrinsic constant).
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQAiZVoechKqOuFEgEQLT1ACdHf68xL2Vq2Wx+UTkBA5e48 uRXx4An2T7
IMRvqSCxEjbwl1+fHdEPN56A
=gYPN
-----END PGP SIGNATURE-----
deko wrote:
To get the control type use this:
If ctl.ControlType = acCommandButton Then ...

I wish that worked... it's returning a number - it does not seem to want to
match the constant "acWhatever"

The reason this is important is because, as you know, the same property name
can be found in different object types. So, the code below might return
results like this in the immediate window:


< SNIP >
For Each ctl In frm.Controls

'Debug.Print ctl.ControlType 'returns a number, like
"104" rather than "acCommandButton"
'so, it seems I cannot use:
'If ctl.ControlType = rst!ObjectType Then 'to match
ControlType
With ctl
If ctl.Name = rst!ObjectName Then
Debug.Print "********** making changes to " & ctl.Name
For Each prp In ctl.Properties
If prp.Name = rst!PropName Then
Debug.Print prp.Name & " = " & prp.Value
prp = rst!ChangeTo
Debug.Print prp.Name & " ==>> changed to " & prp.Value
End If
Next
End If
End With
Next


Nov 12 '05 #5
> Your rst!ObjectType should be a numeric value representing one of the
intrinsic constants for the different control types. My guess is that
rst!ObjectType = "Command Button" - correct? Change it to the
appropriate number (intrinsic constant).


Actually, rst!ObjectType is the constant name, e.g. acCommandButton,
acListBox, etc.

it's strange how the constants don't work if I use:

If ctl.ControlType = acCommandButton Then ...

so I've been forced to use:

If ctl.ControlType = Nz(DLookup("PropValue", "qryProperties", _
"ObjectType = " & rst!ObjectType & _
" AND PropName = ContrloType"), 0) Then ...

(does DLookup return a string value?)

The list of constants is derived from the form itself, and saved in a table
like this:

Select Case ctl.ControlType
Case acLabel
strType = acLabel

so I'm saving the constant as a string value - is that why it's not working?

Nov 12 '05 #6
deko wrote:
The list of constants is derived from the form itself, and saved in a table
like this:

Select Case ctl.ControlType
Case acLabel
strType = acLabel

so I'm saving the constant as a string value - is that why it's not working?


These are most likely Long values and this may be the problem. You can use your
Object Browser (F2) to look at Access.AcControlType values (e.g. a Textbox has
a value of 109).

--
'-------------------------------
' John Mishefske
'-------------------------------

Nov 12 '05 #7
perhaps I should make an array of all the numeric constant values?? The
Select Case is something of a hack, and it would be nice to use the numbers
if I can keep track of them. Is there another way to work with these
things?

What I am doing is making an inventory of form/report properties, and
tabulating everything in a datasheet which is used to then make changes by
entering the new value...

thx

"John Mishefske" <mi****@execpc.com> wrote in message
news:10*************@corp.supernews.com...
deko wrote:
The list of constants is derived from the form itself, and saved in a table like this:

Select Case ctl.ControlType
Case acLabel
strType = acLabel

so I'm saving the constant as a string value - is that why it's not
working?
These are most likely Long values and this may be the problem. You can use your Object Browser (F2) to look at Access.AcControlType values (e.g. a Textbox has a value of 109).

--
'-------------------------------
' John Mishefske
'-------------------------------

Nov 12 '05 #8
> These are most likely Long values and this may be the problem. You can use
your
Object Browser (F2) to look at Access.AcControlType values (e.g. a Textbox has a value of 109).


Thanks for the tip on finding those values in the Object Browser.
Basically, I was mixing up the numeric values with the constants - e.g. 104
vs. acCommandButton. I'll assume those numeric values serve some purpose
other than confusion...

I added the ctl.ControlType (a numeric value) to the recordset (as a Long)
and did this:

For Each ctl In frm.Controls
If ctl.ControlType = rst!Ctl_ID Then 'e.g. 104
With ctl
If ctl.Name = rst!ObjectName Then 'e.g. "cmdClose"
For Each prp In ctl.Properties
If prp.Name = rst!PropName Then 'e.g. "FontName"
prp = rst!ChangeTo 'e.g. "Arial" to "Tahoma"

works great!
Nov 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fred Chen | last post by:
Hi, I've created a custom component. I want one of the properties of my custom component to be the output file name which is found in project properties. I know that I can get this by writing...
8
by: deko | last post by:
I'm hoping someone can sanity check my understanding of the Object Model for Forms/Controls. I'm having trouble drilling down into Control properties. First, I have a record set with the...
1
by: Rahim | last post by:
i want to change all the label control style Properties, server control properties at runtime how should i call all the label at runtime, which is present at webform, any collections???? i...
3
by: Mateusz Rajca | last post by:
Hello, I have Form1 - the main form with some controls including a button. I have Form2 - the properties form with a combobox and button. The user can select the colors of a button in the...
6
by: AMP | last post by:
Hello, I have an mdi program with a child form ("A") and another child ("B"). I want to change the text of a label on A by changing an item from a combobox on B. I can code the...
0
by: clintonG | last post by:
I've been looking for blogs, documentation, patterns, and/or your comments regarding how to design and use static methods of a "helper" class to change properties of controls in the page,...
3
by: codeslinger73 | last post by:
I ran into sort of a timing issue. I want to hold some default properties for form controls (font, borderstyle, etc) in the registry or a config file and then update these things after init but...
1
by: Dave | last post by:
I'd like to be able to change the ImageURL property for an Image control that resides inside a FormView. Can someone give me a hand with this? I'm not sure how to reference it. Thanks
2
by: onionman07 | last post by:
I am having a problem with a program i am trying to develop that uses menus to modify and track current properties of controls on a form. I have 2 PictureBox controls where I can choose how to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.