I am having a problem with a forms behavior. I have a Part Munber search text box that, when there are no rows returned from the search, makes the form detail section invisible. The problem is that when there are no rows returned, the Detail section is made invisible but I can't seem to get the focus back to the txtSearchPN. Any help/guidance is appreciated. My code -
Private Sub txtSearchPN_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo ErrorHandler
Me.Dirty = False
If IsNull(Me.txtSearchPN.Value) = True Then
ChangeRecordsource
ElseIf Len(Me.txtSearchPN.Value) >= 4 Then
ChangeRecordsource
End If
DoEvents
Do While Screen.ActiveControl.Name <> "txtSearchPN"
DoEvents
Me.txtSearchPN.SetFocus
Loop
If IsNull(Me.txtSearchPN.Value) = True Then
'do nothing
Else
Me.txtSearchPN.SelStart = Len(Me.txtSearchPN.Value) + 1
End If
ErrorHandler:
Select Case Err.Number
Case 0
'do nothing
Case 2110 'error generated if object does not have focus
Resume Next
Case Else
MsgBox "txtSearchPN_KeyUp Error" & vbCr & vbLf & "Record error number and description" _
& vbCr & vbLf & "Error Number = " & Err.Number _
& vbCr & vbLf & "Error Desc. = " & Err.Description, vbOKOnly + vbExclamation, "txtSearchPN_KeyUp Error"
End Select
End Sub
Private Sub ChangeRecordsource()
'refresh form from txtSearchPN change events
Dim strSearchPN As String
Dim strPlantCode As String
Dim strModelYear As String
Dim strSQL As String
'set variables
If Nz(Me.txtSearchPN.Value, "") = "" Then
strSearchPN = ""
Else
strSearchPN = Me.txtSearchPN.Value & "*"
End If
strPlantCode = Me.txtPlantCode.Value
strModelYear = Me.txtModelYear.Value
'conditions
If strSearchPN = "" Then
strSQL = "SELECT tblPartDumpImport.PART, tblPartDumpImport.DOCK, tblPartDumpImport.DESCRIPTION, tblPartDumpImport.BLD_RATE," _
& " tblPartDumpImport.PARTWT, tblPartDumpImport.CONTAINER, tblPartDumpImport.Supplier, tblKanBanImport.ROUTE," _
& " tblKanBanImport.STATION, tblKanBanImport.PACKSIZE, tblPartInfo.PartStatus" _
& " FROM (tblPartInfo RIGHT JOIN tblPartDumpImport ON (tblPartInfo.PartNumber = tblPartDumpImport.PART)" _
& " AND (tblPartInfo.PlantCode = tblPartDumpImport.PLANT))" _
& " LEFT JOIN tblKanBanImport ON (tblPartDumpImport.PLANT = tblKanBanImport.PLANT)" _
& " AND (tblPartDumpImport.BasePart = tblKanBanImport.BasePart)" _
& " WHERE (((tblPartDumpImport.PLANT)=[Forms]![frmMainMenu]![cmbPlantName])" _
& " AND ((tblPartDumpImport.ModelYear)=[Forms]![frmMainMenu]![cmbModelYear]));"
Else
strSQL = "SELECT tblPartDumpImport.PART, tblPartDumpImport.DOCK, tblPartDumpImport.DESCRIPTION, tblPartDumpImport.BLD_RATE," _
& " tblPartDumpImport.PARTWT, tblPartDumpImport.CONTAINER, tblPartDumpImport.Supplier, tblKanBanImport.ROUTE," _
& " tblKanBanImport.STATION, tblKanBanImport.PACKSIZE, tblPartInfo.PartStatus" _
& " FROM (tblPartInfo RIGHT JOIN tblPartDumpImport ON (tblPartInfo.PartNumber = tblPartDumpImport.PART)" _
& " AND (tblPartInfo.PlantCode = tblPartDumpImport.PLANT))" _
& " LEFT JOIN tblKanBanImport ON (tblPartDumpImport.PLANT = tblKanBanImport.PLANT)" _
& " AND (tblPartDumpImport.BasePart = tblKanBanImport.BasePart)" _
& " WHERE (((tblPartDumpImport.PLANT)=[Forms]![frmMainMenu]![cmbPlantName])" _
& " AND ((tblPartDumpImport.PART) LIKE " & "'" & strSearchPN & "'" & ")" _
& " AND ((tblPartDumpImport.ModelYear)=[Forms]![frmMainMenu]![cmbModelYear]));"
End If
Me.RecordSource = strSQL
Debug.Print Screen.ActiveForm.Name
Debug.Print Screen.Application.Name
' Debug.Print Screen.ActiveControl.Name
If Me.Recordset.RecordCount <> 0 Then
Me.Detail.Visible = True
Else
Me.Detail.Visible = False
End If
Debug.Print Screen.ActiveForm.Name
Debug.Print Screen.ActiveControl.Name
End Sub
Everything works fine until I enter somethiing in txtSearchPN that returns no records, then the detail section is hidden but I can not set the cursor or the focus back to txtSearchPN. Is it because the object that last had the focus was hidden?
Stumped - I have 2 forms with same problem now.
Any help is greatly appreciated.
Joel
Private Sub txtSearchPN_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo ErrorHandler
Me.Dirty = False
If IsNull(Me.txtSearchPN.Value) = True Then
ChangeRecordsource
ElseIf Len(Me.txtSearchPN.Value) >= 4 Then
ChangeRecordsource
End If
DoEvents
Do While Screen.ActiveControl.Name <> "txtSearchPN"
DoEvents
Me.txtSearchPN.SetFocus
Loop
If IsNull(Me.txtSearchPN.Value) = True Then
'do nothing
Else
Me.txtSearchPN.SelStart = Len(Me.txtSearchPN.Value) + 1
End If
ErrorHandler:
Select Case Err.Number
Case 0
'do nothing
Case 2110 'error generated if object does not have focus
Resume Next
Case Else
MsgBox "txtSearchPN_KeyUp Error" & vbCr & vbLf & "Record error number and description" _
& vbCr & vbLf & "Error Number = " & Err.Number _
& vbCr & vbLf & "Error Desc. = " & Err.Description, vbOKOnly + vbExclamation, "txtSearchPN_KeyUp Error"
End Select
End Sub
Private Sub ChangeRecordsource()
'refresh form from txtSearchPN change events
Dim strSearchPN As String
Dim strPlantCode As String
Dim strModelYear As String
Dim strSQL As String
'set variables
If Nz(Me.txtSearchPN.Value, "") = "" Then
strSearchPN = ""
Else
strSearchPN = Me.txtSearchPN.Value & "*"
End If
strPlantCode = Me.txtPlantCode.Value
strModelYear = Me.txtModelYear.Value
'conditions
If strSearchPN = "" Then
strSQL = "SELECT tblPartDumpImport.PART, tblPartDumpImport.DOCK, tblPartDumpImport.DESCRIPTION, tblPartDumpImport.BLD_RATE," _
& " tblPartDumpImport.PARTWT, tblPartDumpImport.CONTAINER, tblPartDumpImport.Supplier, tblKanBanImport.ROUTE," _
& " tblKanBanImport.STATION, tblKanBanImport.PACKSIZE, tblPartInfo.PartStatus" _
& " FROM (tblPartInfo RIGHT JOIN tblPartDumpImport ON (tblPartInfo.PartNumber = tblPartDumpImport.PART)" _
& " AND (tblPartInfo.PlantCode = tblPartDumpImport.PLANT))" _
& " LEFT JOIN tblKanBanImport ON (tblPartDumpImport.PLANT = tblKanBanImport.PLANT)" _
& " AND (tblPartDumpImport.BasePart = tblKanBanImport.BasePart)" _
& " WHERE (((tblPartDumpImport.PLANT)=[Forms]![frmMainMenu]![cmbPlantName])" _
& " AND ((tblPartDumpImport.ModelYear)=[Forms]![frmMainMenu]![cmbModelYear]));"
Else
strSQL = "SELECT tblPartDumpImport.PART, tblPartDumpImport.DOCK, tblPartDumpImport.DESCRIPTION, tblPartDumpImport.BLD_RATE," _
& " tblPartDumpImport.PARTWT, tblPartDumpImport.CONTAINER, tblPartDumpImport.Supplier, tblKanBanImport.ROUTE," _
& " tblKanBanImport.STATION, tblKanBanImport.PACKSIZE, tblPartInfo.PartStatus" _
& " FROM (tblPartInfo RIGHT JOIN tblPartDumpImport ON (tblPartInfo.PartNumber = tblPartDumpImport.PART)" _
& " AND (tblPartInfo.PlantCode = tblPartDumpImport.PLANT))" _
& " LEFT JOIN tblKanBanImport ON (tblPartDumpImport.PLANT = tblKanBanImport.PLANT)" _
& " AND (tblPartDumpImport.BasePart = tblKanBanImport.BasePart)" _
& " WHERE (((tblPartDumpImport.PLANT)=[Forms]![frmMainMenu]![cmbPlantName])" _
& " AND ((tblPartDumpImport.PART) LIKE " & "'" & strSearchPN & "'" & ")" _
& " AND ((tblPartDumpImport.ModelYear)=[Forms]![frmMainMenu]![cmbModelYear]));"
End If
Me.RecordSource = strSQL
Debug.Print Screen.ActiveForm.Name
Debug.Print Screen.Application.Name
' Debug.Print Screen.ActiveControl.Name
If Me.Recordset.RecordCount <> 0 Then
Me.Detail.Visible = True
Else
Me.Detail.Visible = False
End If
Debug.Print Screen.ActiveForm.Name
Debug.Print Screen.ActiveControl.Name
End Sub
Everything works fine until I enter somethiing in txtSearchPN that returns no records, then the detail section is hidden but I can not set the cursor or the focus back to txtSearchPN. Is it because the object that last had the focus was hidden?
Stumped - I have 2 forms with same problem now.
Any help is greatly appreciated.
Joel