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

Can I use same event procedures?

Status
Not open for further replies.

Blondie96

Programmer
Aug 12, 2004
119
0
0
US
I have a form that accepts

BegDate1 BegHour1 EndDate1 EndHour1
BegDate2 BegHour2 EndDate2 EndHour2
BegDate3 BegHour3 EndDate3 EndHour3
BegDate4 BegHour4 EndDate4 EndHour4
BegDate5 BegHour5 EndDate5 EndHour5

Each set of fields go through the same checks. Is it possible to use 1 event procedure that will handle all the similar functions for each field

ex.

OnFocus_BegHourX.... etc
OnLostFocus_EndDateX...

If so, please explain how.

Thanks,
Tamra
 
you can set up a function ie
function OnFocusBegHour()
dim ctl as control
set ctl=me.activecontrol
ctl.....
end function

and set the on focus event of each control to
=OnFocusBegHour()
 
Probably can...
Create a Function that requires a string input, place this function in the code window behind the form .. then pass the name of the control to the function

Function fCheckDate(strCtl as String)
If Not IsDate(Me(strCtl)) Then
MsgBox "A Date is Required"
Me.SometxtBoxName.SetFocus 'Set focus to some other textbox
Me(strCtl).SetFocus 'Return Focus to textbox being checked
End If
End Function

Then in the AfterUpdate Event for each of the textbox controls enter
= fCheckDate("TheControlName")

PaulF
 
How are ya Blondie96 . . . . .
Blondie96 said:
[blue]Each set of fields go through the same checks. [purple]Is it possible to use 1 event procedure that will handle all the similar functions for each field[/purple][/blue]
Yes its possible. Use the [purple]BeforeUpdate[/purple] event of the form to perform a [blue]full validation.[/blue] If everything 's ok the record is saved, if not, you can roll back saving with the [purple]Cancel[/purple] property and set the focus as necessary. The code would look something like the following:
Code:
[blue]   Dim Ctl As Control, flg As Boolean
   Dim Msg As String, style As Integer, title As String
   
   For Each Ctl In Me.Controls
      If Ctl.ControlType = acTextBox Then
         If (Ctl.Name = "BegDate1") Or (Ctl.Name = "BegDate2") Or _
            (Ctl.Name = "BegDate13") Or (Ctl.Name = "BegDate4") Or _
            (Ctl.Name = "BegDate1") Then
            
            flg = [purple][b]Your_Validation_Code[/b][/purple] [green]'returns true/False[/green]
            
            If Not flg Then
               Msg = "Your Message"
               style = vbCritical + vbOKOnly
               title = "Your TitleBar text"
               MsgBox Msg, style, title
               Cancel = True
               Me(Ctl.Name).SetFocus
               Exit For
            End If
         
         End If
      End If
   Next[/blue]

Calvin.gif
See Ya! . . . . . .
 
I will give you a specific example. This same code is used to validate the cboendHour3 field. I would like to modify it so that I can call it for cboendHour1,cboendHour2,cboendHour3,cboendHour4 ...cboendHour10.

How could I do that?

Private Sub cboEndHour3_Exit(Cancel As Integer)
If Not IsNull(dtEndDate3) Then
If IsNull(cboEndHour3) Then
Cancel = True
MsgBox "End Hour Required."
Me![cboEndHour3] = Null
Else
If cboEndHour3 < cboStartHour3 Then
If dtEndDate3 = dtStartDate3 Then
cboEndHour3 = Null
MsgBox "To Create this Time span, End Date Must be greater than Start " & _
"Date."
dtEndDate3 = Null
Forms!frmSchedHrsSpecificChnl!dtEndDate3.SetFocus
End If
End If
End If
Else
If Not IsNull(cboEndHour3) Then
MsgBox "End Date Required."
Forms!frmSchedHrsSpecificChnl!dtEndDate3.SetFocus
End If
End If
End Sub


Thanks,

Tamra
 
something like this might work:


Private Sub Form_BeforeUpdate(Cancel As Integer)
dim x as integer
for x = 1 to 10
If Not IsNull(Me("dtEndDate" & x)) Then
If IsNull(Me("cboEndHour" & x)) Then
Cancel = True
MsgBox "End Hour Required."
Me("cboEndHour" & x ) = Null
Else
If Me("cboEndHour" & x) < Me("cboStartHour" & x) Then
If Me("dtEndDate" & x) = Me("dtStartDate" & x) Then
Me("cboEndHour" & x) = Null
MsgBox "To Create this Time span, End Date Must be greater than Start " & _
"Date."
Me("dtEndDate" & x) = Null
Forms!frmSchedHrsSpecificChnl("dtEndDate" & x).SetFocus
End If
End If
End If
Else
If Not IsNull(Me("cboEndHour" & x)) Then
MsgBox "End Date Required."
Forms!frmSchedHrsSpecificChnl("dtEndDate" & x).SetFocus
End If
End If
Next x
End Sub

PaulF
 
Blondie96 . . . . .

The following code validates [blue]Date & Time[/blue], as well as any [blue]Range Crossovers[/blue], for a specific [purple]Index[/purple] of your fields. That is, you call a function, supplying the prime index of the fields.

As an example, since your using the [blue]OnExit[/blue] event of EndHour to do your checking, the event code for BegDate1 BegHour1 EndDate1 EndHour1 would be:
Code:
[blue]Private Sub EndHour1_Exit(Cancel As Integer)
   If Not DateTimeOK([purple][b]1[/b][/purple]) Then Cancel = True
End Sub[/blue]
For BegDate2 BegHour2 EndDate2 EndHour2 would be:
Code:
[blue]Private Sub EndHour2_Exit(Cancel As Integer)
   If Not DateTimeOK([purple][b]2[/b][/purple]) Then Cancel = True
End Sub[/blue]
As for the function, in the code module for the form, copy/paste the following code:
Code:
[blue]Public Function DateTimeOK(Idx As Integer) As Boolean
   Dim Msg As String, Style As Integer, Title As String
   Dim BD As String, ED As String, BH As String, EH As String
   Dim DL As String, qTxt As String, strIdx As String
   
   Style = vbCritical + vbOKOnly
   DL = vbNewLine & vbNewLine
   
   'Setup name references.
   strIdx = LTrim(Str(Idx))
   BD = "BegDate" & strIdx
   ED = "EndDate" & strIdx
   BH = "BegHour" & strIdx
   EH = "EndHour" & strIdx
   
   'Detect errors (any qTxt flags an error!)
   If Not IsDate(Me(BD)) Then
      qTxt = "'Beginning Date'"
      Me(BD).SetFocus
   ElseIf Not IsDate(Me(BH)) Then
      qTxt = "'Beginning Hour'"
      Me(BH).SetFocus
   ElseIf Not IsDate(Me(ED)) Then
      qTxt = "'Ending Date'"
      Me(ED).SetFocus
   ElseIf Not IsDate(Me(EH)) Then
      qTxt = "'Ending Hour'"
      Me(EH).SetFocus
   ElseIf Me(BD) >= Me(ED) Then
      qTxt = "Date"
      Me(BD).SetFocus
   ElseIf Me(BH) >= Me(EH) Then
      qTxt = "Hour"
      Me(BD).SetFocus
   End If
   
   If qTxt = "" Then
      DateTimeOK = True
    Else
      If qTxt = "Date" Or qTxt = "Hour" Then
         Msg = " Beginning " & qTxt & " is not less than 'Ending " & qTxt & "!" & DL & _
               "Check your " & qTxt & "s and try again . . ."
         Title = qTxt & "s Range Error! . . ."
         MsgBox Msg, Style, Title
      Else
         Msg = qTxt & " is empty or not a date!" & DL & _
               "Check the " & qTxt & " and try again . . ."
         Title = "Invalid or Missing Data Error! . . ."
         MsgBox Msg, Style, Title
      End If
   End If
         
End Function[/blue]
Per my earlier post, you want to reconsider using the BeforeUpdate event of the form. With slight modification they can all be checked, and saving rolled back if any errors are detected. This allows the user to breeze thru the form unfeathered, but prevent saving until all is correct.

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

Part and Inventory Search

Sponsor

Back
Top