Run-time Error '13': Type mismatch (1 Viewer)

TobyMace

Registered User.
Local time
Today, 18:18
Joined
Apr 13, 2018
Messages
65
Hi all again!

I am writing what I thought was a simple bit of VBA but have become stuck. I am getting this error on the line:

If Unit_Type = "SP2240" Or "SP2241" Or "SP2242" Or "SP2270" Or "SP2271" Or "SP2280" Then

The rest of the code is just simple:

PreESS_Tested.Visible = False

I feel really silly and that it's a simple fix but any help is most appreciated.
 

jleach

Registered User.
Local time
Today, 14:18
Joined
Jan 4, 2012
Messages
308
Break out each OR into a full expression:

Code:
If Unit_Type = "ASDF" OR Unit_Type = "QWERT" OR Unit_Type = "ZXCV" Then

Tedious, but required. hth
 

TobyMace

Registered User.
Local time
Today, 18:18
Joined
Apr 13, 2018
Messages
65
I knew it was simple! Thank you!
 

jleach

Registered User.
Local time
Today, 14:18
Joined
Jan 4, 2012
Messages
308
No prob. Now that you're fixed, a few additional thoughts.

I usually prefer to use parentheses to make it a bit more readable, and line breaks perhaps. Not strictly required, but reading it later makes it more clear what's going on:

Code:
If (UnitType = "ASDF") _
    OR (UnitType = "CVBN") _
    OR (UnitType = "ERTY") _
    OR (UnitType = "QWER") _
    OR (UnitType = "ZXCV") Then

   ...

Also, you can get a bit more creative using string comparisons and delimiters to simplify the If expression:

Code:
'put all your possibles into a delimited string
Const MATCHES = ";ASDF;QWER;ZXCV;"

'check if the value, with the delimiter on each side, is present in the matches
If InStr(1, ";" & Unit_Type & ";", MATCHES) > 0 Then
   ...it was found
End If

There's other ways also, and more than a handful of this pattern of ORs can often indicate a less-than-normalized data structure somewhere further downstream (it's a bit of a code smell), but anyway, it's some more food for thought.

Cheers
 

TobyMace

Registered User.
Local time
Today, 18:18
Joined
Apr 13, 2018
Messages
65
I always wanted to make it look a bit neater but once a code worked I never wanted to muck it up so didn't dare touch it!

I'm completely self taught on Access so far by browsing existing databases at my firm and applying aspects to my one so I've been keeping it simple so far but I'll give that a go when I become more confident!

Thanks again!
 

Users who are viewing this thread

Top Bottom