Link to home
Start Free TrialLog in
Avatar of Bruj
BrujFlag for United States of America

asked on

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

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?

Thanks!

Bruce
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

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.
not really sure what you are after, give this a try

fpath="C:\folderName\"
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"
Avatar of hitsdoshi1
hitsdoshi1

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"
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.  
Avatar of Bruj

ASKER

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!


Thanks!
did you even try the post at http:#a35750629 ?
Avatar of Bruj

ASKER

capricorn1
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
.scrollbars=xxx
.etc
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.



Thanks!
Avatar of Bruj

ASKER

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!
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruj

ASKER

Thank you much. I will play with this.