Excellent idea AceMan!!!
I will try the idea of removing the requery/refresh and see how it flows.
The problem with the empty recordset I havetaken care of by checking the new recordsource to see if it returns row and if not making the detail invisible and not updating the recordset. It seems the focus goes to the detail and won't allow me to redirect.Here is the new mysterious ChangeRecordsource sub:
Private Sub ChangeRecordsource()
'refresh form from various change events
'PlantCode, ModelYear, Status, txtSearchPN
Dim strSearchPN As String
Dim strPlantCode As String
Dim strPartStatus As String
Dim strModelYear As String
Dim strSQL As String
Dim db As Database
Dim rsNewRecordsource As Recordset
'validate fields
If Me.PlantCode.Value = "" Then
MsgBox "No Value for PlantCode," & vbCrLf & vbCrLf & "Please select a value from the list." _
, vbOKOnly + vbExclamation, "Empty PlantCode Field."
Me.cmbPartStatus.SetFocus
Exit Sub
End If
If Me.cmbModelYear.Value = "" Then
MsgBox "No Value for Model Year." & vbCrLf & vbCrLf & "Please select a value from the list." _
, vbOKOnly + vbExclamation, "Empty Model Year Field."
Me.cmbModelYear.SetFocus
Exit Sub
End If
'set variables
If Nz(Me.txtSearchPN.Value, "") = "" Then
strSearchPN = Nz(Me.txtSearchPN.Value, "")
Else
strSearchPN = Me.txtSearchPN.Value & "*"
End If
strPlantCode = Me.cmbPlantCode.Value
strPartStatus = Nz(Me.cmbPartStatus.Value, "")
strModelYear = Me.cmbModelYear.Value
'conditions
If strSearchPN = "" Then
'values PartStatus null
If strPartStatus = "" Then
strSQL = "SELECT tblPartInfo.PlantCode, tblPartInfo.PartNumber, tblPartInfo.DOCK, tblPartInfo.Storage, tblPartInfo.Description," _
& " tblPartInfo.PartWeight, tblPartInfo.Bld_Rate, tblPartInfo.ValidatedBld_Rate," _
& " tblPartInfo.PackDensity, tblPartInfo.Container, tblPartInfo.Length, tblPartInfo.Width," _
& " tblPartInfo.Height, tblPartInfo.DataSource, tblPartInfo.PartStatus, tblPartInfo.NumberofStations, tblPartInfo.ModelYear" _
& " From tblPartInfo" _
& " WHERE (((tblPartInfo.PlantCode)=" & "'" & strPlantCode & "'" & ")" _
& " AND ((tblPartInfo.ModelYear)=" & "'" & strModelYear & "'" & "))" _
& " ORDER BY tblPartInfo.DOCK, tblPartInfo.Description;"
Else
strSQL = "SELECT tblPartInfo.PlantCode, tblPartInfo.PartNumber, tblPartInfo.DOCK, tblPartInfo.Storage, tblPartInfo.Description," _
& " tblPartInfo.PartWeight, tblPartInfo.Bld_Rate, tblPartInfo.ValidatedBld_Rate," _
& " tblPartInfo.PackDensity, tblPartInfo.Container, tblPartInfo.Length, tblPartInfo.Width," _
& " tblPartInfo.Height, tblPartInfo.DataSource, tblPartInfo.PartStatus, tblPartInfo.NumberofStations, tblPartInfo.ModelYear" _
& " From tblPartInfo" _
& " WHERE (((tblPartInfo.PlantCode)=" & "'" & strPlantCode & "'" & ")" _
& " AND ((tblPartInfo.PartStatus)=" & "'" & strPartStatus & "'" & ")" _
& " AND ((tblPartInfo.ModelYear)=" & "'" & strModelYear & "'" & "))" _
& " ORDER BY tblPartInfo.DOCK, tblPartInfo.Description;"
End If
Else
'values PartStatus null
If strPartStatus = "" Then
strSQL = "SELECT tblPartInfo.PlantCode, tblPartInfo.PartNumber, tblPartInfo.DOCK, tblPartInfo.Storage, tblPartInfo.Description," _
& " tblPartInfo.PartWeight, tblPartInfo.Bld_Rate, tblPartInfo.ValidatedBld_Rate," _
& " tblPartInfo.PackDensity, tblPartInfo.Container, tblPartInfo.Length, tblPartInfo.Width," _
& " tblPartInfo.Height, tblPartInfo.DataSource, tblPartInfo.PartStatus, tblPartInfo.NumberofStations, tblPartInfo.ModelYear" _
& " From tblPartInfo" _
& " WHERE (((tblPartInfo.PlantCode)=" & "'" & strPlantCode & "'" & ")" _
& " AND ((tblPartInfo.ModelYear)=" & "'" & strModelYear & "'" & ")" _
& " AND ((tblPartInfo.PartNumber) LIKE " & "'" & strSearchPN & "'" & "))" _
& " ORDER BY tblPartInfo.DOCK, tblPartInfo.Description;"
Else
strSQL = "SELECT tblPartInfo.PlantCode, tblPartInfo.PartNumber, tblPartInfo.DOCK, tblPartInfo.Storage, tblPartInfo.Description," _
& " tblPartInfo.PartWeight, tblPartInfo.Bld_Rate, tblPartInfo.ValidatedBld_Rate," _
& " tblPartInfo.PackDensity, tblPartInfo.Container, tblPartInfo.Length, tblPartInfo.Width," _
& " tblPartInfo.Height, tblPartInfo.DataSource, tblPartInfo.PartStatus, tblPartInfo.NumberofStations, tblPartInfo.ModelYear" _
& " From tblPartInfo" _
& " WHERE (((tblPartInfo.PlantCode)=" & "'" & strPlantCode & "'" & ")" _
& " AND ((tblPartInfo.PartStatus)=" & "'" & strPartStatus & "'" & ")" _
& " AND ((tblPartInfo.ModelYear)=" & "'" & strModelYear & "'" & ")" _
& " AND ((tblPartInfo.PartNumber) LIKE " & "'" & strSearchPN & "'" & "))" _
& " ORDER BY tblPartInfo.DOCK, tblPartInfo.Description;"
End If
End If
Set db = DBEngine.Workspaces(0).Databases(0)
Set rsNewRecordsource = db.OpenRecordset(strSQL)
If rsNewRecordsource.EOF = True And rsNewRecordsource.BOF = True Then
Me.Detail.Visible = False
Me.cmdAddPartToRoute.Visible = False
Else
Forms!frmPartInfo.RecordSource = strSQL
Forms!frmPartInfo.Requery
Forms!frmPartInfo.Refresh
Me.Detail.Visible = True
Me.cmdAddPartToRoute.Visible = True
End If
rsNewRecordsource.Close
Set rsNewRecordsource = Nothing
Set db = Nothing
DoEvents
Me.cmdClose.SetFocus
End Sub
Everything is working fine now, Thanks for the help!!!!!
Joel