Is there a way to get the code for the form created in Access?

Posted on 2011-05-12
Last Modified: 2012-05-11
I know that in Excel, you can "export" the form and in VB you can save the form and get the code on how to build that form.
I have not been able to find similiar in Access.
I am looking for coding on how to rebuild the look of the screen like where txtbox1 is, the size etc...
In Excel, I have been able to do this which allows me to write code to dynamically change the screens easily.
Thinks like adding rows as needed, change field size, I had before used several "templates" that allow me to change groups of items at once. I is really nice having the code layout so you know where to put things... The learning curve of moving from one product to another...
Or is there a tool that would allow me to get this?


Question by:Bruj
    LVL 92

    Expert Comment

    by:Patrick Matthews
    As far as I know you cannot export access forms in quiyte the same way as in excel or word.

    However, you can save your forms to another db, and then use TransferDatabase to import forms into your current database on demand.
    LVL 119

    Expert Comment

    by:Rey Obrero
    not really sure what you are after, give this a try

    application.saveastext acForm, "form1", fPath &  "form1.txt"

    open the text file form1.txt

    to retrieve the form from the text file

    application.loadfromtext acForm, "form1", fPath &  "form1.txt"
    LVL 9

    Expert Comment

    You're creating Access objects (forms, controls). Access provides the
    ability to use VBA to write script that can be triggered by different
    events on an object (eg. button press, form open.. etc). Generally there
    won't be any script unless you write it (or it has been generated by a
    wizard or something).

    eg. to view the VBA script behind a form open the form in design view
    and select View/Code from the menu. Or, open the form/control's property
    sheet and scroll down to the list of events. If there is VBA script
    behind an event the text "[Event Procedure]" will be displayed. To
    access it simply click on that text and then press the build button that
    appears to the right.

    If you really want to see what happens 'behind the scenes' when you create a
    form, you can use the undocumented 'SaveAsText' method to save the
    definition of a form to a text file. For example ...

    Press Ctrl+F11 then Ctrl+G and type the following line and press enter and take a look at Form1.txt

    SaveAsText acForm, "Form1", "c:\form1.txt"
    LVL 42

    Expert Comment

    In Access, we normally construct forms in the design view GUI.  But, with much more effort the same forms can be created entirely in code.  

    I believe the question is this:  After constructing a form in design mode, is there a way to automatically generate the code for creating the identical form.  Sort of: is there a way to decompile a form into VBA code.  

    Author Comment

    After doing some research, it appears that you can get all of the info by stepping through the controls.
    Private Sub CommandButton1_Click()
    Dim cCont As Control
        For Each cCont In Me.Controls
    Debug.Print TypeName(cCont)
            If TypeName(cCont) = "TextBox" Then
                a = cCont.Name
                b = cCont.Top
                C = cCont.Left
    ' etc...
            End If
            If TypeName(cCont) = "Image" Then
                a = cCont.Name
                b = cCont.Top
                C = cCont.Left
                D = cCont.Picture
                ' etc...
            End If
         Next cCont
    End Sub

    Open in new window

    Now... can anybody point me to a source of controls and a list of their properties? I spent about 2 hours looking for this but could not find it. I am SURE it is out there!

    LVL 119

    Expert Comment

    by:Rey Obrero
    did you even try the post at http:#a35750629 ?

    Author Comment

    Yes I did.

    I have many forms to convert, so I was hoping to create a module that would go through each vba project and then pull the forms.

    Using the method (and it is very viable) What I would need to do is save it down to the file, then re-read the file to extract the info needed. I am not real strong reading in text files. This is the main issue with this approach. There are several PLUSes to it as well!

    Ideally, what I want to get to is a point where all of the controls are able to be extracted and then VBA code written similiar to

    dim MyForm as new form
    with MyForm
    .Caption = "My New Form"
    .popup = True
    .Modal = True
    .border style
    end with

    with textbox1
    .name = "whatever"
    .Top = xxx
    .Left = xxxx
    .Width = xxxx
    Height = xxxx
    .font = "Arial"
    end with

    Not very hard to convert from the txtfile as it is laid out very similiar.
    I can open and read the file, but I dont know how to do it in chunks, and with the images being embedded, I am not sure how that would effect being able to put the whole file into a string to parse from. Of course I can research this. I was just thinking it would be easier to go straight from the origianl object.

    There are several reasons I am looking for this information
    First is as I listed above, so I can change the form dynamically

    Second, it would allow me to port the forms to/From Excel to or from Access (I know I will need to change measurements)
    Some of our programs, managemnt wants to have both apps able to run.

    I would like to be able to work with the txt form as well, becuase it would allow me to convert some forms that we have in VB to VBA a little easier than opening up the project and then recreate the form manually.

    As far as the forms I am now trying to work with in Access, I have embedded the images and so there is a lot of "garbage" in the txt file.


    Author Comment

    capricorn1, It was late last week, and I was trying to get out of the office. Sorry for not responding, which I should have at that time.
    I do value your input and suggestions!
    LVL 119

    Accepted Solution

    see this link

    Reading Text Files

    Author Closing Comment

    Thank you much. I will play with this.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
    Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now