Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2003
    Posts
    311

    Answered: creating txtboxes at runtime and placing ona form?

    I wanted to add a text box for the number of the value in another text box and align them with my cboExpense my code dont work, does anyone have ideas on this >?

    i tried :

    Dim t As TextBox
    Dim i As Integer
    For i = 0 To txtNum.Value

    Set t = CreateObject("textbox")

    t.Name = txt & i
    t.Visible = True
    t.Left = Me.cboExpense.Left

    next i

    /M~W\

  2. Best Answer
    Posted by hammbakka

    "
    Originally posted by Apel
    true, but this only works in design view and not at runtime
    That's right, the code of SyberLynx is right, but it should not run in a [.mde] file."


  3. #2
    Join Date
    Apr 2002
    Location
    Germany
    Posts
    228
    Sadly Access VBA does not allow dynamic control creation as VB does.
    There's no way via the Windows API either as Access controls are not windows in the sense of the WinAPI unless they have the focus. Maybe you can try with the MS Form ActiveX controls although they surely add a lot of bloat.

  4. #3
    Join Date
    Nov 2003
    Posts
    1,487
    From the Access Help File

    The following example first creates a new form based on an Orders table. It then uses the CreateControl function to create a text box control and an attached label control on the form.
    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
    Check out the CreateControl and CreateReportControl Functions

    Last edited by CyberLynx; 12-12-03 at 02:06.

  5. #4
    Join Date
    Apr 2002
    Location
    Germany
    Posts
    228
    true, but this only works in design view and not at runtime

  6. #5
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1

    Lightbulb

    Originally posted by Apel
    true, but this only works in design view and not at runtime
    That's right, the code of SyberLynx is right, but it should not run in a [.mde] file.

  7. #6
    Join Date
    Sep 2015
    Posts
    1

    Creating Control at run-time in MS Access

    There is bug in MS Access that even if you follow the instructions from the official website of MS still you won't be able to '"create" and "edit" controls at "run-time".
    I found one way to go around this bug (witch is a combination of solutions I got from others).
    Non working solution: Don't tray to define a variable to create the object:-

    Sample 1:
    Code:
            DoCmd.OpenForm "Form1", acDesign
            Dim t as TextBox
            set t = CreateControl("Form1", acTextBox)
            t.FontName = "Arial"  'Any sample as test.   
            .ForeColor = vbRead   'Any sample as test.   
            .DefaultValue = 10    'Any sample as test.   
            'etc...
    This way will give you error (by the way it is what you get from MS official site).

    If you use the "With" it will work, but....
    Sample 2:
    Code:
            DoCmd.OpenForm "Form1", acDesign
            With CreateControl("Form1", acTextBox)
                .FontName = "Arial"  'Any sample as test.   
                .ForeColor = vbRead  'Any sample as test.   
                .DefaultValue = 10   'Any sample as test.   
                'etc...     
            End With
    This way Won't give you any error (bug free) But....
    You won't be able to edit it later through the code; because we didn't use an object or variable to hold it or point to it (as in Sample 1).


    My solution is: To use the name of the control to call it using "Controls()", and that is after useing "With" to create the control.:-
    Sample 3:
    Code:
            DoCmd.OpenForm "Form1", acDesign
            Dim strName as string
            With CreateControl("Form1", acTextBox)
                .FontName = "Arial"  'Any sample as test.   
                .ForeColor = vbRead	 'Any sample as test.   
                .DefaultValue = 10   'Any sample as test.   
                ' etc...
    	    strName = .Name   'Get the default name from the control (or you can give one).
            End With
            'And to be able to edit it later use the name you got in the previous lines.
            Me.Controls(strName).FontName = "Times New Roman"	'Any sample as test.   
            Me.Controls(strName).ForeColor = vbGreen	        'Any sample as test.   
            Me.Controls(strName).DefaultValue = 20	                'Any sample as test.   
            ' etc...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •