p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access (http://p2p.wrox.com/forumdisplay.php?f=18)
-   -   creating controls on the fly (http://p2p.wrox.com/showthread.php?t=7865)

mtrtxs January 5th, 2004 03:24 PM

creating controls on the fly
 
Hi,

I am trying creating forms on the fly from code to replace a clutter of too many small forms in a DB.

I hope to use code to add command buttons, and I want to attach code to them, and to be able to close the forms unsaved - without "save or not" dialog boxes and prompts. I want to just use the forms, then ditch them.


My code so far:

Private Sub NewControls()

Dim frm As Form
Dim ctlLabel As Control
Dim ctlText As Control
Dim BtnTest As Control
Dim intDataX As Integer
Dim intDataY As Integer
Dim intDataZ As Integer
Dim intLabelX As Integer
Dim intLabelY As Integer
Dim intLabelZ As Integer

Set frm = CreateForm ' Create new form

frm.RecordSource = "TblOrders" '''' with Orders table as record source

' size for textbox
intDataX = 2000 ' top, left, width - position, size new controls
intDataY = 100
intDataZ = 3000 ' width of textbox is set here - third size argument

' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "shipname", intDataX, intDataY, intDataZ)

' size for lable
intLabelX = 100
intLabelY = 100
intLabelZ = 1 ' hmmmm....odd, width of label is dependent
' on text within - rather than the argument here - not my
' prime question though.

' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , ctlText.Name, "Shipping Name ", intLabelX, intLabelY, intLabelZ)

With frm
    .DividingLines = False
    .ScrollBars = False
    .RecordSelectors = False
    .AutoResize = True
    .AutoCenter = True
    .BorderStyle = 3
    .Caption = "test"
End With

' line below to build a command button on the fly isn't working
' I don't know why - help - I just get errors

setBtnTest = CreateControl(frm.Name, acCommandButton, , , , 100, 100, 100, 100)

a = frm.Name

DoCmd.OpenForm a, acNormal
DoCmd.MoveSize , , 5555, 5555

End Sub



Thanks!!

C


fishypew January 7th, 2004 12:52 PM

I've got this working (not every detail, but basics)
You may have had trouble with specifying the name of
your form as String. I added a variable for form name.

Public Sub NewControls()

Dim frm As Form
Dim BtnTest As Control
Dim frmname As String

Set frm = CreateForm
frmname = frm.Name
frm.RecordSource = "tblCompany"
Set BtnTest = CreateControl(frmname, acCommandButton, , , , 100, 100, 100, 100)
DoCmd.OpenForm frmname, acNormal
End Sub


mtrtxs January 7th, 2004 05:39 PM

Hello Fishypew and all,

Thanks!

I see I had a typo - missing space in:
setBtnTest = CreateControlblahblahblah

It should have been:
set BtnTest = CreateControlblahblahblah

I'm now at code below...command button working (yay!) but with a macro (mega-boo!!!). I haven't cracked the problem of how to attach code to my command button.

Can you help me plug some code into the form module, or show me how to get the command button to avail itself of code elsewhere?

Out, out accursed macros!

Thanks in advance!!!!!


Private Sub NewControls()

Dim frm As Form
Dim ctlLabel As Control
Dim ctlText As Control
Dim BtnTest As Control

Dim intDataX As Integer
Dim intDataY As Integer
Dim intDataZ As Integer
Dim intLabelX As Integer
Dim intLabelY As Integer
Dim intLabelZ As Integer

Set frm = CreateForm ''''' Create new form

frm.RecordSource = "TblOrders" ' with Orders table as record source

' top, left, width, height - position and size new controls

intDataX = 2000
intDataY = 100
intDataZ = 3000 ' width of textbox is set here - third size argument

' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "shipname", intDataX, intDataY, intDataZ)
With ctlText

End With

intLabelX = 100
intLabelY = 100
intLabelZ = 1 ' hmmm...width of label seems to be dependent on text within

' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , ctlText.Name, "Shipping Name", intLabelX, intLabelY, intLabelZ)

b = ctlLabel.Width

a = frm.Name

With frm
    .DividingLines = False
    .ScrollBars = False
    .RecordSelectors = False
    .AutoResize = True
    .AutoCenter = True
    .BorderStyle = 3
    .Caption = "test"
    .Modal = True
    .ControlBox = False
End With

Set BtnTest = CreateControl(frm.Name, acCommandButton, , , "", 400, 800, 600, 600)

BtnTest.OnClick = "mcrSlam.slamDunk"
' Works, but uggh - it's a macro!

BtnTest.Caption = "&Close"
BtnTest.Cancel = True

DoCmd.OpenForm a, acNormal
DoCmd.MoveSize , , 5555, 5555

End Sub

mtrtxs January 8th, 2004 04:07 AM

Problem solved.

In case anyone needs to create forms from code - this should be a useful start.

Below is just a "proof of concept" - but it creates a form with textboxes and a command button, and code that closes the form without saving. And it works.



Private Sub NewControls()

Dim frm As Form
Dim mdl As Module

Dim ctlLabel As Control
Dim ctlText As Control
Dim ctlText2 As Control
Dim BtnTest As Control

Dim intDataX As Integer
Dim intDataY As Integer
Dim intDataZ As Integer
Dim intLabelX As Integer
Dim intLabelY As Integer
Dim intLabelZ As Integer

Dim a As String
Dim b As String
Dim strAddCode As String

Set frm = CreateForm ' Create new form
a = frm.Name
frm.RecordSource = "TblOrders" ' with Orders table as record source

' top, left, width - position and size new controls
intDataX = 2000
intDataY = 100
intDataZ = 3000

' Create unbound text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "shipname", intDataX, intDataY, intDataZ)

intLabelX = 100
intLabelY = 100
intLabelZ = 1 ' hmmm...width of label
                  ' seems to be dependent on text within

' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , ctlText.Name, "Shipping Name", intLabelX, intLabelY, intLabelZ)

With frm
    .DividingLines = False
    .ScrollBars = False
    .RecordSelectors = False
    .AutoResize = True
    .AutoCenter = True
    .BorderStyle = 3
    .Caption = "test"
    .Modal = True
    .ControlBox = False
    .HasModule = True
End With

Set BtnTest = CreateControl(frm.Name, acCommandButton, , , "", 400, 800, 600, 600)
b = BtnTest.Name ' KEEP c is used in code to close form

Set ctlText2 = CreateControl(frm.Name, acTextBox, , "", "=date()", 2000, 500, 3000)
With ctlText2
.TextAlign = 1
End With

BtnTest.OnClick = "[Event Procedure]"

Set mdl = Forms![Form1].Module
strAddCode = ""
strAddCode = "Private Sub " & b & "_Click()" & vbCrLf
strAddCode = strAddCode & "DoCmd.Close acForm, me.name, acSaveNo"
strAddCode = strAddCode & vbCrLf
strAddCode = strAddCode & "End Sub"

With mdl
    .InsertText strAddCode
End With

BtnTest.Caption = "&Close"
BtnTest.Cancel = True

DoCmd.OpenForm a, acNormal
DoCmd.MoveSize , , 8000, 8000

End Sub

linux.daemon September 8th, 2004 03:20 AM

Thanks to the sample code provided in this thread I have been able to build my dynamic form. But there is one thing I dont like: the controlls must be added to a new form. but what if I want to add the controlls to a existing form (like "Me")? Basically I don't want to have it as a new window popping up. But when I add it do "Me" I get a "Form must be in design view..." error message. So is there a way I could do that?

Regards,
Chrstioph


SerranoG September 8th, 2004 07:19 AM

Wow, this is very clever and interesting to read. I do have a great curiousity about why you'd want to create a form and its controls "on the fly" rather than just do it on your own once manually and open and close it when needed. Why reinvent the wheel each time?

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

linux.daemon September 8th, 2004 08:15 PM

I want to create the forms "on-the-fly" because
a) I have many, many different forms to create which would probably take me a week to do
b) I want to give the user the possiblity to "customize" the forms
i.e. I am thinking of something like adding unlimited "key-value" pairs to a form. The user could then add as many of these fileds to the form and give some basic attributes like name, size, and x/y position. Then the user-defined fields get added to the form.
I think you get the point.
Hope anyone can help me.

Regards,
Christoph


Braxis September 9th, 2004 04:12 AM

I've used code similar to this to provide DBA's with a tool for editing all the lookup tables in a large database.

The DBA just chose a table from from a drop-down, clicked a button, and a nice form appeared on screen which allowed them to review, edit and add new data to the chosen table.

The drop-down got it's data from another table, which held the names of the tables that could be edited, along with a description of the data held in the table.


Brian Skelton
Braxis Computer Services Ltd.

Braxis September 9th, 2004 04:21 AM

Have a look at 'CreateEventProc Method' and 'InsertLines Method' in the VBA help. These should give you what you need.


Brian Skelton
Braxis Computer Services Ltd.

linux.daemon September 10th, 2004 04:51 AM

Ok, got it working.
But here is another qusteion:
How can I populate the fields I add to the form with values (for a "add" feature it is fine when the fields are empty but for the "edit" version, I need the old values in the field)

Regards,
Christoph



All times are GMT -4. The time now is 01:39 PM.

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