Home All Groups Group Topic Archive Search About

Programatically design a form

Author
5 Jul 2005 9:46 AM
Peter T
I'm familiar with vba but fairly new to vb6

I want to replicate an Excel vba userform to a vb6 form. The form is very
complex, as I'm forever redesigning it I build a new vba userforms from an
array of control types and their properties stored in an excel worksheet.
Something like this

myArray = Excel-Worksheet-Range.Value ' control types & properties
' my array is 1 base, 2 dim's

With ThisWorkbook.VBProject.VBComponents(sFormName).Designer
For i = 1 To UBound(myArray)
s = "Forms." & myArray (i, 1) & ".1"  ' control classname
..Controls.Add (s)
..Controls(i - 1).Name = myArray (i, 2)
etc

I know how to get my array into vb, I also know I will need to change some
vba classnames (eg Forms.CommandButton.1" ) and property types to their vb6
equivalents.
However, and my question, how would I use this array it to design a vb6 form
that I can save.

TIA,
Peter T

Author
5 Jul 2005 11:41 AM
J French
On Tue, 5 Jul 2005 10:46:04 +0100, "Peter T" <peter_t@discussions>
wrote:

<snip>

>I know how to get my array into vb, I also know I will need to change some
>vba classnames (eg Forms.CommandButton.1" ) and property types to their vb6
>equivalents.
>However, and my question, how would I use this array it to design a vb6 form
>that I can save.

You would save yourself a lot of grief if you looked into Control
Arrays
Author
5 Jul 2005 4:21 PM
Peter T
Thanks for the tip. Unfortunately it does not appear from Ken's response to
be possible to use a Control Array to create a form other than at run time,
as opposed to a form that I can save which is what I want to do.

Regards,
Peter T

Show quoteHide quote
> >I know how to get my array into vb, I also know I will need to change
some
> >vba classnames (eg Forms.CommandButton.1" ) and property types to their
vb6
> >equivalents.
> >However, and my question, how would I use this array it to design a vb6
form
> >that I can save.
>
> You would save yourself a lot of grief if you looked into Control
> Arrays
Author
5 Jul 2005 5:03 PM
Ken Halter
"Peter T" <peter_t@discussions> wrote in message
news:OGfbm3XgFHA.2472@TK2MSFTNGP15.phx.gbl...
> Thanks for the tip. Unfortunately it does not appear from Ken's response
> to
> be possible to use a Control Array to create a form other than at run
> time,
> as opposed to a form that I can save which is what I want to do.
>
> Regards,
> Peter T

You can only create "True" control arrays at design time. You can only
create an array of controls at runtime (not a control array)... confusing?
For example. Here's one of the event handlers for a "True" control array.
Notice the Index argument.

Private Sub Command1_Click(Index As Integer)

End Sub

There's no way to set up WithEvents to work with an array so controls added
to an array at runtime, using Controls.Add won't be able to support events.
There are ways around that.... anyway. There's no way to save a form, as a
form, at runtime. You add the code to create the controls, and leave it
there for good. Every time that form loads, all controls will be re-created.
One issue is, VB is a fully compiled language. The concept of a "frm" or
"frx" file no longer exists once it's compiled. That means you can't reallt
save any results from anything you've done at runtime and expect that to be
there the next time the form loads.

To add controls to a "True" control array at runtime, use Load instead of
Controls.Add... here's an extremely "bare bones" sample...

Add Controls at Runtime
http://www.vbsight.com/Code.htm

...to get event behavior from controls you add using Controls.Add, without
setting a WithEvents variable for each, you can use something similar to the
code shown in.....

Share Events by using Implements
http://www.vbsight.com/Code.htm

Another that may help is... Add Controls at Runtime With Events (kind of) on
that same page (time to break up that page and add better descriptions)


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
5 Jul 2005 9:45 PM
Peter T
Thanks again Ken for your further explanation of the "confusing"
differences. Not sure if I am more or less confused <g>

The examples you have made available for download are much appreciated. Now
to digest.

Regards,
Peter T

Show quoteHide quote
> > Thanks for the tip. Unfortunately it does not appear from Ken's
> > response to  be possible to use a Control Array to create a form
> > other than at run time, as opposed to a form that I can save
> > which is what I want to do.
>
> You can only create "True" control arrays at design time. You can only
> create an array of controls at runtime (not a control array)... confusing?
> For example. Here's one of the event handlers for a "True" control array.
> Notice the Index argument.
>
> Private Sub Command1_Click(Index As Integer)
>
> End Sub
>
> There's no way to set up WithEvents to work with an array so controls
added
> to an array at runtime, using Controls.Add won't be able to support
events.
> There are ways around that.... anyway. There's no way to save a form, as a
> form, at runtime. You add the code to create the controls, and leave it
> there for good. Every time that form loads, all controls will be
re-created.
> One issue is, VB is a fully compiled language. The concept of a "frm" or
> "frx" file no longer exists once it's compiled. That means you can't
reallt
> save any results from anything you've done at runtime and expect that to
be
> there the next time the form loads.
>
> To add controls to a "True" control array at runtime, use Load instead of
> Controls.Add... here's an extremely "bare bones" sample...
>
> Add Controls at Runtime
> http://www.vbsight.com/Code.htm
>
> ..to get event behavior from controls you add using Controls.Add, without
> setting a WithEvents variable for each, you can use something similar to
the
> code shown in.....
>
> Share Events by using Implements
> http://www.vbsight.com/Code.htm
>
> Another that may help is... Add Controls at Runtime With Events (kind of)
on
> that same page (time to break up that page and add better descriptions)
>
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
>
Author
6 Jul 2005 1:59 AM
John B
Ken Halter wrote:
Show quoteHide quote
> "Peter T" <peter_t@discussions> wrote in message
> news:OGfbm3XgFHA.2472@TK2MSFTNGP15.phx.gbl...
>
>>Thanks for the tip. Unfortunately it does not appear from Ken's response
>>to
>>be possible to use a Control Array to create a form other than at run
>>time,
>>as opposed to a form that I can save which is what I want to do.
>>
>>Regards,
>>Peter T
>
>
> You can only create "True" control arrays at design time. You can only
> create an array of controls at runtime (not a control array)... confusing?
> For example. Here's one of the event handlers for a "True" control array.
> Notice the Index argument.
>
> Private Sub Command1_Click(Index As Integer)
>
> End Sub
>
> There's no way to set up WithEvents to work with an array so controls added
> to an array at runtime, using Controls.Add won't be able to support events.
And didnt that piss me off mightily when I had to have 11 controls set @
runtime with 11 different event handlers :(
Of course all the event handlers called the same method passing a diff
param each time but thats beside the point :)

JB
<snip>
Author
6 Jul 2005 3:02 PM
Patrick Pirtle
I don't wish to hijack this thread, but the replies all sound
as though they suggest a solution to a problem I've had
for some time, and have had to solve through brute force.

In my office, we have structural drafters and civil drafters,
all of whom draw using a CAD program.  There is an
office application (VBA-based, not VB) that has a
multi-page control.  The problem is that, although large
blocks of code and several of the pages are the same
between the two groups, quite a bit is different.

I originally started to write the app and displayed, or
hid, those pages that didn't apply to whichever group
was using the program.  Here's the rub - At some point,
I started getting out-of-memory errors.  These could be
cured by deleting some controls from my form, so I
deduced (assumed) I had reached some limit as to the
number of controls.

After unsuccessfully trying to solve the problem, I
split the app into TWO apps.  Of course, now I have
large blocks of code that have to be updated TWICE.

So, my question is...Is it possible to somehow load an
entire page, along with a bunch of controls ON that
page AND the associated code, from a file on disk at
runtime?  This way, once my app starts and determines
which group the drafter belongs to, it could load only
the pertinent pages/controls/code into my form.

TIA for any help and suggestions.  And now, back to
the regularly-scheduled thread...


Ken Halter wrote:
> "Peter T" <peter_t@discussions> wrote in message
> news:OGfbm3XgFHA.2472@TK2MSFTNGP15.phx.gbl...
>
> You can only create "True" control arrays at design time. You can only
> create an array of controls at runtime (not a control array)...
> confusing? For example. Here's one of the event handlers for a "True"
> control array. Notice the Index argument.

[snip]
--------------------------------------------------------
The impossible just takes a little longer
Author
6 Jul 2005 4:16 PM
Peter T
Patrick,

As I know somewhat more about VBA than VB I'll have a go at answering your
question. We are talking VBA, not VB - right?

I understand the number of controls on a vba userform is limited to 256.
I've never tested that but have no problem with say 150. Also some report
problems with more than 64k of code in a module (so shift elsewhere).

You can certainly add controls to a userform at runtime, see example in Help
if you F1 over the .Add in say

Dim ctl as Control
Set ctl = UserForm1.Controls.Add("Forms.CommandButton.1")

Adding and removing code is more complicated. Chip Pearson, an Excel MVP,
shows how here:
http://www.cpearson.com/excel/vbe.htm

Note his comments about setting a ref to VBA Extensibility, also in Office
XP there are additional security settings to allow.

An alternative approach might be put all your controls on the form at design
stage, "Load" the form, then delete all unwanted controls, (very easy in a
few indexed loops), then "Show" the form.

However, you say you have lots of controls doing similar things. Probably
easier to use WithEvents Class(s), one set of code to handle all similar
controls. When you add a new control you just instantiate a new instance of
the class, adding a new reference to this class, in a Collection (unknown
qty) or array if pre-determined qty. Or possibly one set of controls, two
sets of WithEvents class's for different purposes and set only the relevant
class. The WithEvents code will respond same way as if the code was in the
userform module. I haven't fully explained but very easy.

Regards,
Peter T

Show quoteHide quote
"Patrick Pirtle" <p**@skilling.com> wrote in message
news:uAMTGvjgFHA.3608@TK2MSFTNGP12.phx.gbl...
> I don't wish to hijack this thread, but the replies all sound
> as though they suggest a solution to a problem I've had
> for some time, and have had to solve through brute force.
>
> In my office, we have structural drafters and civil drafters,
> all of whom draw using a CAD program.  There is an
> office application (VBA-based, not VB) that has a
> multi-page control.  The problem is that, although large
> blocks of code and several of the pages are the same
> between the two groups, quite a bit is different.
>
> I originally started to write the app and displayed, or
> hid, those pages that didn't apply to whichever group
> was using the program.  Here's the rub - At some point,
> I started getting out-of-memory errors.  These could be
> cured by deleting some controls from my form, so I
> deduced (assumed) I had reached some limit as to the
> number of controls.
>
> After unsuccessfully trying to solve the problem, I
> split the app into TWO apps.  Of course, now I have
> large blocks of code that have to be updated TWICE.
>
> So, my question is...Is it possible to somehow load an
> entire page, along with a bunch of controls ON that
> page AND the associated code, from a file on disk at
> runtime?  This way, once my app starts and determines
> which group the drafter belongs to, it could load only
> the pertinent pages/controls/code into my form.
>
> TIA for any help and suggestions.  And now, back to
> the regularly-scheduled thread...
>
>
> Ken Halter wrote:
> > "Peter T" <peter_t@discussions> wrote in message
> > news:OGfbm3XgFHA.2472@TK2MSFTNGP15.phx.gbl...
> >
> > You can only create "True" control arrays at design time. You can only
> > create an array of controls at runtime (not a control array)...
> > confusing? For example. Here's one of the event handlers for a "True"
> > control array. Notice the Index argument.
>
> [snip]
> --------------------------------------------------------
> The impossible just takes a little longer
>
>
Author
6 Jul 2005 9:45 PM
Patrick Pirtle
Peter -
Many thanks for your reply.  Please see my inline comments below.

Peter T wrote:
> Patrick,
>
> As I know somewhat more about VBA than VB I'll have a go at answering
> your question. We are talking VBA, not VB - right?

[Patrick]===========Yup.



> You can certainly add controls to a userform at runtime, see example
> in Help if you F1 over the .Add in say

[Patrick]===========More precisely, if I already HAVE a multi-page
control that was added at Design time, can I import additional PAGES, each
of which also contains a bunch of controls, into the existing multi-page?
For example, the linked images:

    ftp://mka.com/outbox/mainCiv.jpg
    ftp://mka.com/outbox/mainStr.jpg

show the two applications (title bar indicates which group).

The pages labelled "Scales," "Genl," "Cells," "Anno," "Options," and
"About" are the same between the apps.  Each of these pages has up
to 50 controls--see the example:

    ftp://mka.com/outbox/mainStr-Steel.jpg

The rest of the pages are different between the two user groups.

I'd like to merge the DIFFERENT pages into the existing contol at
runtime.




> An alternative approach might be put all your controls on the form at
> design stage, "Load" the form, then delete all unwanted controls,
> (very easy in a few indexed loops), then "Show" the form.

[Patrick]===========But, won't I still run into the memory problems
with too many controls?


--------------------------------------------------------
The impossible just takes a little longer
Author
6 Jul 2005 11:45 PM
Peter T
Patrick,

> > You can certainly add controls to a userform at runtime, see example
> > in Help if you F1 over the .Add in say
>
> [Patrick]===========More precisely, if I already HAVE a multi-page
> control that was added at Design time, can I import additional PAGES, each
> of which also contains a bunch of controls, into the existing multi-page?

Are your pages similar. If so, with a Multipage you can copy the page with
all it's controls and paste as a new page + controls, into what ever
location you want. Then make minor adjustments to controls as necessary, eg
captions. I answered a related question in excel.programming -

http://tinyurl.com/ddy3e

Use the OP's scenario given earlier in the thread and run my copy paste
code.

It didn't work for the OP first time but he quickly came back and said it
did. I've no idea if one can do this in VB, if not maybe VBA has some things
going for it.

You may be able to leave all code intact, or better still use the WithEvents
Class approach I suggested earlier.

If your pages are significantly different, then add a new multipage and the
controls to the multipage (not the form). As I suggested eariler but a lot
more code.


> > An alternative approach might be put all your controls on the form at
> > design stage, "Load" the form, then delete all unwanted controls,
> > (very easy in a few indexed loops), then "Show" the form.
>
> [Patrick]===========But, won't I still run into the memory problems
> with too many controls?

I've no idea, depends on many things, not least what your overall code does.

Are you sure your problems relate to too many controls and not something
else if all is within the limits I mentioned previously.  Eg, if you're
automating things and not releasing object var's in the correct order that
quickly bloats memory.

I see you are using images, do you have a lot loaded.  You can store these
on a second dummy form and set to image controls on page as the page is
activated. Then set the picture to nothing (remove picture) when
deactivated.

Regards,
Peter T

PS for readers dropping in - above relates to VBA, not VB.
Author
7 Jul 2005 12:12 AM
Peter T
PS - Ignore my final comments, I didn't read your scenario carefully. Seems
you have well over 250 controls so that could well be the cause of your
memory problem.

Also, in the post I referred you to I noted that the clipboard didn't clear
with Cutcopymode = false. I didn't go into that but if you are copying so
many controls multiple times better double check. If necessary look into the
"DataObject" method or as a last resort the clipboard can be cleared with a
set of API's.

Peter T
Author
7 Jul 2005 2:52 PM
Patrick Pirtle
Peter -
MANY thanks for your reply.  You've given me a bunch of
ideas to think about and play around with.  Could you answer
another question or two for me?

1. Is the limit on the number of controls based upon EACH
form in a VBA app?  Can each form have, say, 256?  Or,
is that the limit for the entire app, regardless of number of
forms?

2. If the limit from #1 is PER FORM, and I use your sug-
gestions for copying/pasting pages at runtime, does the
paste bring all the code along with the pasted page and
its controls?  If so, then maybe I could have a second,
dummy form that holds the "to-be-loaded-at-runtime"
pages (and their code).  Then, depending upon which of
my user groups is running the app, it could just insert th
appropriate pages.

3. Finally, as I got started on this entire app as a stop-gap
measure (and it has since grown into an app that is in use
full-time by 30+ people), it is probably suffering from all
kinds of problems due to my not releasing memory in the
correct manner.  Quite frankly, I don't understand how
or when such things need to occur, and feel overwhelmed
about how to even START on figuring this out.

Can you give me any advice for where to go for good
info on determining if memory is being cleared correctly?
Is that something as easy as looking to see how much
memory is available, running and exiting an app, and re-
checking how much memory is available?  Or, do you
use some third-party app that can "watch" your program
run?

Once again, Peter, you've helped me immensely.  Thanks!



Peter T wrote:
> PS - Ignore my final comments, I didn't read your scenario carefully.
> Seems you have well over 250 controls so that could well be the cause
> of your memory problem.
>
> Also, in the post I referred you to I noted that the clipboard didn't
> clear with Cutcopymode = false. I didn't go into that but if you are
> copying so many controls multiple times better double check. If
> necessary look into the "DataObject" method or as a last resort the
> clipboard can be cleared with a set of API's.
>
> Peter T

--
--------------------------------------------------------
The impossible just takes a little longer
Author
7 Jul 2005 5:30 PM
Peter T
Patrick,

> Peter -
> MANY thanks for your reply.  You've given me a bunch of
> ideas to think about and play around with.  Could you answer
> another question or two for me?
>
> 1. Is the limit on the number of controls based upon EACH
> form in a VBA app?  Can each form have, say, 256?  Or,
> is that the limit for the entire app, regardless of number of
> forms?

AFAIK 256 per form. I don't know but might have problems if multiple forms
are loaded even if not Show'n. If you reference a form it will automatically
load into memory, which would occur if you copy controls from one form to
another - so would need to unload the dummy form when done.

> 2. If the limit from #1 is PER FORM, and I use your sug-
> gestions for copying/pasting pages at runtime, does the
> paste bring all the code along with the pasted page and
> its controls?  If so, then maybe I could have a second,
> dummy form that holds the "to-be-loaded-at-runtime"
> pages (and their code).  Then, depending upon which of
> my user groups is running the app, it could just insert th
> appropriate pages.

Just the controls, not the code can be copy/paste. Did you try the example I
referred you to with some sample code in the form?

Without knowing what commonality your pages and controls have it's difficult
to suggest the best approach. But I strongly suspect the "WithEvents Class"
method will serve you very well.  Just one Event routine for all your
buttons, even 500 or even if you don't know how many in advance. This might
route to appropriate routines in normal modules depending on [say] which
button was clicked. Minimal code in your form, and no need to worry about
adding or copying code at runtime.

> 3. Finally, as I got started on this entire app as a stop-gap
> measure (and it has since grown into an app that is in use
> full-time by 30+ people), it is probably suffering from all
> kinds of problems due to my not releasing memory in the
> correct manner.  Quite frankly, I don't understand how
> or when such things need to occur, and feel overwhelmed
> about how to even START on figuring this out.

Without any idea of what you are doing difficult to comment. In generall all
objects var's should be released when not needed, eg public vars stay in
scope when the app terminates, so set myOb = nothing first. BUT if you haved
used any like this:

Set oApp = Create(myApp)
Set oAppThing = oApp.Add some-App-thing

then release in this order

oAppThing.close
Set oAppThing = Nothing
oApp.Quit
Set oApp = Nothing

> Can you give me any advice for where to go for good
> info on determining if memory is being cleared correctly?
> Is that something as easy as looking to see how much
> memory is available, running and exiting an app, and re-
> checking how much memory is available?  Or, do you
> use some third-party app that can "watch" your program
> run?

Off the top of my head I can't think where to direct you. Best avoid
problems in the first place. I suspect there are many in this group better
qualified to answer.

I can though recommend you to the .excel.programming group. It's obviously
geared to Excel but it's vast covering lots of generic vba, including your
issues. There are many there far more expert than I.

Explain your Multipage problem, be specific with some detail of what
commonality and differences there are between both pages and page controls,
eg buttons 10-10-20 of 50 on Page(0) do similar to buttons 10-20 of 50 on
Page(1) except ....  I'll be surprised if you don't receive ideas and a good
example of how to create a WithEvents Class specific to your needs. Not sure
how many are familiar with the copy / paste controls method, so if
appropriate include a copy of the link I gave earlier with something like -
"I'm thinking of using the method described here to copy multipage pages +
controls".

Ask about releasing memory issues seperately though you would need to be a
bit more specific to get useful answers.

Good luck.

Now to turn to my own issues in this thread which I've barely begun to
address!

Peter T
Author
6 Jul 2005 1:21 AM
J French
On Tue, 5 Jul 2005 17:21:50 +0100, "Peter T" <peter_t@discussions>
wrote:

>Thanks for the tip. Unfortunately it does not appear from Ken's response to
>be possible to use a Control Array to create a form other than at run time,
>as opposed to a form that I can save which is what I want to do.

You can't /create/ a Form
- I guess you mean populate a Form

Are you writing some sort of Form designer ?

If so, you could certainly do it using Control Arrays
Author
6 Jul 2005 9:50 AM
Peter T
> You can't /create/ a Form
> - I guess you mean populate a Form
>
> Are you writing some sort of Form designer ?
>
> If so, you could certainly do it using Control Arrays

Perhaps I used the wrong terminology. Hope I have not mislead either you or
Ken, if so apologies.

Yes, a Form designer in this sense. I don't want to have to drag controls
from the toolbox onto the form (in correct order) and type in all their
properties. Instead I want to automate with a predefined array of data.

I have a complicated VBA form which I want to simulate in VB. All the VBA
form's control details are stored on an Excel spreadsheet, effectively a 2D
array which I can amend to suit VB. I envisaged (envisioned) doing something
like this:

- VB contains an empty form, also a class module + code to receive the array
from Excel, and add controls to form. Press F5 and run.

- In Excel set a reference to the VB .vhp. Instantiate the VB class, send
the array and call a function to build / populate the Form.

- When done stop VB, save the Form.
- Import the Form into my main project.

Is something along these lines feasible ?

As I need to use WithEvents to handle my controls, from what I've learnt
recently it seems easier to work with a pre-built form and not to re-create
at runtime, each time.

Regards,
Peter T
Author
6 Jul 2005 12:25 PM
Larry Serflaten
Show quote Hide quote
"Peter T" <peter_t@discussions> wrote
> > You can't /create/ a Form
> > - I guess you mean populate a Form
> >
> > Are you writing some sort of Form designer ?
> >
> > If so, you could certainly do it using Control Arrays
>
> Perhaps I used the wrong terminology. Hope I have not mislead either you or
> Ken, if so apologies.
>
> Yes, a Form designer in this sense. I don't want to have to drag controls
> from the toolbox onto the form (in correct order) and type in all their
> properties. Instead I want to automate with a predefined array of data.
>
> I have a complicated VBA form which I want to simulate in VB. All the VBA
> form's control details are stored on an Excel spreadsheet, effectively a 2D
> array which I can amend to suit VB. I envisaged (envisioned) doing something
> like this:
>
> - VB contains an empty form, also a class module + code to receive the array
> from Excel, and add controls to form. Press F5 and run.
>
> - In Excel set a reference to the VB .vhp. Instantiate the VB class, send
> the array and call a function to build / populate the Form.
>
> - When done stop VB, save the Form.
> - Import the Form into my main project.
>
> Is something along these lines feasible ?


I hope you can see the work you are forcing upon yourself due to the
reliance on proper indexing of the control array.  I would suggest you
avoid using numeric indexing of the Controls array!  Either use the
proper names of the controls, or create your own groupings for the
loops you need:

delta = Array("Command1", "Command2", "Text1", "Text4")

For Each nam in delta
  Me.Controls(nam).Visible = False
Next

Of course, if your conserving resources, you'd be using control arrays,
in which case use the control themselves:

delta = Array(Buttons(1), Buttons(2), Texts(3), Text4)

For Each ctl in delta
  ctl.Visible = False
Next

Your delta array can be held for the life of the form such that it is
always available, or you can create ad hoc arrays on the fly:

For Each ctl in Array(Buttons(3), Buttons(4), Texts(3), Text4)
  ctl.Visible = False
Next

All of those methods do not rely on the controls being in a certain
order in the Controls collection, and they *provide documentation*
of the actual controls used in place of that hidden indexing!

Nonetheless....    ... with that said ...

When you get the form just how you want it, and then save it, how
does VB know the correct order, the next time you load your project?

When you save your form, it goes out to a text file, and from that VB
has to re-create the form, the next time it is loaded.  So, instead of trying
to build a form for VB to save, look into building the text file.  As a test
create a form with a few controls on it, and save it to disk.  Then open
that .frm file in notepad and re-arrange the controls as listed in the file.

Now add that form to a different project and compare the two Controls
collections.  You'll find you are able to affect the order by ordering them
in the .frm file, so really all you have to do is translate your Excel spreadsheet
to a .frm file that VB will accept.  Creating a text file from your data is going
to be a bit easier than trying to interface it with VB to build a new form.... 

See if that helps.  But I still advise you don't rely on the order of the controls
in the Controls array.  Other methods work as well, and are easier to maintain!

LFS
Author
6 Jul 2005 2:26 PM
Peter T
Thanks very much Larry for the detailed explanation. Your advice makes a lot
of sense, especially combined with that given by others. It implies more
re-writing of existing code than might otherwise be the case, no doubt would
repay in the long run. As you've gathered, coming from VBA, Control arrays
are a new concept for me.

Think I need to seek professional advice to short cut my learning curve, not
only of the form but other aspects.

Regards,
Peter T


Show quoteHide quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:#zNkQUigFHA.2560@TK2MSFTNGP10.phx.gbl...
>
> "Peter T" <peter_t@discussions> wrote
> > > You can't /create/ a Form
> > > - I guess you mean populate a Form
> > >
> > > Are you writing some sort of Form designer ?
> > >
> > > If so, you could certainly do it using Control Arrays
> >
> > Perhaps I used the wrong terminology. Hope I have not mislead either you
or
> > Ken, if so apologies.
> >
> > Yes, a Form designer in this sense. I don't want to have to drag
controls
> > from the toolbox onto the form (in correct order) and type in all their
> > properties. Instead I want to automate with a predefined array of data.
> >
> > I have a complicated VBA form which I want to simulate in VB. All the
VBA
> > form's control details are stored on an Excel spreadsheet, effectively a
2D
> > array which I can amend to suit VB. I envisaged (envisioned) doing
something
> > like this:
> >
> > - VB contains an empty form, also a class module + code to receive the
array
> > from Excel, and add controls to form. Press F5 and run.
> >
> > - In Excel set a reference to the VB .vhp. Instantiate the VB class,
send
> > the array and call a function to build / populate the Form.
> >
> > - When done stop VB, save the Form.
> > - Import the Form into my main project.
> >
> > Is something along these lines feasible ?
>
>
> I hope you can see the work you are forcing upon yourself due to the
> reliance on proper indexing of the control array.  I would suggest you
> avoid using numeric indexing of the Controls array!  Either use the
> proper names of the controls, or create your own groupings for the
> loops you need:
>
> delta = Array("Command1", "Command2", "Text1", "Text4")
>
> For Each nam in delta
>   Me.Controls(nam).Visible = False
> Next
>
> Of course, if your conserving resources, you'd be using control arrays,
> in which case use the control themselves:
>
> delta = Array(Buttons(1), Buttons(2), Texts(3), Text4)
>
> For Each ctl in delta
>   ctl.Visible = False
> Next
>
> Your delta array can be held for the life of the form such that it is
> always available, or you can create ad hoc arrays on the fly:
>
> For Each ctl in Array(Buttons(3), Buttons(4), Texts(3), Text4)
>   ctl.Visible = False
> Next
>
> All of those methods do not rely on the controls being in a certain
> order in the Controls collection, and they *provide documentation*
> of the actual controls used in place of that hidden indexing!
>
> Nonetheless....    ... with that said ...
>
> When you get the form just how you want it, and then save it, how
> does VB know the correct order, the next time you load your project?
>
> When you save your form, it goes out to a text file, and from that VB
> has to re-create the form, the next time it is loaded.  So, instead of
trying
> to build a form for VB to save, look into building the text file.  As a
test
> create a form with a few controls on it, and save it to disk.  Then open
> that .frm file in notepad and re-arrange the controls as listed in the
file.
>
> Now add that form to a different project and compare the two Controls
> collections.  You'll find you are able to affect the order by ordering
them
> in the .frm file, so really all you have to do is translate your Excel
spreadsheet
> to a .frm file that VB will accept.  Creating a text file from your data
is going
> to be a bit easier than trying to interface it with VB to build a new
form....
>
> See if that helps.  But I still advise you don't rely on the order of the
controls
> in the Controls array.  Other methods work as well, and are easier to
maintain!
>
> LFS
>
>
>
Author
5 Jul 2005 2:18 PM
Ken Halter
"Peter T" <peter_t@discussions> wrote in message
news:eQ0tcaUgFHA.272@TK2MSFTNGP15.phx.gbl...
> I'm familiar with vba but fairly new to vb6
>
> I want to replicate an Excel vba userform to a vb6 form. The form is very

You'll find a whole bunch of differences in VBA vs VB control functionality.
Those controls you're loading won't be usable without events. Many controls
that exist in VBA work completely different in VB. There's no FM20.dll
support for one thing so... no multicolumn combos, etc.

Regardless... whatever method you use to create the form, the only way to
"save" it is to do all of the work at design time in the VB IDE. Otherwise,
you'll need to re-create it at runtime, everytime. That's not always a bad
thing. Just letting you know.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
5 Jul 2005 4:18 PM
Peter T
Hi Ken,

> > I want to replicate an Excel vba userform to a vb6 form.
>
> You'll find a whole bunch of differences in VBA vs VB control
functionality.
> Those controls you're loading won't be usable without events. Many
controls
> that exist in VBA work completely different in VB. There's no FM20.dll
> support for one thing so... no multicolumn combos, etc.

Most of the controls on my VBA userform are similar to those available in
VB, buttons, labels etc, single column but no multi column combos. I think I
can work around most of the differences.

In VBA I use a separate WithEvents class to handle similar controls, I hope
I can do same in VB.

But ..

> Regardless... whatever method you use to create the form, the only way to
> "save" it is to do all of the work at design time in the VB IDE.

This is really a shame! Building the form manually is hours of work with
plenty of scope for getting details wrong. One reason I need to re-build is
that controls need to be added in the correct order, so not easy to amend an
existing form.

> Otherwise, you'll need to re-create it at runtime, every time.
> That's not always a bad thing. Just letting you know.

As you say, there may be good reasons for creating the form at run time, but
I would still want to test with a pre-built form.

I see I can import my VBA form into a VB project which appears to arrive as
my own ActiveX. Not sure if there's any way to use it, would be handy if
there is.

Thanks very much for your advice. I've only just started and have much to
learn so I may be back..

Regards,
Peter T
Author
5 Jul 2005 10:14 PM
Brian Schwartz
Peter,

> One reason I need to re-build is
> that controls need to be added in the correct order, so not easy to amend
an
> existing form.

If you don't mind me asking, I'm curious what you mean by this--that is, why
this is so?

Brian

--
Brian Schwartz
FishNet Components
http://www.fishnetcomponents.com
Building better tools for developers - Be part of it!
Author
6 Jul 2005 12:17 AM
Peter T
Brian,

I do a lot of looping of controls by index order, eg

Const cDeltaFirst = 20
Const cDeltaLast = 30

For i = cDeltaFirst to cDeltaLast
Me.controls(i).visible = false
Next

In VBA, and I assume similar in VB6 (?), the index order of controls is
determined by the order they were added to the Form, and cannot be changed
(unlike tab-order). While developing the form I change things about, add /
delete controls, resize, tooltips, tab-order etc. So if I add another
"delta" control and want its index say 25, I need to delete a lot of
controls that follow, add my new button, then replace all the others,
correctly.

I just find it so much easier to dump all control details and properties of
my form onto a spreadsheet. Make whatever changes, including add / delete
and reorder, then build a new form. My little vba app that does that also
updates the constants I use to refer to the control index's. In this example
cDeltaLast 30 > 31, so minimal need to rewrite the main code.

Regards,
Peter T

Show quoteHide quote
"Brian Schwartz" <br***@fishnetcomponentswos.com> wrote in message
news:#8C247agFHA.1044@tk2msftngp13.phx.gbl...
> Peter,
>
> > One reason I need to re-build is
> > that controls need to be added in the correct order, so not easy to
amend
> an
> > existing form.
>
> If you don't mind me asking, I'm curious what you mean by this--that is,
why
> this is so?
>
> Brian
>
> --
> Brian Schwartz
> FishNet Components
> http://www.fishnetcomponents.com
> Building better tools for developers - Be part of it!
>
>