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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Field with Focus ? 1

Status
Not open for further replies.

BabyPowder2u

Programmer
May 4, 2005
87
US
Is there a command that will tell me the name of the field that currently has the focus? Reason, I perform the same verification loop on 10 datefields named StartDate1...StartDate10.

I want to know which field has the focus, then I want to do a Right string command to get the # of that datefield. (just 1 digit is fine, I will know if a zero is returned it is the 10th datefield) Then I want to use it by:

for Y from 1 to (#-1)
' to verify things.
next Y

Any guidance is appreciated.
Thanks,
T
 
How are ya BabyPowder2u . . . . .

Code:
[blue]Screen.ActiveControl.Name[/blue]

Calvin.gif
See Ya! . . . . . .
 
How are ya BabyPowder2u . . . . .

Hit submit too soon.

You can also get the previous control that had the focus with:
Code:
[blue]Screen.PreviousControl.Name[/blue]

For a list of others, see the [purple]Screen Object[/purple] in VBE help.

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

Looking at your intent of validation, you don't need to know the name or use [blue]Screen.ActiveControl.Name[/blue].

Try this:
[ol][li]In the [blue]Tag property[/blue] of the date fields of interest, enter [purple]?Date[/purple].[/li]
[li]Here's the example code to run:
Code:
[blue]   Dim ctl As Control
   
   For Each ctl In Me.Controls
      If ctl.contrltype = acTextBox And ctl.Tag = "[purple][b]?Date[/b][/purple]" Then
         If [b]IsDate[/b](ctl) Then
            [green]'Your Validation Here[/green]
         Else
            [green]'Your Error Message if not a date![/green]
         End If
      End If
   Next[/blue]
[/li][/ol]
[purple]Just a thought . . .[/purple]

Calvin.gif
See Ya! . . . . . .
 
Thanks for the Thought AceMan,

I am using the below logic to force data entry into the first empty row
i.e. if they click datefield8 but datefield3 is empty, the cursor is moved to datefield3 for data entry.

I can see where using Screen.ActiveControl.Name I could parse the fieldseq# & loop 1 to (fieldseq# - 1).

I do not understand where to put the
"For each ctl In Me.controls..." you mentioned,
or even if it would provide what I need (to go to a specific location)


My code: (currently the x=1 to 8 is hard coded for each _gotfocus() event. I would like to get the activecontrol.name
and parse it to make it a variable controlled routine.


Private Sub cboStartDate9_GotFocus()
Dim X As Integer
For X = 1 To 8 'datefield#-1 (9-1)
If (Not IsDate(Me("cboStartDate" & CStr(X))) Or (IsNull(Me("cboStartDate" & CStr(X)))) _
Or (Me("cboStartDate" & CStr(X))) = "") Then
' Cancel = True
Me("cboStartDate" & CStr(X)).SetFocus
X = 10
End If
Next X

End Sub
 
BabyPowder2u . . . . .

Does the validation have to be in sequential order?

What I usually do is let the user breeze thru the form and perform all my validation in the forms [blue]BeforeUpdate[/blue] event. The event occurs just before saving, and one by one if any controls fail, the user is notified with a message, focus is set to that control, and saving is rolled back. All controls involved in validation have to pass in order for the record to be saved.

[purple]This also makes it alot easier than programming 8 controls, which takes alot more code.[/purple]

So . . . . . Ya think?

Calvin.gif
See Ya! . . . . . .
 
The consensus here is that most of the validation needs to be as entered, instead of re-directing after completion of form (though there will be checks again for certain fields before final processing to be sure they weren't skipped entirely)
This routine is intended to "quietly" put the user in the correct entry field (no prompts, errors or displays) just resetting focus. Doing this now, makes the programming for full processing later, much easier.
 
OK BabyPowder2u . . . . .

Be aware: a [blue]Null[/blue] is not a date, nor is an empty string [blue]""[/blue]! So you dont't have to check for these seperately when using the [purple]Isdate[/purple] function.

Now, in the [blue]GotFocus[/blue] event of the date controls, replace the code with the following line:
Code:
[blue]   Call DateValidate[/blue]
In the same code module, copy paste the following routine:
Code:
[blue]Public Sub DateValidate()
   Dim X As Integer, ctlName As String
   
   For X = 1 To 8
      Name = "cboStartDate" & CStr(X)
      
      If Not IsDate(Me(Name)) Then
          Me(Name).SetFocus
          Exit For
      End If
   Next

End Sub[/blue]
If you want to include [blue]cboStartDate9[/blue], change the count in [purple]For X[/purple].

Calvin.gif
See Ya! . . . . . .
 
Thank you very much Aceman1, that will reduce tremendously the amount of coding I need to do. I will also be able to use the same logic in another field group I will need to validate similarly.

T
 
BabyPowder2u . . . .

There's a slight error . . .
Code:
[blue][b]Change:[/b]
Dim X As Integer, [purple][b]ctlName[/b][/purple] As String
[b]To:[/b]
Dim X As Integer, [purple][b]Name[/b][/purple] As String[/blue]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top