Link to home
Start Free TrialLog in
Avatar of wlreimer
wlreimer

asked on

How to use ElseIf and With together

I inherited an application that has multiple versions of a single form that all access the same subform.

The subform then refers back to the form that opened it to obtain some information.  However, it's missing a reference to one of the parent forms and I'm not sure how to make it work correctly.

This is the code it has right now:
        If FormIsLoaded("Catalog Floorcovering") Then
            With Access.Forms("Catalog Floorcovering")
                m_strVendor = .Controls("Vendor")
                m_strProductNum = .Controls("VendorStyle")
                m_strProductOpt = .Controls("TextProductOption")
            End With
        Else
            With Access.Forms("Catalog")
            m_strVendor = .Controls("Vendor")
                m_strProductNum = .Controls("Product")
                m_strProductOpt = .Controls("ProductOption")
            End With
        End If

What it's missing is the option to get the info it needs when it's opened from a form named "Catalog CPA" which is just another (slightly different) version of "Catalog" or "Catalog Floorcovering".  

I thought I'd be able to use Else If, but that errors.

Any help with how to add:
            With Access.Forms("Catalog CPA")
            m_strVendor = .Controls("Vendor")
                m_strProductNum = .Controls("Product")
                m_strProductOpt = .Controls("ProductOption")
            End With

Would be greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Anthony Berenguel
Anthony Berenguel
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 Helen Feddema
Put in two ElseIf clauses, one for ElseIf FormIsLoaded("Catalog CPA" and one for Catalog Floorcovering.  Let's hope only one of these forms will be open at the same time; otherwise there will be a problem because the same subform is on all of them.

Also, I assume that you have a FormIsLoaded function, though it isn't really needed -- you can use the IsLoaded property directly, as in this code:

   If Application.CurrentProject.AllForms("fmnuMain").IsLoaded = True Then

Open in new window

You could also try a Select Case selection structure.

Good luck!
SOLUTION
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 Norie
Norie

Can't you find out which form is the subform's parent using the Parent property?

Then do whatever's needed based on that.

For example, something like this which assumes the code is in the subform
Select Case Me.Parent.Name

     Case   "Catalog Floorcovering"
            With Access.Forms("Catalog Floorcovering")
               m_strVendor = .Controls("Vendor")
               m_strProductNum = .Controls("VendorStyle")
               m_strProductOpt = .Controls("TextProductOption")
            End With

     Case "Catalog"
            With Access.Forms("Catalog")
               m_strVendor = .Controls("Vendor")
               m_strProductNum = .Controls("Product")
               m_strProductOpt = .Controls("ProductOption")
            End With

     Case "Catalog CPA"
            With Access.Forms("Catalog CPA")
                m_strVendor = .Controls("Vendor")
                m_strProductNum = .Controls("Product")
                m_strProductOpt = .Controls("ProductOption")
            End With
     End Select

Open in new window

Avatar of wlreimer

ASKER

Thanks everyone, quick and true.  I DID forget to add "Then" when I originally added the ElseIf line.