Form Control Manipulation From A Module

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,648
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
This is likely a pretty basic concept, but I'm struggling.

How does one manipulate userform controls from a procedure in a module? For example, I have this procedure (frm_reset) in a module. It essentially is designed to reset the user controls in (on?) my userform "IFM_Title" to their defaults.

Rich (BB code):
Sub frm_reset()
    mbevents = False
    Dim mcnt As Long
    With UserForms("IFM_Title")
        cbx_pmodel.Value = ""
        cbx_res = ""
        cbx_res.Locked = True
        tbx_title.Value = ""
        chkbx_collected.Value = False
        chkbx_collected.Locked = True
        chkbx_np.Value = False
        chkbx_np.Locked = True
        tbx_titlecnt.Value = ""
        tbx_titlecnt.Locked = True
        tbx_checkedcnt.Value = ""
        tbx_checkedcnt.Locked = True
        tbx_collectedcnt.Value = ""
        bx_collectedcnt.Locked = True
        tbx_date.Value = ""
        tbx_date.Locked = True
        tbx_dur.Value = ""
        tbx_dur.Locked = True
        chkbx_group.Value = False
        chkbx_group.Locked = True
        With lbx_smodel
            .Height = 76
            .Value = ""
            .Visible = False
            .Locked = True
        End With
        lbl_smodel.Visible = False
        tbx_collections.Clear
        cbtn_submit.Enabled = False
        cbtn_delete.Enabled = False
        cbtn_edit.Enabled = False
        
        uniquelist2 'creates model list in dump
        mcnt = ws_modlist.Columns(12).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 1
        lbl_pmodel.Caption = "Primary Model (" & mcnt & " models)"
        lbl_smodel.Caption = "Secondary Model(s) (" & mcnt & " models)"
        cbx_pmodel.List = Application.WorksheetFunction.Transpose(ThisWorkbook.names("modlist").RefersToRange)
        lbx_smodel.List = Application.WorksheetFunction.Transpose(ThisWorkbook.names("modlist").RefersToRange)
        With cbx_res
            .AddItem = "8k (7680x4320)"
            .AddItem = "4k (3840x2160)"
            .AddItem = "2k (2560x1440)"
            .AddItem = "HD2 (1920x1080)"
            .AddItem = "HD1 (1280x720)"
            .AddItem = "SD3 (854x480)"
            .AddItem = "SD2 (640x360)"
            .AddItem = "SD1 (426x240)"
        End With
    End With
    mbevents = True
End Sub

The line in red leaves me with a "Type mismatch error". I tried "With Me" and goit an "Invalid use of Me keyword (clearly I have no concept of when to use Me). If I exclude the "With ... End With", the controls give me "Object required" errors. If I preceed the control with "Userforms ("IFM_Title") I get the "Type mismatch" error.

What is the proper way of doing what I wish to do.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi
You add to your code a parameter to accept the ME keyword you pass as an argument from your userform

Rich (BB code):
Sub frm_reset(ByVal Form As IFM_Title)
    mbevents = False
    Dim mcnt As Long
    With Form
        .cbx_pmodel.Value = ""
        .cbx_res = ""

'rest of code

Do not forget to include the period (full stop) in from of each of the controls which will display the IntelliSense for all the userforms properties.

To call your procedure from your userform, you add the me keyword as the argument

VBA Code:
frm_reset Me

Dave

1735930713487.png
 
Upvote 0
Solution
Change this:
VBA Code:
With UserForms("IFM_Title")
        cbx_pmodel.Value = ""

For this:
VBA Code:
With IFM_Title
        .cbx_pmodel.Value = ""
 
Upvote 0
Thank you both for your solutions. (both work).
Dante, when I get to this portion of my code ...
Rich (BB code):
With .cbx_res
            .AddItem = "8k (7680x4320)"
            .AddItem = "4k (3840x2160)"
            .AddItem = "2k (2560x1440)"
            .AddItem = "HD2 (1920x1080)"
            .AddItem = "HD1 (1280x720)"
            .AddItem = "SD3 (854x480)"
            .AddItem = "SD2 (640x360)"
            .AddItem = "SD1 (426x240)"
End With

The line in red results in an "Expected Function or variable" error.

.cbx_res is a userform combobox control.
 
Upvote 0
It must be without the = symbol

This way:

VBA Code:
    With IFM_Title
      With .cbx_res
        .AddItem "8k (7680x4320)"
        .AddItem "4k (3840x2160)"


      End With
    End With

😇
 
Upvote 0
Makes sense Dante. That's probably why I couldn't figure it out lol.
 
Upvote 0

Forum statistics

Threads
1,225,487
Messages
6,185,270
Members
453,285
Latest member
Wullay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top