Code correction for changing case

I

Irshad Alam

I have field on the form named Model, in which I want to change last 3
alphabet to capital, generall the data entry is like below :

wa470 llc
wa420
3bc

So, I tried to apply the below, which gives no result (no error or show no
change)


Private Sub Model_AfterUpdate()
Dim last3 As String
last3 = right(Me.Model, 3)
If right(Me.Model, 3) = "llc" Then
UCase (last3)
End If
End Sub

Please advice

Regards

Irshad
 
R

Rob Parker

Instead of UCase (last3), use UCase (right(Me.Model, 3))

You don't need to use the local variable last3 at all; and because your If
statement is a single line, you can simplify your code to:
Private Sub Model_AfterUpdate()
If right(Me.Model, 3) = "llc" Then UCase (right(Me.Model, 3))
End Sub

HTH,

Rob
 
I

Irshad Alam

Dear Sir,

I tried the code below as advised,

Private Sub Model_AfterUpdate()
If right(Me.Model, 3) = "llc" Then UCase (right(Me.Model, 3))
End Sub

It makes no difference/changes not even produces error, the text stays as it
is.

I tried on change event also, but same result.

Please check and advice.

Regards

Irshad
 
D

DrGUI

Try:

Me.Model.Value = Left(Me.Model.Value, Len(Me.Model.Value) - 3) &
UCase(Right(Me.Model.Value, 3))
 
J

John W. Vinson

I have field on the form named Model, in which I want to change last 3
alphabet to capital, generall the data entry is like below :

wa470 llc
wa420
3bc

So, I tried to apply the below, which gives no result (no error or show no
change)


Private Sub Model_AfterUpdate()
Dim last3 As String
last3 = right(Me.Model, 3)
If right(Me.Model, 3) = "llc" Then
UCase (last3)
End If
End Sub

Please advice

Regards

Irshad

You're misunderstanding how UCase() works. It doesn't change anything in your
form or in your table; it is just a function that returns a value. Try typing
Ctrl-G and type:

?Ucase("llc")

in the Immediate window and you'll see.

To change the value in the textbox, you must explicitly set the textbox to an
expression, e.g.

Private Sub Model_AfterUpdate()
If Right(Me!Model, 3) = "llc" Then
Me!Model = Left(Me!Model, Len(Me!Model) - 3) & "LLC"
End If
End Sub
 
M

Mike Painter

Irshad said:
Dear Sir,

I tried the code below as advised,

Private Sub Model_AfterUpdate()
If right(Me.Model, 3) = "llc" Then UCase (right(Me.Model, 3))
End Sub
Try it in a beforeupdate event.
 
J

John W. Vinson

Try it in a beforeupdate event.

Actually, somewhat confusingly, the AfterUpdate event is correct. Changing the
value of a control in that control's BeforeUpdate event will cause the
BeforeUpdate event to fire *again*, giving (in this case) an endless loop.

The problem is that Irshad is assuming that UCase means "do something to this
value" - it doesn't, it simply returns a variable consisting of the
changed-case version of its argument. He's not setting anything TO that value.

Just in case, the proper code (well, one of many ways to do this) would be

If Right(Me!Model, 3) = "llc" Then
Me!Model = Left(Me!Model), Len(Me!Model) - 3)) & "LLC"
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top