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
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access 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 January 5th, 2004, 03:24 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

Reply With Quote
  #2 (permalink)  
Old January 7th, 2004, 12:52 PM
Registered User
 
Join Date: Jan 2004
Location: , RI, .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #3 (permalink)  
Old January 7th, 2004, 05:39 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #4 (permalink)  
Old January 8th, 2004, 04:07 AM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old September 8th, 2004, 03:20 AM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #6 (permalink)  
Old September 8th, 2004, 07:19 AM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

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
Reply With Quote
  #7 (permalink)  
Old September 8th, 2004, 08:15 PM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #8 (permalink)  
Old September 9th, 2004, 04:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Oxford, , United Kingdom.
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #9 (permalink)  
Old September 9th, 2004, 04:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Oxford, , United Kingdom.
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #10 (permalink)  
Old September 10th, 2004, 04:51 AM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

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
Controls on the fly language rulllesss XML 0 June 9th, 2008 09:16 AM
Creating tables on the fly Keith Smith Access VBA 1 January 15th, 2008 10:49 AM
creating textfield on the fly Acod Classic ASP Professional 1 April 25th, 2007 09:22 AM
Creating htmls on the fly... merguvan Classic ASP Basics 5 January 31st, 2004 03:31 PM
Creating form controls on the fly roniestein Access 6 December 23rd, 2003 01:30 PM



All times are GMT -4. The time now is 09:07 AM.


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