Hi. I have a search form with 4 unbound text boxes, a command button for search, and a listbox to show search result. When double click the result, a main form(frmMain)opens.
The problem is the record on the Main form is locked and wouldn't move to a next record.
Here's my code for search button:
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strSQL1 As String, strSQL2 As String, strSQL3 As String, strSQL4 As String, strSQL5 As String, strOrder As String, strWhere As String, SQLStmt As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select statement for the RowSource
'tblMain.SID, tblMain.SName, tblDetail.SYear, tblDetail.SStartMonth
strSQL1 = "SELECT tblMain.SID, "
strSQL2 = "tblMain.SName, "
strSQL3 = "tblDetail.SYear, "
strSQL4 = "tblDetail.SStartMonth, "
strSQL5 = "tblDetail.DetID " & _
"FROM tblMain INNER JOIN tblDetail ON tblMain.SID = tblDetail.SID "
strWhere = "WHERE"
strOrder = "ORDER BY tblMain.SID;"
'Set the WHERE clause for the Listbox RowSource if information has been entered into a field on the form
If Not IsNull(Me.txtSID) Then '<--If the textbox txtSID contains no data THEN do nothing
strWhere = strWhere & " (tblMain.SID) Like '*" & Me.txtSID & "*' AND" '<--otherwise, apply the LIKE statment to the QueryDef
End If
If Not IsNull(Me.txtSName) Then
strWhere = strWhere & " (tblMain.SName) Like '*" & Me.txtSName & "*' AND"
End If
If Not IsNull(Me.txtSYear) Then
strWhere = strWhere & " (tblDetail.SYear) Like '*" & Me.txtSYear & "*' AND"
End If
If Not IsNull(Me.txtSStartMonth) Then
strWhere = strWhere & " (tblDetail.SStartMonth) Like '*" & Me.txtSStartMonth & "*' AND"
End If
'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the SQL to the RowSource of the listbox
SQLStmt = strSQL1 & " " & strSQL2 & " " & strSQL3 & " " & strSQL4 & " " & strSQL5 & "" & strWhere & " " & strOrder
Me.SearchList.RowSource = strSQL1 & " " & strSQL2 & " " & strSQL3 & " " & strSQL4 & " " & strSQL5 & "" & strWhere & " " & strOrder
End Sub
And Here's the code for listbox:
Private Sub SearchList_DblClick(Cancel As Integer)
Dim Criteria As String
Dim CurDB As Database
DoCmd.ShowAllRecords
DoCmd.OpenForm "frmMain", , , "[SID] = '" & Me![SearchList] & "'", , acDialog
End Sub
The Main form is bound to tblMain which only has SID(primary key) and SName. The Main form has a subform [tblDetail Subform] which has a variety of subforms in it, which has DetID as a primary key.
How could I make the opened frmMain functions correctly as it is opened directly?
Thanks in advance.
John
The problem is the record on the Main form is locked and wouldn't move to a next record.
Here's my code for search button:
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strSQL1 As String, strSQL2 As String, strSQL3 As String, strSQL4 As String, strSQL5 As String, strOrder As String, strWhere As String, SQLStmt As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select statement for the RowSource
'tblMain.SID, tblMain.SName, tblDetail.SYear, tblDetail.SStartMonth
strSQL1 = "SELECT tblMain.SID, "
strSQL2 = "tblMain.SName, "
strSQL3 = "tblDetail.SYear, "
strSQL4 = "tblDetail.SStartMonth, "
strSQL5 = "tblDetail.DetID " & _
"FROM tblMain INNER JOIN tblDetail ON tblMain.SID = tblDetail.SID "
strWhere = "WHERE"
strOrder = "ORDER BY tblMain.SID;"
'Set the WHERE clause for the Listbox RowSource if information has been entered into a field on the form
If Not IsNull(Me.txtSID) Then '<--If the textbox txtSID contains no data THEN do nothing
strWhere = strWhere & " (tblMain.SID) Like '*" & Me.txtSID & "*' AND" '<--otherwise, apply the LIKE statment to the QueryDef
End If
If Not IsNull(Me.txtSName) Then
strWhere = strWhere & " (tblMain.SName) Like '*" & Me.txtSName & "*' AND"
End If
If Not IsNull(Me.txtSYear) Then
strWhere = strWhere & " (tblDetail.SYear) Like '*" & Me.txtSYear & "*' AND"
End If
If Not IsNull(Me.txtSStartMonth) Then
strWhere = strWhere & " (tblDetail.SStartMonth) Like '*" & Me.txtSStartMonth & "*' AND"
End If
'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the SQL to the RowSource of the listbox
SQLStmt = strSQL1 & " " & strSQL2 & " " & strSQL3 & " " & strSQL4 & " " & strSQL5 & "" & strWhere & " " & strOrder
Me.SearchList.RowSource = strSQL1 & " " & strSQL2 & " " & strSQL3 & " " & strSQL4 & " " & strSQL5 & "" & strWhere & " " & strOrder
End Sub
And Here's the code for listbox:
Private Sub SearchList_DblClick(Cancel As Integer)
Dim Criteria As String
Dim CurDB As Database
DoCmd.ShowAllRecords
DoCmd.OpenForm "frmMain", , , "[SID] = '" & Me![SearchList] & "'", , acDialog
End Sub
The Main form is bound to tblMain which only has SID(primary key) and SName. The Main form has a subform [tblDetail Subform] which has a variety of subforms in it, which has DetID as a primary key.
How could I make the opened frmMain functions correctly as it is opened directly?
Thanks in advance.
John