Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Focus problemm

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
I have the code below, where if MTCEnd value is empty, it flags up a message box, shown remmed out. However on clicking the dialogue box, or remming it out iits inclusion in the procedure, I cannot get back focus to the control MTCEnd. If I rem out the setfocus line, and press the return key after exiting the dialogue box or without it if excluded, the focus returns to the control okay. The field is enabled and not locked. It has an input mask of 00:00:00:00 and is a bound field.

Any ideas, thanks


If IsNull(Me.MTCEnd) = True Then
MsgBox "Timecode End value not entered"
Me.MTCDuration.Visible = False
Me.Label20.Visible = False
Me.Accept1.Enabled = False
'Forms!mainform1!Subform1.Form.MTCEnd.SetFocus
'Me.MTCEnd.SetFocus
'Me.MTCEnd.Text = ""
Exit Sub
End If
 
In the BeforeUpdate event procedure of MTCEnd:
If IsNull(Me.MTCEnd) = True Then
MsgBox "Timecode End value not entered"
Me.MTCDuration.Visible = False
Me.Label20.Visible = False
Me.Accept1.Enabled = False
Cancel = True
Exit Sub
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Many thanks PHV. I am trying out your suggestion, however I am against the problem I always get, that is if a user enters nothing in the field and presses return the beforeupdate event does not get fired, I resort to using the exit event. I will let you know how I get on, thanks
 
Me.Anothercontrol.SetFocus
Me.MTCEnd.SetFocus

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Any code validating whether or not a control is empty (null) has to be in the form's BeforeUpdate event! As you've noted, if you enter the MTCEnd control and leave it empty (no matter how this is done) the control's BeforeUpdate event doesn't fire. Neither does it fire if the control is never entered! Hence the need to place validation in the Form's BeforeUpdate event.

Validation in a control's BeforeUpdate event is reserved for checking whether or not data actually entered meets certain requirements, such as is it all digitd, or if a SSN does it have the right formatting.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
This is driving me nuts. The beforeupdate of the form does not fire if user simply presses return when in field.

I tried putting in

Private Sub MTCStart_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Me.DummyOne.SetFocus
End If
End Sub


And on DummyOne getting focus send back to MTCStart but I get the error 2110, saying Microsoft cannot move focus to control DummyOne. I have tried the full path for the textbox as its on a subform but still will not move.

Regards

I have no other events firing on MTCStart.
 
Back to your first post (ie in the same event):
If IsNull(Me.MTCEnd) = True Then
MsgBox "Timecode End value not entered"
Me.MTCDuration.Visible = False
Me.Label20.Visible = False
Me.Accept1.Enabled = False
Me.DummyOne.SetFocus
Me.MTCEnd.SetFocus
Exit Sub
End If


Note: DummyOne should have NO event procedure.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
A whole day gone. The only way I can do it is by making sure either all other controls have their auto tab stops set to false, or have all other controls disabled until I get there. I tried removing the DummyOne event, but was still not allowed to go to it. A few hours more and I'll format the hard drive.
 
Howdy ZOR . . .

Validating with the control or form [blue]BeforeUpdate[/blue] is certainly one of the best places. However if its not working as you require, [blue]there's no law thats saids you can't validate else where![/blue] You just have to account wether the entire record will be save, forcing you to the next record. This is not the case for you and is academic to your needs.

Try the controls [blue]On Exit[/blue] event where you can use the [blue]Cancel[/blue] arguement:
Code:
[blue]   If IsNull(Me.MTCEnd) = True Then
      MsgBox "Timecode End value not entered"
      Me.MTCDuration.Visible = False
      Me.Label20.Visible = False
      Me.Accept1.Enabled = False
      [purple][b]Cancel = True[/b][/purple]
      Exit Sub
   End If[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Many thanks. Problem is the beforeupdate event does not fire if a user does not type in anything and hits return. At the moment I can reset focus using the onexit event providing there are no other controls to go to. Regards
 
Thanks TheAceman1, just tried the code it works, but if a user wants to cancel the operation, the messagebox goes round and round as the event passes out from the onexit. Getting near to HD Format!
 
ZOR . . .

After [blue]Cancel = True[/blue], on the next line enter [blue]Me.Undo[/blue] . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Thanks, but that frees up the user being able to use cancel, but it remmoves the previous fields entries. Magis roundabout. Regards
 

Am I wrong here, AceMan? How does placing the null value validation for a control in its On Exit event work if the user never moves to the control in question?

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
missinglinq . . .

Of course it won't fire! Since beforeupdate isn't working as Zor requires, I Thought OnExit would be the next best shot . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 

We have to assume we're talking about new records here; if you're worried about a given field being empty, you're generally worried about it when the record is new! If you're only wooried about a field being empty if another field has a given value, you'd handle all this in that field's event. Given that, it's kind of unreasonable to think that no data at all will be entered in the new record, and if any data is entered, even one keywtroke, then Form's BeforeUpdate event will fire! You simply can't test if a control is empty from any of that control's events, because nothing fires if that control is simply skipped!

The only workaround I can think of would be to use Me.MTCEnd.SetFocus in the Form_Current event. This would assure that the control got focus, and the LostFocus event could then be used.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Many thanks. I have got this so far.
It works, but not how I wanted it. I wanted focus to return to the MTCStart
and MTCEnd fields if a user skipped the fields or if the data was incorrect.

I still have to find out what snags there are if its an existing record edit as well as new. Too many permutations in this game.

1) MTCStart - Time Field
2) AName - Text
3) Comments - Text
4) Combo1 - Selected Field
5) Combo2 - Selected Field
6) MTCEnd - Time Field

The fields are filled in a given strict order as shown.

What I have done is I disabled fields 2 - 6.

Private Sub MTCStart_AfterUpdate()
If IsNull(Me.MTCStart) = False Then
Me.AName.SetFocus
End If
End Sub

Private Sub MTCStart_Exit(Cancel As Integer)
If IsNull(Me.MTCStart) = True Then
Me.MTCStart.SetFocus
Exit Sub
End If
End Sub
Exit Sub

Because all other fields were disabled, the focus returns okay if no input on
MTCStart field.

The other fields do not have events except the MTCEnd field

Private Sub MTCEnd_AfterUpdate()
If IsNull(Me.MTCEnd) = True Then
Me.MTCDuration.Visible = False
Me.MTCEnd.SetFocus - Although this code is here, it does not come back, it goes to the AName field
Exit Sub
End If

Private Sub MTCEnd_Exit(Cancel As Integer)
If IsNull(Me.MTCEnd) = True Then
MsgBox "Timecode End value not entered"
Me.MTCDuration.Visible = False
Me.Label20.Visible = False
Me.Accept1.Enabled = False
Exit Sub
End If

' Check data fields entry
If IsNull(Me.Start) = False And IsNull(Me.Comments) = False And IsNull(Me.MTCEnd) = False Then

' Now check Timecode Values
If Val(Me.MTCEnd) > Val(Me.MTCStart) Then
Me.MTCDuration.Visible = True
Me.Label20.Visible = True
Me.Dummy.SetFocus
DoDuration ' This is a procedure for working out time duration between MTCStart and MCEnd
Me.Accept1.Enabled = True
Exit Sub
Else
If Val(Me.MTCEnd) < Val(Me.MTCStart) Then
MsgBox "Timecode End value Less then Start"
Me.Dummy.SetFocus
Else
If Val(Me.MTCEnd) = Val(Me.MTCStart) Then
MsgBox "Timecode End value same as Timecode Start Value"
Me.Dummy.SetFocus
End If: End If: End If

Else
' Data missing
MsgBox "Incompleted Entry"
' Focus set to a dummy field

End If

End Sub
 
ZOR said:
[blue]The fields are filled in a given strict order as shown.[/blue]
Well . . . something I can make sense out of! Why didn't you say this in your post origination?

Be sure to disable/remove any code in Before Update, After Update, On Exit, or Lost focus events, for each control and the form ([blue]we don't need any intervening code here![/blue]).
[ol][li] In the [blue]code module[/blue] of the form, copy/paste the following function:
Code:
[blue]Public Function NoGood() As Boolean
   Dim ctl As Control, DL As String
   Dim Msg As String, Style As Integer, Title As String
   
   Set ctl = Screen.ActiveControl
   DL = vbNewLine & vbNewLine
   
   If IsNull(ctl) Then
      Msg = "Fields have to be entered in order " & _
            "and can't be blank!" & DL & _
            "You have to enter a value in '" & ctl.Name & "' " & _
            "in order to continue! . . ."
      Style = vbInformation + vbOKOnly
      Title = "Blank Field Notice! . . ."
      MsgBox Msg, Style, Title
      [purple][b]NoGood = True[/b][/purple]
   End If
   
   Set ctl = Nothing
   
End Function[/blue]
[/li]
[li]In the [blue]On Exit[/blue] event of the controls of interest, copy/paste the following:
Code:
[blue]   Cancel = NoGood[/blue]
[/li][/ol]
New record or not . . . the user is forced to follow the order! ([blue]tab order that is![/blue])

[blue]Thats it! . . . Give it a whirl and let me know . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Thanks. I took out all other code, and put in your code. If a user selects a new record, then changes hi mind, hitting the cancel button brings up your dialogue box until they enter something in the field then hit cancel. Regards
 
This problem is really driving me mad. I have altered the Function to allow a user to continue or cancel and get out if hitting return with an empty MTCStart field. That seems to be working, but my onexit from the last field is giving me a headache setting focus back to the MTCEnd field if wrong data is entered. Any ideas how I can bury this, thanks

Amended Function
Public Function NoGood() As Boolean
Dim ctl As Control, DL As String
Dim Msg As String, Style As Integer, Title As String, response As Integer

Set ctl = Screen.ActiveControl
DL = vbNewLine & vbNewLine

If IsNull(ctl) Then
' response = MsgBox("Fields have to be entered in order " & _
' "and can't be blank!" & DL & _
' "You have to enter a value in '" & ctl.Name & "' " & _
' "in order to continue! . . .")
' Style = vbInformation + vbYesNoCancel 'vbOKOnly
' Title = "Blank Field Notice! . . ."
' MsgBox Msg, Style, Title
' NoGood = True

response = MsgBox(prompt:="Fields have to be entered. Do you want to continue or cancel 'Yes' or 'No'.", Buttons:=vbYesNo)

If response = vbYes Then
' NoGood = True
Set ctl = Nothing
Else
Me.Undo
Cancel = True
Me.Cancel2.SetFocus
Cancel2_Click
End If
Set ctl = Nothing



End If


Private Sub MTCEnd_Exit(Cancel As Integer)
Cancel = NoGood

' Check data fields entry
If IsNull(Me.MTCEnd) = False Then

' Now check Timecode Values
If Val(Me.MTCEnd) > Val(Me.MTCStart) Then
Me.MTCDuration.Visible = True
Me.Label20.Visible = True
Me.Dummy.SetFocus
DoDuration
Me.Accept1.Enabled = True
Exit Sub
Else

If Val(Me.MTCEnd) < Val(Me.MTCStart) Then
MsgBox "Timecode End value Less then Start"
WANT FOCUS SENT BACK

Else
If Val(Me.MTCEnd) = Val(Me.MTCStart) Then
MsgBox "Timecode End value same as Timecode Start Value"
WANT FOCUS SENT BACK

End If: End If: End If: End If


End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top