visual basic code in subforms

G

Guest

How can I run visual basic code or macros in a subform that is inside an
unbound main form?

What I did was to create an unbound main form then I inserted a multipage
control on it. On every page of the multipage control, that is where I placed
my subforms. Now, I want to run macros or visual basic codes on my subforms.
However, when I open my main form and insert data on the subforms, the macros
I created in the subforms don't work. Instead an error message appears which
says "Run-time error '2487': The Object Type argument for the action of
method is blank or invalid.

What am I supposed to do?

Incidentally, I converted my macros to visual basic code but as of now, I
still have not used those codes knowing that they won't work just like the
macros.

Can you help me? Many thanks in advance. God bless...
 
A

Albert D.Kallal

the macros
I created in the subforms don't work.

If you are talking about regular code, or macros that you wrote for the
form, then they should continue to run when used as a sub-form.

However, do note that you can use references to the form.

forms!frmCustomerInfo!CompanyName

The above is a reference to the company name in a form called
frmcustomerInfo. If we now move the form to a sub-form, then the above
reference will NOT work anymore.

So, in code, you can, and often should use

me!Compnayname

Then, if you move the code to the sub-form, it will continue to work.

So, all of the event code, button code etc. in the form should continue to
function as normal when you move the form to a sub-form.

However, do be aware that references to the form values need be changed if
you did not use "me"

On the other hand, if you are trying to reference the sub-form, or run some
code that exists in the sub-form, then you have to first declare your
functions as public, and can use the flowing format.

forms!NameOfMainFormName!SubFormContorlName.form.NameOfFunction()

If you are in a "main" form, then the above can be shorted to

me!SubFormContorlName.form.NameOfFunction()

Do remember the functions must be declared as public for the above to work.

It is not clear to me if you are trying to run general code that exists in
the form, or call the form's code from a external source (such as a button
on a main form, or perhaps a custom menu bar). So, the code should as a
general rule work, but you got to be aware that references to values will
change, and how you "grab" values from other forms will often need to be
changed here.

It all comes down to what, where, and when you are tyring to run the code,
but general code in the form should contiue to work correclty if you all of
a sudden decide to the form now as a sub-form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top