View Single Post
  #4 (permalink)  
Old January 8th, 2004, 04:07 AM
mtrtxs mtrtxs is offline
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