Solved

SetFocus after Lost Focus

Posted on 2003-11-13
8
504 Views
Last Modified: 2008-03-10
Hi,
I have a function that validates the contents of a field using it's lost focus event. If the contents are invalid, then the focus should be set to the offending field, which seems impossible to do when using the lost focus event. Anyone know a way to do this ? Or a work around. I've tried using the 'After Update' and 'Before Update Event' to no avail.. Perhaps I'm missing something.

Thx in Advance
0
Comment
Question by:CHYRO
  • 4
  • 2
  • 2
8 Comments
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9738452
Try setting Cancel to 1 in the AfterUpdate event.
0
 
LVL 11

Assisted Solution

by:Quetzal
Quetzal earned 45 total points
ID: 9738620
AfterUpdate does not have a Cancel option.  Set Cancel to True in the BeforeUpdate event for the field.
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9738629
Sorry! BeforeUpdate
0
 

Author Comment

by:CHYRO
ID: 9738806
I tried that, but I keep getting
"You must save the field before you execute the GoToControl action, the GoToControl method, or the SetFocus method"

Here's my code:

Private Sub ctrCell_BeforeUpdate(Cancel As Integer)
Dim strTemp As String
strTemp = RemoveSpace(ctrCell)
If strTemp = "" Then
    Cancel = 1
    ctrCell.SetFocus
Else
    ctrCell = strTemp
End If
End Sub





Public Function RemoveSpace(strVal) As String
strVal = Trim(strVal)
AftSpacePoint = 1
Do While Not InStr(AftSpacePoint, strVal, " ") = 0
    AftSpacePoint = InStr(AftSpacePoint, strVal, " ") + 1
    strVal = Left(strVal, AftSpacePoint - 2) & Right(strVal, Len(strVal) - (AftSpacePoint - 1))
Loop
If Len(strVal) < 12 Then ' If the field contains 2 phones numbers
    If Len(strVal) <> 8 Then ' If the field contains 1 phone number and has the incorrect number of digits
        MsgBox "The phone number has not been entered in the correct format." & vbNewLine & _
        "The standard format is of the form '01 671755'."
        RemoveSpace = ""
        Exit Function
    Else ' Reformat the phone number with a space after the second digit.
        RemoveSpace = Left(strVal, 2) & " " & Right(strVal, 6)
    End If
End If
End Function




... Any ideas ?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Accepted Solution

by:
nmcdermaid earned 45 total points
ID: 9738842
You don't need the ctrCell.SetFocus. It stays in there anyway.


0
 

Author Comment

by:CHYRO
ID: 9738848
Its actually the line :-
ctrCell = strTemp
That is giving trouble. Is it possible to modify the field in the 'Before Update' Event.
0
 
LVL 11

Expert Comment

by:Quetzal
ID: 9738866
You can't change the value of the field inside BeforeUpdate, just display the message.
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9738886
You might find it handy to add a custom format for that field.

For example if you put this in the format property of the field:

(00) 000000

It will force the data to look like that... it will put zeros in if the user doesn't, making it a bit more obvious to the usre of they have entered something wrong.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now