I cannot seem to be able to get focus on a control within another SubForm.
I'll explain what I'm doing first.
I have a main form "Project".
First SubForm "subteam" where my code lies.
Second SubForm "subeditemployee" where the code will take me.
The trigger is a NotInList event in where the employee they are trying to add to their team is not found. It then takes them the employee creation subform. The error is not until the very end.
Code:
Private Sub listname_NotInList(NewData As String, Response As Integer)
Dim names As Variant, rsemps As DAO.Recordset
Me.listname = ""
Me.listdept = ""
Response = 3
names = Split(NewData, " ")
SQL = "SELECT [Employees].* FROM [Employees] WHERE (("
max = UBound(names)
If max = 0 Then
formquery = "[Name] Like '*" & names(d) & "*'"
SQL = "SELECT [Employees].* FROM [Employees] WHERE ((([Employees].[Name]) Like '*" & names(d) & "*'));"
Else
For d = 0 To max
If d = 0 Then
formquery = "[Name] Like '*" & names(d) & "*'"
SQL = "SELECT [Employees].* FROM [Employees] WHERE ((([Employees].[Name]) Like '*" & names(d) & "*'"
Else
formquery = formquery & " Or [Name] Like '*" & names(d) & "*'"
SQL = SQL & " OR ([Employees].[Name]) Like '*" & names(d) & "*'"
End If
Next d
SQL = SQL & "));"
End If
Set rsemps = CurrentDb.OpenRecordset(SQL)
With rsemps
If .RecordCount <> 0 Then
.MoveLast
possibles = .RecordCount
If possibles = 1 Then
Response = MsgBox("Employee not found, did you mean '" & .Fields("Name") & "'?", vbYesNo, "Alert")
If Response = 6 Then
Me.listname = .Fields("Name")
If Nz(Me.listname, "") <> "" Then
Me.listdept = CurrentDb.OpenRecordset("SELECT [Employees].Team FROM [Employees] WHERE ((([Employees].Name)='" & Me.listname & "'));").Fields("Team")
End If
DoCmd.RunCommand acCmdSaveRecord
Me.Form.Requery
Else
Forms("Project").subpossibleemp.Form.Filter = formquery
Forms("Project").subpossibleemp.Form.FilterOn = True
Forms("Project").subpossibleemp.Form.Requery
Forms("Project").subpossibleemp.Visible = True
Forms("Project").subeditemployees.listemployees.SetFocus
End If
ElseIf possibles > 1 Then
MsgBox "Employee not found, please check possible other names, or create a new employee.", vbOKOnly, "Alert"
Forms("Project").subpossibleemp.Form.Filter = formquery
Forms("Project").subpossibleemp.Form.FilterOn = True
Forms("Project").subpossibleemp.Form.Requery
Forms("Project").subpossibleemp.Visible = True
Forms("Project").subeditemployees.listemployees.SetFocus
End If
Else
Forms("Project").subeditemployees.listemployees.SetFocus
End If
End With
Set rsemps = Nothing
End Sub