Results 1 to 10 of 10

Thread: Copy a form

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved Copy a form

    If I want to add a new form to my project and want to make it very similar to an existing one, i.e. same properties and layout except for a few controls or details, what would be the best/fastest way to go about? Can I store forms as templates for future use?
    Last edited by krtxmrtz; Feb 2nd, 2005 at 12:22 PM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Copy a form

    Quote Originally Posted by krtxmrtz
    If I want to add a new form to my project and want to make it very similar to an existing one, i.e. same properties and layout except for a few controls or details, what would be the best/fastest way to go about? Can I store forms as templates for future use?
    Copy the .FRM file at the WINDOWS level - to the new filename you want. Do not do this when you are in the project - so make sure you are out of the project.

    Also copy the .FRX file is there is one - that file stores odd binary stuff about a form.

    Then use NOTEPAD to EDIT the .FRM file. You will find only two or three references to the old FORMNAME in that .FRM file - it's just a simple text file - so change those references to the new name you choose.

    Now go into VB - back into the project and ADD A NEW FORM - and you will see this new form available to choose from - add it and you are done.

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Copy a form

    What I would do is create many instances of exactly the same form:
    Code:
    Private Sub cmdCreateNewForm_Click()
    Dim frmNew As frmJob
       Set frmNew = New frmJob
       Load frmNew
       frmNew.Show
       Set frmNew = Nothing
    End Sub
    Each time you click the button a new instance of the form is created and displayed.
    Now what you can do is add a property or function to your form to set what type of form it is:
    Code:
    'In a module
    Public Const FRM_TYPE_SALES As Long = 1
    Public Const FRM_TYPE_ORDERS As Long = 2
    
    'in the form
    Public Sub SetFormType(ByVal plngType As Long)
        Select Case plngType
            Case FRM_TYPE_SALES
                txtSalesAmount.Visible = True
            Case FRM_TYPE_ORDERS
                txtOrderNumber.Visible = True
       End Select
    End Sub
    This code hides and shows a few controls on the form.
    The rest of the controls you can leave as they will be used for both styles of form.
    The code to call this now would be:
    Code:
    Private Sub ShowNewForm(ByVal plngType As Long()
    Dim frmNew As frmJob
       Set frmNew = New frmJob
       Load frmNew
       frmNew.SetFormType plngType
       frmNew.Show
       Set frmNew = Nothing
    End Sub
    This would be called using:
    Code:
    ShowNewForm FRM_TYPE_SALES
    ShowNewForm FRM_TYPE_ORDERS
    ShowNewForm FRM_TYPE_SALES
    Does that make sense?

    Woka

  4. #4

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Copy a form

    Quote Originally Posted by szlamany
    Copy the .FRM file at the WINDOWS level - to the new filename you want...
    That's more or less what I was afraid I'd have to do. Not that it means much trouble really, but I wanted to make sure there wasn't some tool in the developing environment to do it more directly, like when you copy a file in windows and it's exactly the same except the name.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Copy a form

    Quote Originally Posted by Wokawidget
    What I would do is create many instances of exactly the same form...
    I save your code for future reference, though for now Szlamany's suggestion looks simpler.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Copy a form

    Having 2 instances of the form in a project, that do almost exactly the same thing is madness
    More coding if one thing changes, or if there's a bug etc.

    Alternatively you could create a usercontrol that had the common controls in it, and drop that onto 2 different forms, then just add the custom controls to that.

    woka

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Copy a form

    Quote Originally Posted by krtxmrtz
    I save your code for future reference, though for now Szlamany's suggestion looks simpler.
    It might but you should look toward creating forms/controls dynamically and having only few created in design. You may also have a plain form with feew basic controls such button, textbox, label, etc (or without any at all) and control your form's load from some config table. This takes a bit to develop logic but once you're done - the rest is a breeze. And after all you may end up with excutable size of under a 1 MB ...

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Copy a form

    Quote Originally Posted by Wokawidget
    Having 2 instances of the form in a project, that do almost exactly the same thing is madness
    More coding if one thing changes, or if there's a bug etc.
    The thing is those forms are not exactly the same, only at the beginning. The code wouldn't be the same. For example, somtimes I want to have a number of forms without the control box at the upper right corner. As this is not the default setting when you create a new form, I'd rather "make copies" of the original form where there's no control box. Oh boy, it's not that easy to explain, but thank you guys for your suggestions. I'm sure sooner or later I'll have to seriously consider Rhinobulls advice and stop programming recklessly.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Copy a form

    Quote Originally Posted by RhinoBull
    It might but you should look toward creating forms/controls dynamically and having only few created in design. You may also have a plain form with feew basic controls such button, textbox, label, etc (or without any at all) and control your form's load from some config table. This takes a bit to develop logic but once you're done - the rest is a breeze. And after all you may end up with excutable size of under a 1 MB ...
    Morning. Have you got an example of this code?

    WOka

  10. #10

Posting Permissions

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



Click Here to Expand Forum to Full Width