I am trying to create controls, EG a text box, at runtime. ALL examples I find are creating a new form at the same time. I want to create a control on an existing form.
The following does not work:
Dim frm As Form
Dim Box_1 As Control
Set Box_1 = CreateControl(f rm.Name, acRectangle)
With Box_1
.Width = 100
.Height = 100
.Top = 10
.Left = 10
End With
Thanks for any help
2 8181 ADezii 8,834
Recognized Expert Expert
I am trying to create controls, EG a text box, at runtime. ALL examples I find are creating a new form at the same time. I want to create a control on an existing form.
The following does not work:
Dim frm As Form
Dim Box_1 As Control
Set Box_1 = CreateControl(f rm.Name, acRectangle)
With Box_1
.Width = 100
.Height = 100
.Top = 10
.Left = 10
End With
Thanks for any help
To create a Control on an existing Form (frmTest), execute the following code outside of the context of frmTest. The following code will: - Open frmTest in Design Mode.
- Create a Rectangle on frmTest with the following Properties:
- Width = 2.5 inches
- Height = 1 inch
- Top = 3 inches
- Left = 4 inches
- Special Effect = Sunken
- Border Style = Solid
- Back Style = Normal (color will show through)
- Backcolor = Blue
- Open frmTest in order to display the fruits of your labor.
- The following code has been tested and is fully operational.
- Dim frm As Form
-
Dim Box_1 As Control
-
Const conInch As Integer = 1440 'Measurements are in Twips
-
'1 inch = 1,440 Twips
-
-
DoCmd.OpenForm "frmTest", acDesign
-
-
Set frm = Forms!frmTest
-
-
Set Box_1 = CreateControl(frm.Name, acRectangle)
-
-
With Box_1
-
.Width = 2.5 * conInch
-
.Height = 1 * conInch
-
.Top = 3 * conInch
-
.Left = 4 * conInch
-
.SpecialEffect = 2 'Sunken
-
.BorderStyle = 1 'Solid
-
.BackStyle = 1 'Normal
-
.BackColor = vbBlue
-
End With
-
-
DoCmd.OpenForm "frmTest", acNormal, , , acFormEdit
There is bug in MS Access that even if you follow the instructions from the official website of MS still you won't be able to "create" and "edit" controls at "run-time".
I found one way to go around this bug (witch is a combination of solutions I got from others).
Non working solution: Don't tray to define a variable to create the object:-
Sample 1: -
DoCmd.OpenForm "Form1", acDesign
-
Dim t as TextBox
-
set t = CreateControl("Form1", acTextBox)
-
t.FontName = "Arial" 'Any sample as test.
-
.ForeColor = vbRead 'Any sample as test.
-
.DefaultValue = 10 'Any sample as test.
-
'etc...
-
This way will give you error (by the way it is what you get from MS official site).
If you use the "With" it will work, but....
Sample 2: -
DoCmd.OpenForm "Form1", acDesign
-
With CreateControl("Form1", acTextBox)
-
.FontName = "Arial" 'Any sample as test.
-
.ForeColor = vbRead 'Any sample as test.
-
.DefaultValue = 10 'Any sample as test.
-
'etc...
-
End With
-
This way Won't give you any error (bug free) But....
You won't be able to edit it later through the code; because we didn't use an object or variable to hold it or point to it (as in Sample 1).
My solution is: To use the name of the control to call it using "Controls() ", and that is after useing "With" to create the control.:-
Sample 3: -
DoCmd.OpenForm "Form1", acDesign
-
Dim strName as string
-
With CreateControl("Form1", acTextBox)
-
.FontName = "Arial" 'Any sample as test.
-
.ForeColor = vbRead 'Any sample as test.
-
.DefaultValue = 10 'Any sample as test.
-
' etc...
-
strName = .Name 'Get the default name from the control (or you can give one).
-
End With
-
'And to be able to edit it later use the name you got in the previous lines.
-
Me.Controls(strName).FontName = "Times New Roman" 'Any sample as test.
-
Me.Controls(strName).ForeColor = vbGreen 'Any sample as test.
-
Me.Controls(strName).DefaultValue = 20 'Any sample as test.
-
' etc...
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: owen |
last post by:
Generally speaking, what does it mean when I see a "button" with red text
showing this message instead of the control I've dragged onto the web form
in Design View.? (But the page works fine at runtime). Does it indicate
a problem with References for example?
Thanks
Owen
PS. replies cc'd by email appreciated.
owen.southwood@mantix.com
|
by: Matt Warner |
last post by:
Hi guys,
A couple of people have already posted questions about similar issues but
haven't had any response.
Occasionally, sometimes after running the app for a few hours, it bombs out
saying that it could not create a windows handle.
On one machine the stacktrace is this:
|
by: Sinisa |
last post by:
I have a Windows form which has multiple groups of the same window
controls.
I wanted to create a control class that has the standard controls in
one.
something like:
public class Sensor : System.Windows.Forms.Control
{
private System.Windows.Forms.TextBox Calculation;
|
by: Carlos Lozano |
last post by:
Hi,
What is the right way to create an OCX COM component. The component is
already registerred, but can't create an instance. I am using the reference
to the interop module created.
If I use javascript or vbscript it works.
I will appreciate any help.
I do the following (C#):
|
by: jcrouse |
last post by:
I am rotating some text is some label controls. In the one place I use it it
works fine. In the other place I use it I can't figure out the syntax. I
don't really understand the event. Where it works fine, it seems to fire
when the form changes visibility. Here is the code.
Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint
If lblP1JoyUp.Visible = True Then
Dim...
| |
by: Mats Lycken |
last post by:
Hi,
I'm creating a CMS that I would like to be plug-in based with different
plugins handling different kinds of content.
What I really want is to be able to load/unload plugins on the fly
without restarting the application.
What I did was to create an AppDomain that loaded the plugins and
everything was great, until I tried to pass something else that strings
between the domains...
|
by: Guillaume BRAUX |
last post by:
Hello,
What I want to do is to add a userControl to a form class witch is a
different class from the one the button is generated.
For example, I want to instanciate a label in "class1" and add it ("show
it") on a WinForm situated in "class2" (without having to add code to class2
!)
The problem is that I need to instanciate "class2" from "class1" to be able
to do a class2.controls.add(myLabel) in class1..
|
by: Marc Gravell |
last post by:
::: Question:
Does anyone know of a simple Control / UserControl implementation that hosts
a UITypeEditor? Or the correct way to call PropertyDescriptor.GetEditor()
and get a non-null result for usage in a Form?
::: Background:
Basically, I have a highly-dynamic (runtime) bespoke object model, and I
want to drop some (runtime-only) properties onto the form. As it happens,
the objects implement all the right interfaces for Binding to...
|
by: timnels |
last post by:
I have an issue where I have a user control that is launched into a
floating form. At some point later, I allow the user to "unfloat" the
user control by reparenting it on a split container in another form.
Problem is if I wake a tooltip when the window is floated, and then try
the same thing when it is reparented, the app crashes with " Cannot
access a disposed object.Object name: 'Form'.
Presumably, this is a result of the tooltips...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |