If...Else not doing what I want

WineSnob

Not Bright but TENACIOUS
Local time
Today, 13:17
Joined
Aug 9, 2010
Messages
211
I am trying to set the value of txtCPdate to blank field If Me.txtCPno.value does not contain certain values. Here is what I have. It works correctly when then txtCPno = 220160 or 225203 or 225204 or "22051C". I want it to change to blank if you change the txtCPno to something other than the above. Right now it retains the value of the last date.
So if txtCPno <> 220160 or 225203 or 225204 or "22051C" Then txtCPdate = "". However this control filed needs to be filled in with the correct date which comes from a book. The 4 values listed are the most common values.

Private Sub txtCPno_Change()
Me.txtCPdate.Value = ""
If Me.txtCPno.Value = 220160 Then
Me.txtCPdate.Value = "5 / 2 / 2003"

ElseIf Me.txtCPno.Value = 225203 Then
Me.txtCPdate.Value = "3 / 1 / 1984"

ElseIf Me.txtCPno.Value = 225204 Then
Me.txtCPdate.Value = "5 / 1 / 1985"

ElseIf Me.txtCPno.Value = "22051C" Then
Me.txtCPdate.Value = "3 / 23 / 1995"
Else
Me.txtCPdate.Value = ""
End If
End Sub
 
In my view you are working with text boxes and the values would be strings , not numbers, so I'd be using quotes.
Also, why do you want to use the on Change event -- after every character???
And why the spaces in the ""Date format""?

I'd suggest a Select case construct (for clarity) and the After Update event

Private Sub txtCPno_AfterUpdate()

Select Case Me.txtCPno.Value
Case "220160"
Me.txtCPDate.Value = "5 / 2 / 2003"
Case "225203"
Me.txtCPDate.Value = "3 / 1 / 1984"
Case "225204"
Me.txtCPDate.Value = "5 / 1 / 1985"
Case "22051C"
Me.txtCPDate.Value = "3 / 23 / 1995"
Case Else
Me.txtCPDate.Value = " "
End Select
End Sub

Good luck
 
I am using a combobox with the 4 common values. Therefore the on change event. But I get what you are saying. Never used Case before.
Thanks that did it.
 

Users who are viewing this thread

Back
Top Bottom