Are there disadvantages in putting code into Userforms instead of modules? Are there disadvantages in putting code into Userforms instead of modules? vba vba

Are there disadvantages in putting code into Userforms instead of modules?


Disclaimer I wrote the article Victor K linked to. I own that blog, and manage the open-source VBIDE add-in project it's for.

Neither of your alternatives are ideal. Back to basics.


To select different filters I have differnt (sic) userforms.

Your specifications demand that the user needs to be able to select different filters, and you chose to implement a UI for it using a UserForm. So far, so good... and it's all downhill from there.

Making the form responsible for anything other than presentation concerns is a common mistake, and it has a name: it's the Smart UI [anti-]pattern, and the problem with it is that it doesn't scale. It's great for prototyping (i.e. make a quick thing that "works" - note the scare quotes), not so much for anything that needs to be maintained over years.

You've probably seen these forms, with 160 controls, 217 event handlers, and 3 private procedures closing in on 2000 lines of code each: that's how badly Smart UI scales, and it's the only possible outcome down that road.

You see, a UserForm is a class module: it defines the blueprint of an object. Objects usually want to be instantiated, but then someone had the genius idea of granting all instances of MSForms.UserForm a predeclared ID, which in COM terms means you basically get a global object for free.

Great! No? No.

UserForm1.ShowdecisionInput1 = UserForm1.decisionIf decisionInput1 Then  UserForm2.ShowElse  UserForm3.ShowEnd If

What happens if UserForm1 is "X'd-out"? Or if UserForm1 is Unloaded? If the form isn't handling its QueryClose event, the object is destroyed - but because that's the default instance, VBA automatically/silently creates a new one for you, just before your code reads UserForm1.decision - as a result you get whatever the initial global state is for UserForm1.decision.

If it wasn't a default instance, and QueryClose wasn't handled, then accessing the .decision member of a destroyed object would give you the classic run-time error 91 for accessing a null object reference.

UserForm2.Show and UserForm3.Show both do the same thing: fire-and-forget - whatever happens happens, and to find out exactly what that consists of, you need to dig it up in the forms' respective code-behind.

In other words, the forms are running the show. They're responsible for collecting the data, presenting that data, collecting user input, and doing whatever work needs to be done with it. That's why it's called "Smart UI": the UI knows everything.

There's a better way. MSForms is the COM ancestor of .NET's WinForms UI framework, and what the ancestor has in common with its .NET successor, is that it works particularly well with the famous Model-View-Presenter (MVP) pattern.


The Model

That's your data. Essentially, it's what your application logic need to know out of the form.

  • UserForm1.decision let's go with that.

Add a new class, call it, say, FilterModel. Should be a very simple class:

Option ExplicitPrivate Type TModel    SelectedFilter As StringEnd TypePrivate this As TModelPublic Property Get SelectedFilter() As String    SelectedFilter = this.SelectedFilterEnd PropertyPublic Property Let SelectedFilter(ByVal value As String)    this.SelectedFilter = valueEnd PropertyPublic Function IsValid() As Boolean    IsValid = this.SelectedFilter <> vbNullStringEnd Function

That's really all we need: a class to encapsulate the form's data. The class can be responsible for some validation logic, or whatever - but it doesn't collect the data, it doesn't present it to the user, and it doesn't consume it either. It is the data.

Here there's only 1 property, but you could have many more: think one field on the form => one property.

The model is also what the form needs to know from the application logic. For example if the form needs a drop-down that displays a number of possible selections, the model would be the object exposing them.


The View

That's your form. It's responsible for knowing about controls, writing to and reading from the model, and... that's all. We're looking at a dialog here: we bring it up, user fills it up, closes it, and the program acts upon it - the form itself doesn't do anything with the data it collects. The model might validate it, the form might decide to disable its Ok button until the model says its data is valid and good to go, but under no circumstances a UserForm reads or writes from a worksheet, a database, a file, a URL, or anything.

The form's code-behind is dead simple: it wires up the UI with the model instance, and enables/disables its buttons as needed.

The important things to remember:

  • Hide, don't Unload: the view is an object, and objects don't self-destruct.
  • NEVER refer to the form's default instance.
  • Always handle QueryClose, again, to avoid a self-destructing object ("X-ing out" of the form would otherwise destroy the instance).

In this case the code-behind might look like this:

Option ExplicitPrivate Type TView    Model As FilterModel    IsCancelled As BooleanEnd TypePrivate this As TViewPublic Property Get Model() As FilterModel    Set Model = this.ModelEnd PropertyPublic Property Set Model(ByVal value As FilterModel)    Set this.Model = value    ValidateEnd PropertyPublic Property Get IsCancelled() As Boolean    IsCancelled = this.IsCancelledEnd PropertyPrivate Sub TextBox1_Change()    this.Model.SelectedFilter = TextBox1.Text    ValidateEnd SubPrivate Sub OkButton_Click()    Me.HideEnd SubPrivate Sub Validate()    OkButton.Enabled = this.Model.IsValidEnd SubPrivate Sub CancelButton_Click()    OnCancelEnd SubPrivate Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)    If CloseMode = VbQueryClose.vbFormControlMenu Then        Cancel = True        OnCancel    End IfEnd SubPrivate Sub OnCancel()    this.IsCancelled = True    Me.HideEnd Sub

That's literally all the form does. It isn't responsible for knowing where the data comes from or what to do with it.


The Presenter

That's the "glue" object that connects the dots.

Option ExplicitPublic Sub DoSomething()    Dim m As FilterModel    Set m = New FilterModel    With New FilterForm        Set .Model = m 'set the model        .Show 'display the dialog        If Not .IsCancelled Then 'how was it closed?            'consume the data            Debug.Print m.SelectedFilter        End If    End WithEnd Sub

If the data in the model needed to come from a database, or some worksheet, it uses a class instance (yes, another object!) that's responsible for doing just that.

The calling code could be your ActiveX button's click handler, New-ing up the presenter and calling its DoSomething method.


This isn't everything there is to know about OOP in VBA (I didn't even mention interfaces, polymorphism, test stubs and unit testing), but if you want objectively scalable code, you'll want to go down the MVP rabbit hole and explore the possibilities truly object-oriented code bring to VBA.


TL;DR:

Code ("business logic") simply doesn't belong in forms' code-behind, in any code base that means to scale and be maintained across several years.

In "variant 1" the code is hard to follow because you're jumping between modules and the presentation concerns are mixed with the application logic: it's not the form's job to know what other form to show given button A or button B was pressed. Instead it should let the presenter know what the user means to do, and act accordingly.

In "variant 2" the code is hard to follow because everything is hidden in userforms' code-behind: we don't know what the application logic is unless we dig into that code, which now purposely mixes presentation and business logic concerns. That is exactly what the "Smart UI" anti-pattern does.

In other words variant 1 is slightly better than variant 2, because at least the logic isn't in the code-behind, but it's still a "Smart UI" because it's running the show instead of telling its caller what's happening.

In both cases, coding against the forms' default instances is harmful, because it puts state in global scope (anyone can access the default instances and do anything to its state, from anywhere in the code).

Treat forms like the objects they are: instantiate them!

In both cases, because the form's code is tightly coupled with the application logic and intertwined with presentation concerns, it's completely impossible to write a single unit test that covers even one single aspect of what's going on. With the MVP pattern, you can completely decouple the components, abstract them behind interfaces, isolate responsibilities, and write dozens of automated unit tests that cover every single piece of functionality and document exactly what the specifications are - without writing a single bit of documentation: the code becomes its own documentation.