Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 6th, 2006, 12:26 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding controls dynamicly

Hi ,
How can i add in Acces VBA Editor Controls to a specific form ,
like CommandButton , Label or TextBox , and not in a design view?

Paula.

Reply With Quote
  #2 (permalink)  
Old February 6th, 2006, 03:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Pauls,

1. Open the form you want to add the control to (Form2) in design view and hide it.
2. Add a new control using the CreateControl method.
3. Toggle the form to normal view.

Also, to position the new control you are working in twips (1" * 1440), not inches.

==Code============================================ =======

' Form1 code module
Private Sub Command1_Click()

    ' used to convert inches to twips
    Const twipsPerInch As Long = 1440

    ' Open form you want to add control to
    ' in design view and hide it.
    DoCmd.OpenForm "Form2", acDesign, , , , acHidden

    ' Add new control
    With CreateControl( _
             FormName:="Form2", _
             ControlType:=acTextBox, _
             Section:=acDetail, _
             Left:=1 * twipsPerInch, _
             Top:=1 * twipsPerInch, _
             Height:=0.25 * twipsPerInch, _
             Width:=1 * twipsPerInch)

             ' set control properties
             .Name = "txtTextBox"
    End With

    ' toggle form to Normal view
    DoCmd.OpenForm "Form2"
End Sub

HTH,

Bob


Reply With Quote
  #3 (permalink)  
Old February 6th, 2006, 04:39 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks ,
it really worked .
But how do i remove the message that asks me whether i want to save changes (i don't want to save changes)?


Reply With Quote
  #4 (permalink)  
Old February 6th, 2006, 07:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Not sure you can. Check out the DeleteControl command in VBA help. Maybe deleting the control before closing the form will prevent the form from attempting to save design changes. Don't know.

CreateControl is really designed for use with custom Builders and Add-ins where you would want to save design changes.

Bob

Reply With Quote
  #5 (permalink)  
Old February 7th, 2006, 05:41 AM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe this will work , but the form has only the 'Close' event and not 'Closing' (or at least i didn't find it) , So with the close event it doesn't work (and it's logically , because 'closed' evend is performed after the 'closing' event and you can't change anything in there) .


Reply With Quote
  #6 (permalink)  
Old February 7th, 2006, 09:31 AM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I solved the last problem with the event 'Unload' .
But there's another issue about the original topic :
I tried to use the code inside Form_Load event , because that in that specific form i had some defined controls , but when the form loads i want also to add dynamicly controls - THIS DOESN'T WORK.
The exception is that adding or removing controls availble only in design view . Which changes do i need to do that it will work?


Reply With Quote
  #7 (permalink)  
Old February 7th, 2006, 10:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Try using the OnActivate or OnOpen event. Bottom line is the Form has to be open in design view to add the control. What code did you add to the Unload event to cancel the save?

Bob

Reply With Quote
  #8 (permalink)  
Old February 7th, 2006, 11:09 AM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried to write in these 2 events , but it threw exception .
I aslo tried other events like 'Render' but it didn't do anything , and this events don't occure .
about not saving , the command is the following:
DoCmd.Close acForm, "testTbl", acSaveNo

Reply With Quote
  #9 (permalink)  
Old February 7th, 2006, 11:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

How are you opening the form? Is it a startup form that would enable you to place the code in an AutoExec macro perhaps.

Reply With Quote
  #10 (permalink)  
Old February 7th, 2006, 12:02 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I need to open the form manually only .

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding controls to a form Scootterp Access VBA 2 August 4th, 2009 02:32 PM
Adding Controls Dynamically andyj00 C# 0 March 19th, 2007 08:54 AM
Adding Constituent Controls AGS BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 1 December 12th, 2005 06:32 AM
Adding controls to C# clojinted General .NET 1 February 12th, 2005 03:17 PM
Dynamically Adding Controls - Error pagates Excel VBA 1 August 28th, 2003 01:59 AM



All times are GMT -4. The time now is 10:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.