Runtime Error 13

penfold1992

Registered User.
Local time
Today, 20:35
Joined
Nov 22, 2012
Messages
169
I have a macro like so:

Private Sub cmb_Numeric_AfterUpdate()
If Me.cmbcolor.Value = "Red" Or "Blue" Then

Me.cmbnumeric.Value = "12"
End If
End Sub

however this gives me an error and im not sure why...

can anyone explain why. or give me a good work around?
 
You have got the If statement wrong.. It should be..
Code:
Private Sub cmb_Numeric_AfterUpdate()
     If Me.cmbcolor = "Red" [COLOR=Red][B]Or Me.cmbcolor =[/B][/COLOR] "Blue" Then
             Me.cmbnumeric.Value = "12"
     End If
End Sub
Also looking at the ComboBox name as Numeric I would guess the ComboBox returns a Number and you are comparing it against String.. Which explains your Run Time error.. Data Type Mismatch..So see what the RowSource of the ComboBox should be..
 
You have got the If statement wrong.. It should be..
Code:
Private Sub cmb_Numeric_AfterUpdate()
     If Me.cmbcolor = "Red" [COLOR=red][B]Or Me.cmbcolor =[/B][/COLOR] "Blue" Then
             Me.cmbnumeric.Value = "12"
     End If
End Sub
Also looking at the ComboBox name as Numeric I would guess the ComboBox returns a Number and you are comparing it against String.. Which explains your Run Time error.. Data Type Mismatch..So see what the RowSource of the ComboBox should be..

ahh thanks for that. The name "numeric" is a text string and not a number, its confusing but it can have letters in it too

In fact, I dont only have 2 options "Red"/"blue", i have a lot more... which means im going to have to list them all with Me.cmbcolor before each of them -sigh-

at least i know now though =)
 
hmm... ok so ive ran into another issue

If I want to incorporate cases with words... am I not going to have to write:

If cmbcolor.Value = "Red" Or "Blue" Or "Green" Or "Black" Then
number = 1
End If


Also is it possible to make Locked fields visibly different from unlocked fields?
 
As Paul said, you can use Select Case:
Code:
Private Sub cmbColor_AfterUpdate()

 Select Case cmb.Color.Value
  Case "Red", "Blue", "Black", "Green"
    Number = 1
  Case "Purple", "Pink", "Maroon"
   Number = 2
  Case Else
   Number = 9
 End Select

End Sub

Linq ;0)>
 
Am just expanding your example.. A simple SELECT would be something like..
Code:
Select Case Me.cmbcolor
    Case "Red", "Blue", "Green", "Black"
        number = 1
    Case "Orange", "White"
        number = 2
    Case Else
        number = 10
End Select
You can look into the Enabled property.. I think with a combination with the Locked property should do the trick..
Code:
Me.cmbcolor.Enabled = False
Me.cmbcolor.Locked = False

EDIT: Opened the link, and was distracted.. OOPS.. Thanks Linq.. :)
 
Last edited:
...is it possible to make Locked fields visibly different from unlocked fields

I had to test this, and then couldn't remember whose post I was responding to! :o

  1. Right-Click on the Textbox
  2. Click on Conditional Formatting
  3. Under Condition1 select 'Expression Is'
  4. In the Condition box (to the right of Condition1) enter [TextboxA].Locked=True replacing TextboxA with the actual name of the Textbox
  5. Select whatever formatting you want (BackColor?)
  6. Click on OK
Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom