Hi,
Does anyone know how to add controls to a form, dynamically??
the CreateControl method is what you want, plenty of info in help file, here's an example
Code:
Sub NewControls()
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
' Create new form with Orders table as its record source.
Set frm = CreateForm
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100
' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
End Sub
Was this answer helpful ?
Yes No
Quote:
| Originally Posted by meloncolly the CreateControl method is what you want, plenty of info in help file, here's an example
Code:
Sub NewControls()
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
' Create new form with Orders table as its record source.
Set frm = CreateForm
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100
' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
End Sub
|
I have tried it. But what I want is to add a control in the existing form, not create a new form and add a control to it.
Do you know if it is possible of not?
Was this answer helpful ?
Yes No
so simply ignore the code about creating a new form...you may have to switch the form into design mode in code before you add the new control
Was this answer helpful ?
Yes No
and here's some code for that..
The following example uses the GetCurrentView subroutine to determine whether a form is in Form or Datasheet view. If it's in Form view, a message to the user is displayed in a text box on the form; if it's in Datasheet view, the same message is displayed in a message box.
GetCurrentView Me, "Please contact system administrator." Sub GetCurrentView(frm As Form, strDisplayMsg As String) Const conFormView = 1 Const conDataSheet = 2 Dim intView As Integer intView = frm.CurrentView Select Case intView Case conFormView frm!MessageTextBox.SetFocus ' Display message in text box. frm!MessageTextBox = strDisplayMsg Case conDataSheet ' Display message in message box. MsgBox strDisplayMsg End SelectEnd Sub
Was this answer helpful ?
Yes No
Quote:
| Originally Posted by meloncolly and here's some code for that..
The following example uses the GetCurrentView subroutine to determine whether a form is in Form or Datasheet view. If it's in Form view, a message to the user is displayed in a text box on the form; if it's in Datasheet view, the same message is displayed in a message box.
GetCurrentView Me, "Please contact system administrator." Sub GetCurrentView(frm As Form, strDisplayMsg As String) Const conFormView = 1 Const conDataSheet = 2 Dim intView As Integer intView = frm.CurrentView Select Case intView Case conFormView frm!MessageTextBox.SetFocus ' Display message in text box. frm!MessageTextBox = strDisplayMsg Case conDataSheet ' Display message in message box. MsgBox strDisplayMsg End SelectEnd Sub |
Thanks a lot, The code is helpful, but I can't figure how to switch a form form design view to form view?
Was this answer helpful ?
Yes No
ok, you can't add controls to the form that has the code running in it, so you need to run the code from one form to add controls to another.
The code below works for me.
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
Dim thisform
thisform = "test"
'DoCmd.Close acForm, thisform
DoCmd.OpenForm thisform, acDesign
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100
' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(thisform, acTextBox, acDetail, "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(thisform, acLabel, acDetail, _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
Was this answer helpful ?
Yes No