Type Mismatch on If Statement of Combobox with a query as Row Source (1 Viewer)

Mackbear

Registered User.
Local time
Today, 00:13
Joined
Apr 2, 2019
Messages
168
Hi Good day!

Please help, I cannot figure out why I am getting type mismatch error here. pbrootcause1 is a combobox with a query as a rowsource, not sure if this is what is causing the problem. When I go on debug, all the variables have values. I have set them all up as a string, not sure what was missing.

Dim pbrc1, rcval1, rcval2, rcval3, rcval4, rcval5, rcval6, rcval7, rcval8 As String

pbrc1 = Me.pbrootcause1.Text
rcval1 = "Invalid Notice"
rcval2 = "Invalid Late Fee"
rcval3 = "Invalid Past Due"
rcval4 = "Prior to Live Balance"
rcval5 = "Vendor Issue"
rcval6 = "Setup Issue"
rcval7 = "Processing Issue"
rcval8 = "Payment Issue"


If pbrc1 = rcval1 Or rcval2 Or rcval3 Then
Me.pbrootcause2.Enabled = False
Me.pbrootcause3.Enabled = False
Me.pbrootcause4.Enabled = False
Else
If pbrc1 = rcval4 Or rcval5 Then
Me.pbrootcause3.Enabled = False
Me.pbrootcause4.Enabled = False
Else
If pbrc1 = rcval6 Or rcval7 Or rcval8 Then
Me.pbrootcause4.Enabled = False
End If
End If
End If

Me.pbrootcause2.Requery

Looking forward to your help, thanks in advance!
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:13
Joined
Mar 14, 2017
Messages
8,738
I have set them all up as a string
Incorrect. When you declare variables like this:
Dim pbrc1, rcval1, rcval2, rcval3, rcval4, rcval5, rcval6, rcval7, rcval8 As String

...You have actually only declared rcval8 as String. The rest will end up as Variants, behavior dependent on the good graces of VBA implicit conversion.

First thing I'd suggest since it's quick and simple to try, is to declare them properly and see if you still have the problem.
Dim pbrc1 as String, rcval1 as String, rcval2 as String, rcval3 as String, rcval4 as String, rcval5 as String, rcval6 as String, rcval7 as String, rcval8 As String

Next thing to correct:
Code:
If pbrc1 = rcval1 Or rcval2 Or rcval3 Then
Try:
Code:
If (pbrc1 = rcval1) Or (pbrc1=rcval2) Or (pbrc1=rcval3) Then

and correct similar lines accordingly as well.
 
Last edited:

Isaac

Lifelong Learner
Local time
Yesterday, 22:13
Joined
Mar 14, 2017
Messages
8,738
Also an If statement structured like this might be easier to visualize.
Just me. :)
Code:
If (pbrc1 = rcval1) Or (pbrc1 = rcval2) Or (pbrc1 = rcval3) Then
    Me.pbrootcause2.Enabled = False
    Me.pbrootcause3.Enabled = False
    Me.pbrootcause4.Enabled = False
ElseIf (pbrc1 = rcval4) Or (pbrc1 = rcval5) Then
    Me.pbrootcause3.Enabled = False
    Me.pbrootcause4.Enabled = False
ElseIf (pbrc1 = rcval6) Or (pbrc1 = rcval7) Or (pbrc1 = rcval8) Then
    Me.pbrootcause4.Enabled = False
End If
 

Mackbear

Registered User.
Local time
Today, 00:13
Joined
Apr 2, 2019
Messages
168
Incorrect. When you declare variables like this:
Dim pbrc1, rcval1, rcval2, rcval3, rcval4, rcval5, rcval6, rcval7, rcval8 As String

...You have actually only declared rcval8 as String. The rest will end up as Variants, behavior dependent on the good graces of VBA implicit conversion.

First thing I'd suggest since it's quick and simple to try, is to declare them properly and see if you still have the problem.
Dim pbrc1 as String, rcval1 as String, rcval2 as String, rcval3 as String, rcval4 as String, rcval5 as String, rcval6 as String, rcval7 as String, rcval8 As String

Next thing to correct:
Code:
If pbrc1 = rcval1 Or rcval2 Or rcval3 Then
Try:
Code:
If (pbrc1 = rcval1) Or (pbrc1=rcval2) Or (pbrc1=rcval3) Then

and correct similar lines accordingly as well.


Thanks a lot! This worked... I appreciate the quick response very much.
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:13
Joined
Mar 14, 2017
Messages
8,738
Thanks a lot! This worked... I appreciate the quick response very much.
Glad you got it working! You're very welcome.
FYI, good habits of variable declarations notwithstanding, I guess the main problem was probably those boolean statements, which ended up treating a string as if it could be a true/false statement.
I.E., this code will generate the same error for the same reason:
Code:
Sub Test()
Dim str As String
str = "something"
If str Then 'this line is treating a string as if it were a boolean statement
    MsgBox "ok"
End If
End Sub

Good luck with the rest of your project (y)
 

Users who are viewing this thread

Top Bottom