 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 6th, 2006, 12:26 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

February 6th, 2006, 03:09 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
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
|
|

February 6th, 2006, 04:39 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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)?
|
|

February 6th, 2006, 07:48 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
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
|
|

February 7th, 2006, 05:41 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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) .
|
|

February 7th, 2006, 09:31 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|

February 7th, 2006, 10:16 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
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
|
|

February 7th, 2006, 11:09 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

February 7th, 2006, 11:22 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
How are you opening the form? Is it a startup form that would enable you to place the code in an AutoExec macro perhaps.
|
|

February 7th, 2006, 12:02 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I need to open the form manually only .
|
|
 |