Question : MS access VB string not updating with fields from search form

I basically have a search form with 5 fields. When the user presses the search button, a report will open filtered with data based on one or more of the 5 fields.

If no data is entered in the search form, the user will be alerted and not allowed to press on until data is entered.

If data is there, strfilter will be built on data from each of the fields. This is the code I have in the search button:

Private Sub Label15_Click()

Dim strFilter As String
Dim strMsg As String

If IsNull(Me.cmbAgencyType) And IsNull(Me.cmbCountry) And _
IsNull(Me.cmbGenre) And IsNull([txtName]) Then
    strMsg = MsgBox("Please select or enter a search term!", vbOKOnly)
Exit Sub
End If



strFilter = ""

If Not IsNull(Me!txtMediaCategory) Then
    strFilter = strFilter & "[Media Category] ='" & Me!txtMediaCategory & "' AND"
End If

If Not IsNull(Me!cmbGenre.Value) Then
    strFilter = stfilter & "[Advertising PR Genre] = " & Me!cmbGenre.Value & " AND"
End If

If Not IsNull(Me!cmbAgencyType.Value) Then
    strFilter = stfilter & "[Advertising Agency Type] = " & Me!cmbAgencyType.Value & " AND"
End If

If Not IsNull(Me!cmbCountry.Value) Then
    strFilter = stfilter & "[Country] = " & Me!cmbCountry.Value & " AND"
End If

If Not IsNull(Me!txtName) Then
    strFilter = strFilter & "[Name] ='" & Me!txtName & "' AND"
End If

strMsg = MsgBox(strFilter, vbOKOnly)
   
End Sub

--
This is the data type of the fields mentioned:
[Media Category] = text
[Advertising PR Genre] = text
[Advertising Agency Type] =text
[Country] = text
[Name] = text

--
The problem is when I entered data on all fields in the search form and press search, the strfilter only updates with data from the last field. It does not take data from all fields.

What could the problem be?

Answer : MS access VB string not updating with fields from search form

the variable strFilter

in some places you have named it stFilter
in others strFilter

hence you are not concatenating to the same variable!

USE

Option Explicit

in the declarations section of your module
Random Solutions  
 
programming4us programming4us