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

Help With Search Form Results 2

Status
Not open for further replies.

caykamanj1966

Technical User
Jun 23, 2013
45
0
0
US
All,
I have the below code for a search form. It works perfectly, but I have a unbound field (combo box) called: txtFilterStatus and the field in the table is called: status.

That combox has several values and 2 of those values are: Activated and Deactivated.

If I search for Activated, it also gives me the Deactivated records, which I don't want in the results. Of course, it is because the word "activated" is also in deactivated.

How do I modify the code below to not get the deactivated records, when searching for the activated records?:

Code:
Private Sub Command137_Click()

On Error GoTo ErrorHandler

'Set the Dimensions of the Module
Dim strSQL As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()

'Constant Select statement for the Query definition
'strSQL = "SELECT tbl_weeklyMAs.ma_id,"
'strSQL = strSQL & "tbl_weeklyMAs.impacts, tbl_weeklyMAs.submit_date, tbl_weeklyMAs.status, tbl_weeklyMAs.title, tbl_weeklyMAs.planned_start_date, tbl_weeklyMAs.planned_end_date,"
'strSQL = strSQL & "tbl_weeklyMAs.requestor "
'
strSQL = "SELECT tbl_weeklyMAs.* "
strSQL = strSQL & "FROM tbl_weeklyMAs "
strWhere = "WHERE"

strOrder = "ORDER BY tbl_weeklyMAs.ma_id;"

'Set the WHERE clause for the QueryDef if information has been entered into a field on the form

If Not IsNull(Me.txtFilterImpacts) And Me.txtFilterImpacts <> "" Then
'<--If the textbox ma_id and the other text boxes contains no data THEN do nothing
strWhere = strWhere & " (tbl_weeklyMAs.impacts) Like '*" &
Me.txtFilterImpacts & "*' AND" '<--otherwise, apply the LIKE statement to the QueryDef
End If

If Not IsNull(Me.txtFilter_ma_id) And Me.txtFilter_ma_id <> "" Then
strWhere = strWhere & " (tbl_weeklyMAs.ma_id) Like '*" &
Me.txtFilter_ma_id & "*' AND"
End If

If Not IsNull(Me.txtFilterSubmitDate) And Me.txtFilterSubmitDate <> "" Then
strWhere = strWhere & " (tbl_weeklyMAs.submit_date) Like '*" &
Me.txtFilterSubmitDate & "*' AND"
End If

If Not IsNull(Me.txtFilterStatus) And Me.txtFilterStatus <> "" Then
strWhere = strWhere & " (tbl_weeklyMAs.status) Like '*" &
Me.txtFilterStatus & "*' AND"
End If

If Not IsNull(Me.txtFilterTitle) And Me.txtFilterTitle <> "" Then
strWhere = strWhere & " (tbl_weeklyMAs.title) Like '*" &
Me.txtFilterTitle & "*' AND"
End If

If Not IsNull(Me.txtFilterRequestor) And Me.txtFilterRequestor <> "" Then
strWhere = strWhere & " (tbl_weeklyMAs.requestor) Like '*" &
Me.txtFilterRequestor & "*' AND"
End If

If Not IsNull(Me.txtFilterPlannedStartDate) Then
strWhere = strWhere & " (tbl_weeklyMAs.planned_start_date) >= #" &
Me.txtFilterPlannedStartDate & "# AND"
End If

If Not IsNull(Me.txtFilterPlannedEndDate) Then
strWhere = strWhere & " (tbl_weeklyMAs.planned_end_date) <= #" &
Me.txtFilterPlannedEndDate & "# AND"
End If

'Remove the last AND from the SQL statement
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'MsgBox strSQL & " " & strWhere & " " & strOrder
'Pass the QueryDef to the query
Set qryDef = dbNm.QueryDefs("qry_Search")
    qryDef.SQL = strSQL & " " & strWhere & " " & strOrder

Dim dbs As DAO.Database, rst As DAO.Recordset
Dim rsCnt As Integer 'the counter

Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("qry_Search", dbOpenDynaset)

With rst
   If .recordcount > 0 Then 'What you want done
   DoCmd.OpenForm "frm_SearchResults"

   Else
       MsgBox "There is no data that meets your criteria.  Please try again.", vbOKOnly, "No Data Found" 'What you want done

   DoCmd.OpenForm "frm_search_MAs"

   End If
   End With

   Set dbs = Nothing
   Set rst = Nothing

   Me.txtFilterImpacts = Null
   Me.txtFilter_ma_id = Null
   Me.txtFilterSubmitDate = Null
   Me.txtFilterStatus = Null
   Me.txtFilterTitle = Null
   Me.txtFilterPlannedStartDate = Null
   Me.txtFilterPlannedEndDate = Null
   Me.txtFilterRequestor = Null

   ExitHandler:
       Exit Sub

   ErrorHandler:

       If Err = 2489 Then
          Resume ExitHandler
       Else
          MsgBox Err.Description
          Resume ExitHandler

   End If
