Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-29-2018, 12:19 PM
Cosmo Cosmo is offline Show userForm from variable Windows Vista Show userForm from variable Office 2007
Competent Performer
Show userForm from variable
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default Show userForm from variable

I have several documents which use the same userForms, and I'm trying to make a function to show them in a specific order (Not all will contain the same forms, not always the same order). I have created an array with the forms in the order they need, but I can't seem to use the contents of the array to show the forms. I can use the TypeName and a Select Case block to show the form, but since not all documents will have all of the forms, this won't compile across the board. Plus, if I need to add/delete/rename any of the userforms, it will require updating this section, which I would prefer not to be hard-coded.



Code:
    ' Create an array using the needed forms in order:
    Dim dialogs(4) As UserForm
 
    ' NOTE: Using 'NEW' doesn't make any difference to functionality
    Set dialogs(0) = frmDataInput
    Set dialogs(1) = frmSummaryInput
    Set dialogs(2) = New frmCondition
    Set dialogs(3) = New frmSummaryInput
 
    ' The following code would be in a separate function, common to all documents
    Dim i As Integer
    Dim ct As Integer
 
    ct = UBound(dialogs)
    For i = 0 To ct
        Set dialog = dialogs(i)
 
        ' NOTE: This doesn't work:
        ' dialog.show
 
        ' Need to use 'Select Case' to activate forms
        Dim name As String
        name = TypeName(dialog)
        Select Case name
        Case "frmSummaryInput"
            frmSummaryInput.show
        Case "frmDataInput"
            frmDataInput.show
        Case "frmCondition"
            frmCondition.show
        End Select
 
    Next I
Is there a way to get the 'dialog' variable to run it's 'show' method without needing to hard-code all of the form names?
Reply With Quote
  #2  
Old 01-30-2018, 08:52 AM
Cosmo Cosmo is offline Show userForm from variable Windows Vista Show userForm from variable Office 2007
Competent Performer
Show userForm from variable
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

When I search the object browser, I find 4 hidden items with the .show and .hide methods:

Library.........Class......Member
Unknown5...._Form....Show
Unknown6...._Form....Show
Unknown7...._Form....Show
Unknown8...._Form....Show

The UserForm class is missing these methods, so I'm assuming that the UserForms used in the application are a subclass of this UserForm class, utilizing these hidden _Form classes? (my terminology here may not be completely accurate)

Is there any class that exposes the .show method that can be used as a variable to do what I need?
Reply With Quote
  #3  
Old 01-31-2018, 05:43 AM
gmaxey gmaxey is offline Show userForm from variable Windows 7 32bit Show userForm from variable Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

This seems to show all in the array:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 1/31/2018
Dim Dialogs(2) As Object
Dim lngIndex As Long
  Set Dialogs(0) = New UserForm1
  Set Dialogs(1) = New UserForm2
  Set Dialogs(2) = New UserForm3
  For lngIndex = 0 To UBound(Dialogs)
    Dialogs(lngIndex).Show
  Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 01-31-2018, 06:13 AM
Cosmo Cosmo is offline Show userForm from variable Windows Vista Show userForm from variable Office 2007
Competent Performer
Show userForm from variable
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

Thanks for the response.

I tried to replicate your results and at first, it wasn't working. Then I noticed that you dimmed the array as 'Object', when I had it as 'UserForm'. That seems to be the trick in getting it to work. Odd that the generic 'Object' works, but the specific 'UserForm' doesn't. I'm a bit rusty in VisualBasic, so these issues are easy to stumble upon.

I believe I now have what I need to get what I wanted to work. Thanks again.
Reply With Quote
  #5  
Old 01-31-2018, 12:37 PM
Cosmo Cosmo is offline Show userForm from variable Windows Vista Show userForm from variable Office 2007
Competent Performer
Show userForm from variable
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

Ok, apparently I'm even rustier in VBA than I had originally thought. I thought I had everything, but I'm having a problem that I believe I've run into before, but don't remember how to solve.

I'm passing the userForm from one function into a class, and instead of passing the UserForm itself, apparently the object I'm receiving as a parameter is the UserForm.Controls object.
Code:
Function test()
   Dim frm As UserForm
   Set frm = New UserForm1


   AddAsObject (frm)

   AddAsUserForm (frm)
End Function

Function AddAsObject(ByRef frm As Object)
   ' This works because the variable received is a 'Controls' object
End Function

Function AddAsUserForm(ByRef frm As UserForm)
   ' This DOESN'T work because the variable received is NOT a 'UserForm' object
End Function
So, how do I pass the UserForm as a parameter to another function?
Reply With Quote
  #6  
Old 01-31-2018, 12:59 PM
Cosmo Cosmo is offline Show userForm from variable Windows Vista Show userForm from variable Office 2007
Competent Performer
Show userForm from variable
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

For now, I have solved my issue with a bit of a hack. In the subFunction, I check the typeof the variable 'frm', and if it is 'Controls', I loop through the controls to get the parent UserForm. (NOTE, this won't work if there are no controls on the UserForm, but at least it is acceptable until I find a more proper way to make this work since that's never going to be the case in my program)

Code:
    If (TypeOf frm Is Controls) Then
        Dim ctrl As Control
        For Each ctrl In frm
            If (TypeOf ctrl.Parent Is UserForm) Then
                Set frm = ctrl.Parent
                Exit For
            End If
        Next
    End If
I'd prefer to code this properly, and not rely on this hack, so if anyone can tell me how to pass the userForm properly between the two functions, I would appreciate it very much.

Thanks.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Show userForm from variable Assigning a string variable to a userform label caption Larry_1 Excel Programming 3 12-18-2017 06:59 AM
Show userForm from variable Show Userform when Content Control CheckBox ticked derajlance Word VBA 1 05-13-2016 01:55 PM
Using Userform.Show in Excel 2013 BillCPA Excel Programming 0 05-04-2015 09:52 AM
Command button to show userform suddenly stopped working jpb103 Word VBA 0 05-22-2014 06:05 AM
Show userForm from variable Show userform without losing document focus? Or other method to get a graphic to pop? AlexR Word VBA 7 03-31-2013 12:17 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:24 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft