VBA code to create controls on a Tab Control

DanR

Registered User.
Local time
Tomorrow, 06:40
Joined
Mar 14, 2002
Messages
54
Hello

I am developing an Access 2000 application in which forms are altered dynamically based on options chosen by the user.

The CreateObject Function places checkboxes and other objects on a form, and this works fine.

The key line of code is:

Set ctlCheck = CreateControl(frm.Name, acCheckBox, , , "", posX, posY, chkWidth, chkHeight)

My problem is that I want to place the objects on pages of a tab control. I can do this in 'click and point' mode, but when I try it with VBA code, the objects are attached to the form, not the tab control page.

Any suggestions on how to tie the CreateControl command to a tab control page?

Things I have tried without success include:

a) using the page object name as the form name (the first parameter in the brackets) - it appears that the object must be a form.

b) using the page objects collection as the parent.

Thanks for your help

Dan
 
You have to use Forms( ).Controls( ).Page( ).Name for the parent argument.

This is the function I wrote for that purpose:

Code:
Function MakeControl(strFormName As String, _
                    iControlType As Integer, _
                    iLeft As Integer, _
                    iRight As Integer, _
                    Optional strCtlName As String = "", _
                    Optional iSection As Integer = acDetail, _
                    Optional strParentControlName As String = "", _
                    Optional strPageName As String = "", _
                    Optional strColumnName As String = "") As Variant


On Error GoTo Ooops


Dim frm As Form
Dim ctlParent As Control
Dim ctlCreate As Control


Set frm = Forms(strFormName)
If strPageName = "" Then
    Set ctlParent = frm.Controls(strParentControlName)
Else:
    Set ctlParent = frm.Controls(strParentControlName).Pages(strPageName)
End If


Set ctlCreate = CreateControl(strFormName, _
                              iControlType, _
                              iSection, _
                              ctlParent.Name, _
                              strColumnName, _
                              iLeft, iRight)


If strCtlName <> "" Then ctlCreate.Name = strCtlName

        
MakeControl = ctlCreate


BailOut:
Set frm = Nothing
Set ctlParent = Nothing
Set ctlCreate = Nothing
Exit Function


Ooops:
MakeControl = Null
GoTo BailOut


End Function

Alex

[This message has been edited by Alexandre (edited 03-14-2002).]
 
Thanks very much Pat & Alex

Alex your code was very useful, and Pat that advice will save me lots of trouble down the track.

I have changed approach, so the function to automatically populate the form with controls will only be used to update the forms, not in everyday use.

cheers
Dan
 
Code:
MakeControl = ctlCreate
Alex

Tweaked as follows...
Code:
Function MakeControl(....) as [B]Object [/B]
.
.
.
[B]Set [/B]MakeControl = ctlCreate
.
.
and it's still paying dividends a dozen years later.
_______________________
the machines never forget
 
Last edited:

Users who are viewing this thread

Back
Top Bottom