473,769 Members | 8,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a control on EXISTING form at runtime

5 New Member
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
Dec 4 '07 #1
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:
  1. Open frmTest in Design Mode.
  2. Create a Rectangle on frmTest with the following Properties:
    1. Width = 2.5 inches
    2. Height = 1 inch
    3. Top = 3 inches
    4. Left = 4 inches
    5. Special Effect = Sunken
    6. Border Style = Solid
    7. Back Style = Normal (color will show through)
    8. Backcolor = Blue
  3. Open frmTest in order to display the fruits of your labor.
  4. The following code has been tested and is fully operational.
Expand|Select|Wrap|Line Numbers
  1. Dim frm As Form
  2. Dim Box_1 As Control
  3. Const conInch As Integer = 1440     'Measurements are in Twips
  4.                                     '1 inch = 1,440 Twips
  5.  
  6. DoCmd.OpenForm "frmTest", acDesign
  7.  
  8. Set frm = Forms!frmTest
  9.  
  10. Set Box_1 = CreateControl(frm.Name, acRectangle)
  11.  
  12. With Box_1
  13.   .Width = 2.5 * conInch
  14.   .Height = 1 * conInch
  15.   .Top = 3 * conInch
  16.   .Left = 4 * conInch
  17.   .SpecialEffect = 2    'Sunken
  18.   .BorderStyle = 1      'Solid
  19.   .BackStyle = 1        'Normal
  20.   .BackColor = vbBlue
  21. End With
  22.  
  23. DoCmd.OpenForm "frmTest", acNormal, , , acFormEdit
Dec 4 '07 #2
kaddoura1
2 New Member
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:
Expand|Select|Wrap|Line Numbers
  1.         DoCmd.OpenForm "Form1", acDesign
  2.         Dim t as TextBox
  3.         set t = CreateControl("Form1", acTextBox)
  4.         t.FontName = "Arial"  'Any sample as test.   
  5.         .ForeColor = vbRead   'Any sample as test.   
  6.         .DefaultValue = 10    'Any sample as test.   
  7.         'etc...
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1.         DoCmd.OpenForm "Form1", acDesign
  2.         With CreateControl("Form1", acTextBox)
  3.             .FontName = "Arial"  'Any sample as test.   
  4.             .ForeColor = vbRead  'Any sample as test.   
  5.             .DefaultValue = 10   'Any sample as test.   
  6.             'etc...     
  7.         End With
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1.         DoCmd.OpenForm "Form1", acDesign
  2.         Dim strName as string
  3.         With CreateControl("Form1", acTextBox)
  4.             .FontName = "Arial"  'Any sample as test.   
  5.             .ForeColor = vbRead     'Any sample as test.   
  6.             .DefaultValue = 10   'Any sample as test.   
  7.             ' etc...
  8.             strName = .Name   'Get the default name from the control (or you can give one).
  9.         End With
  10.         'And to be able to edit it later use the name you got in the previous lines.
  11.         Me.Controls(strName).FontName = "Times New Roman"    'Any sample as test.   
  12.         Me.Controls(strName).ForeColor = vbGreen            'Any sample as test.   
  13.         Me.Controls(strName).DefaultValue = 20                    'Any sample as test.   
  14.         ' etc...
  15.  
Sep 14 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
6119
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
0
600
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:
0
2155
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;
15
6750
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#):
6
3163
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...
12
3175
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...
5
2000
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..
2
8040
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...
9
2434
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...
0
9589
marktang
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...
0
9423
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,...
0
10216
Oralloy
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...
0
10049
jinu1996
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...
1
7413
isladogs
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...
0
5310
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...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
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...

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.