Results 1 to 9 of 9
  1. #1
    Join Date
    May 2008
    Posts
    7

    Unanswered: creating userform using vba.....

    Hi all,
    Actually I want to create a userform using vba dynamically i.e I have to create labels, textboxes and so on dynamically at runtime. How can I do this?
    thanks in advance.

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    I'd be interested to know why, but look at CreateControl in VBA help.
    Paul

  3. #3
    Join Date
    May 2008
    Posts
    7
    thanks for reply,
    I tried with createControl on button event as:

    DoCmd.OpenForm "Form5", acDesign, , , , acHidden
    Dim ctlNewTxtbox As Access.Control
    Set ctlNewTxtbox = CreateControl("Form5", acTextBox, acDetail, , , 5106, 1418, 621, 240)

    Set ctlNewTxtbox = Nothing
    DoCmd.Restore

    It shows the error as:
    Run time error '29054'
    Project database can't add, rename or delete the control(s) you requested.

    what should i do?
    thanks

  4. #4
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    I think you need something like

    Dim ctlNewTxtbox As New Access.Control

    But I don't do enough coding in creating controls on the fly, so I could easily be wrong. Have you checked the help files for examples of using CreateControl?
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  5. #5
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Seems like an odd requirement... I'm assuming it's to do with permissioning? If so, then a more efficient method may be to create the controls up front and hae them hidden+enabled based on user rights.
    George
    Home | Blog

  6. #6
    Join Date
    Sep 2002
    Location
    South Wales
    Posts
    580

    Wink Creating a form at runtime

    Here is a link to the MSDN CreateForm Method:

    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    ...And the CreateControl

    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx


    However, I would be very interested to know a bit about why you are trying to do this as I suspect there is probably a better solution the forum could come up with for you.

    Keep us posted.
    Last edited by garethdart; 06-12-08 at 04:55.
    Windows Server 2003-8 / Terminal Services / SQL 2000 / Access 2003 / Office 2003-7 / Exchange 2003-7 / Blackberry Enterprise Server / AutoCAD / Lambert And Butler / Red Bull

  7. #7
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    If you are using Access 2000 or later I wouldn't suggest creating the form. I believe you need Exclusive access to the database to design any objects within the database. If more than one person access the database at a time your code may not run properly. I would put together a small prototype and test it before you put alot of time into it.

    If you have your database split between data and interface, and each person will have a copy of the frontend (interface) then you may not have a problem.

  8. #8
    Join Date
    May 2008
    Posts
    7
    Thanks to you all.
    Finally I got it. We can create dynamic user form as follows:
    We can do this in a subform, so create a subform and follow the procedures:


    1. Ensure that the subform is not opened with the main form.
    (me.subform.SourceObject="")

    2. Open the subform in design view:
    DoCmd.OpenForm msub_name, acDesign, , , acFormEdit, acHidden

    3. Delete unnecessary controls from the subform
    Call DeleteControl(msub_name, mcont.name)

    4. Use the createcontrol function to add the necessary controls

    5. Save the subform
    DoCmd.Close acForm, msub_name, acSaveYes

    6. In the main form, restore the sourceobject property of the subform

  9. #9
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Ah! NOW we learn you were trying to edit a subform on a mainform that is open, using VBA from the mainform!!!

    Thanks for posting the solution
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

Posting Permissions

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