End Sub

Any help will greatly be appreciated and thanks in advance!
 
What are possible structures of text around activated/deactivated substrings in the field that make necessary '*' in the pattern in Like operator?

combo
 
Use = rather than like and remove the *:

Code:
If Not IsNull(Me.txtFilterStatus) And Me.txtFilterStatus <> "" Then
    strWhere = strWhere & " (tbl_weeklyMAs.status) [highlight #FCE94F]=[/highlight] '" &
    Me.txtFilterStatus & "' AND"
End If

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
I would go with Duane's solution, but aside from that...

"(combo box) called: txtFilterStatus" - wouldn't be nice to have a combo box be named as combo box: cboFilterStatus, and not as a text box txtFilterStatus?

Also, I would have a table tbl_Status that would look something like:
[pre]ID Status
1 New
2 Activated
3 Deactivated
4 Goofing Off
.....[/pre]

Can you imagine how easy it would be to have numbers (IDs) stored in your other table(s) instead of "Activated" or "Deactivated" or whatever?

BTW, all of your:[tt]
If Not IsNull(Me.cboFilterStatus) And Me.cboFilterStatus <> "" Then[/tt]
could be a lot shorter: [tt]
If (Me.cboFilterStatus & "") <> "" Then[/tt]
or: [tt]
If Len(Me.cboFilterStatus & "") > 0 Then[/tt]
:)

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
dhookum thanks again my friend! As usual, you are tha' BOMB, meaning good:) That worked!

Andrzejek, thank you for pointing out my naming convention for combo boxes and I have fixed that.

I really appreciate everyone's help with this and I hope you guys have an awesome day.

Go Nuggets!!!:)
 
All, I have another issue with this code. I hope someone can help me figure this one out.

When I try to do a search date range for the below, it is not giving me the correct dates.

Say for instance if I put in a range of "5/1/2023" for "txtFilterPlannedStartDate and "5/23/2023" for "txtFilterPlannedEndDate", it is giving me years of 2020 and 2021, so it is not querying correctly. Can someone help me figure out what the problem is?:

Code:
If Not IsNull(Me.txtFilterPlannedStartDate) Then
strWhere = strWhere & " (tbl_weeklyMAs.planned_start_date) >= #" &
Me.txtFilterPlannedStartDate & "# AND"
End If

If Not IsNull(Me.txtFilterPlannedEndDate) Then
strWhere = strWhere & " (tbl_weeklyMAs.planned_end_date) <= #" &
Me.txtFilterPlannedEndDate & "# AND"
End If
 
Always use
Code:
Debug.Print strWhere
You can then view the where statement.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
dhookom, I'm sorry, I am not familiar with doing this.

I put "Debug.Print strWhere" at the end of my code.

I did bring up the "Immediate" window, but I do not know how to run the code to see what it is doing.

 
Code:
If Not IsNull(Me.txtFilterPlannedStartDate) Then
   strWhere = strWhere & " (tbl_weeklyMAs.planned_start_date) >= #" & Me.txtFilterPlannedStartDate & "# AND"
End If

If Not IsNull(Me.txtFilterPlannedEndDate) Then
   strWhere = strWhere & " (tbl_weeklyMAs.planned_end_date) <= #" & Me.txtFilterPlannedEndDate & "# AND"
End If
Debug.Pring strWhere  [COLOR=#4E9A06]'Check this value in the immediate window[/color]

If you aren't finished with the strWhere, insert the debug line later.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Sorry dhookom, I can't figure how to run this code. This beginning to be nerve racking.
 
It runs when the code runs and puts the value of strWhere in the immediate window.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
dhookom, I have that piece of code after the "End If" and went to the form and did a search, but nothing appears in the "Immediate" window.
 
That suggests the code isn’t being run. Add a breakpoint early in the module and step through line by line using the F8 key.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
I just figured out there is something going on with the data in this table.

I put [Start Date] [End Date] directly in the "Criteria" of this database and another and my other database gives the correct results, but this database does not. If I put in a search range of like, 1/1/2001 and 1/1/2002, I should only get a result between those dates, but it continues to give me other years.

So there is something going on with the data itself, and I am struggling trying to figure out why.

I will also try your suggestion also.
 
You need to learn how to debug your code. There is a FAQ in the Access VBA forum where I give some troubleshooting tips.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
I figured out the issue.

I did a SQL View and noticed I had a field that was "Ordered By" that was giving me the issue. I put that in for the something else and neglected to take it out.

Once I took that out, it worked perfectly!

Thanks for hanging in there with me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top