rubberduck-vba / MVVM

Model-View-ViewModel Infrastructure for VBA/VB6
MIT License
56 stars 16 forks source link

Run-time error '13': Type mismatch #10

Open tothzola opened 3 years ago

tothzola commented 3 years ago

error

Hello, If I want to implement a validation to ComboBox control and have the following error: Run-time error '13': Type mismatch on this line when it evaluates to TRUE:

If Not Validator Is Nothing Then
    BindingBase.AsINotifyValidationError.RegisterHandler ValidationManager
End If

BindPropertyPath:

Context.Bindings.BindPropertyPath ViewModel, "ComboBoxItemIndex", Me.ComboBox, _
            Mode:=TwoWayBinding, _
            UpdateTrigger:=OnPropertyChanged, _
            Validator:=New RequiredSelectionValidator, _
            ValidationAdorner:=ValidationErrorAdorner.Create(Target:=Me.ComboBox, _
            TargetFormatter:=ValidationErrorFormatter.WithErrorBorderColor.WithErrorBackgroundColor)

RequiredSelectionValidator Class:

'@ModuleDescription "A validator that requires a ComboBox Item to be selected."
Option Explicit

Implements IValueValidator

Private Function IValueValidator_IsValid(ByVal value As Variant, ByVal Source As IBindingPath, ByVal Target As IBindingPath) As Boolean
    IValueValidator_IsValid = value > -1
End Function

Private Property Get IValueValidator_Message() As String
    IValueValidator_Message = "Value cannot be empty."
End Property

Private Property Get IValueValidator_Trigger() As BindingUpdateSourceTrigger
    IValueValidator_Trigger = OnExit
End Property

Any suggestions?

retailcoder commented 3 years ago

Hi there!

It looks like ValidationManager isn't implementing INotifyValidationError - I think I was working around that area (that and ListBox bindings) when I last touched this code.

Assuming the ViewModel property is a Long, then the binding itself seems fine... I'll take a look at my local copy of the workbook/project to see if there aren't any changes I haven't pushed, but it looks like validation is kind of broken at the moment.

Note, I'm still not convinced writing a MVVM framework in VBA is the best idea... thinking twinBASIC could be what we're missing to make this happen (without needing to import hundreds of classes in a VBA project). At this stage MVVM in VBA is very much at proof-of-concept level, I wouldn't push to use it for real production work (although that could be very cool!)

tothzola commented 3 years ago

Hello @retailcoder, 😊. Thanks for the quick feedback.

Well I didn't notice that with the ValidationManager, maybe because I'm not that familiar with this whole framework. Have to admit that I'm novice in some areas and struggling to understand the whole picture.

In other order of thinking maybe you are right abought VBA -> and the MVVM pattern maybe it should be done as a Library, or in my case is this overkill? I don't know.

How would you advise or do such a complex project (see further down)? Would you implement this pattern (MVVM) or just a combination of MVC-MVP?

If you have a little time to investigate the concept of the project what I'm trying to achieve in excel, to have an idea, could you please look at the project (debitor-crud) I wrote some time ago, you probably will have a laugh about the style I wrote it. Everything lives in Excel, and that is the whole point because the file is placed on SharePoint where different users have access and can fill in the data's/values in a UserForm presented, values must be validated. It is database of customers data incapsulated in excel.

Back then I didn't know anything about OOP or how could I write some concepts more efficiently. I was so happy when I found your tutorials and Rubberduck-VBA, finally something else then a simple macro. Rubberduck-VBA changed everything a thought I knew about VBA and programming! 🤷‍♂️ Starving to read more about your knowledge to VBA and patterns! 👏

It is really hard to find some good tutorials/examples in VBA for my case, that can be maintained easily for future changes. Exactly the principals of SOLID-> (O-Open for extensions and closed for modifications) in OOP programming this is my vision of how I would, try to build the project.

That is may real problem because the project pointed in this message works and currently used in production, but every new changes more or less I must change half of the code I wrote and some times (mostly😫) this becomes very time consuming and frustrating. BIG BIG thanks, looking forward hearing from you cheers.

retailcoder commented 3 years ago

I downloaded your project (and added myself as a watcher on your repository), I'll have a look and come back here with my findings. MVVM in VBA is definitely very very much an experimental thing, but it should be possible to incrementally refactor your existing working code into a more OOP design like Model-View-Presenter.

The central idea is always the same: separate the data from the rest of the code, then isolate and abstract away & inject/set the dependencies from the outside.

I'm not promising anything, but do you mind if I go ahead and refactor your code and then write a blog post (or series) about the refactoring process, showing the thought process and explaining the code changes along the way?

tothzola commented 3 years ago

@retailcoder Wow, thank you. 😌

That would be awesome to read about! Glad that you are interested and willing to refactor and document it as a post or series.

Looking forward to read about it.

You made my Day, awesome!!! Thanks Cheers

tothzola commented 3 years ago

Hello @retailcoder , I hade some other projects ongoing so I hade no time to investigate further this problem. Still have a question regarding MVVM Framework. If you consider building with twinBASIC as a library than the file must be installed on a local computer in order to have it work with the excel project. If that is the case, that in my case it can't be as a solution. I need the framework to be available without installation, project file sits on website (SharePoint) and is is used by 50+ colleges which all have excel 2019 local installed without the possibility to install other add ins, installations are generally blocked by system admin.

Could you please advise how can the Validation type mismatch error be fixed? Due to the complexity I still can't figure this out.

Thanks for your reply.