Access help: VB

minendo

Elite Member
Aug 31, 2001
35,560
22
81
What I am trying to do is filter a report using criteria set in a form based on the data input in the database. I've got my form, filters, etc set, but for some reason the attached code does not work.

Private Sub Set_Filter_Click()

Code:
Dim strSQL As String, intCounter As Integer


  For intCounter = 1 To 4
      If Me("Filter" & intCounter) <> "" Then

         strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] " _
             & " = " & Chr(34) & Me("Filter" & intCounter) & Chr(34) & "" _
             And ""
      End If
  Next

  If strSQL <> "" Then
     
     strSQL = Left(strSQL, (Len(strSQL) - 5))

     Reports![date_report].Filter = strSQL
     Reports![date_report].FilterOn = True
   End If

End Sub

Any ideas as to why I keept getting an error on the strSQL = command?
 
Last edited:

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
What I am trying to do is filter a report using criteria set in a form based on the data input in the database. I've got my form, filters, etc set, but for some reason the attached code does not work.

Private Sub Set_Filter_Click()

Code:
Dim strSQL As String, intCounter As Integer


  For intCounter = 1 To 4
      If Me("Filter" & intCounter) <> "" Then

         strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] " _
             & " = " & Chr(34) & Me("Filter" & intCounter) & Chr(34) & "" And ""
      End If
  Next

  If strSQL <> "" Then
     
     strSQL = Left(strSQL, (Len(strSQL) - 5))

     Reports![date_report].Filter = strSQL
     Reports![date_report].FilterOn = True
   End If

End Sub

Any ideas as to why I keept getting an error on the strSQL = command?

See if that works?
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
See if that works?

Nope. Still errors on:

Code:
         strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] " _
             & " = " & Chr(34) & Me("Filter" & intCounter) & Chr(34) & "" And ""
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
Why are you double quoting this?
Code:
"" And ""

Remove double quotes. And if you MSUT double quote there (for some reason) then use what you have been using before and stay consistent: char(34)
 

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
Why are you double quoting this?
Code:
"" And ""

Remove double quotes. And if you MSUT double quote there (for some reason) then use what you have been using before and stay consistent: char(34)

lol That's the problem. Just tested it out myself and it works when taking out those double quotes.
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
Why are you double quoting this?
Code:
"" And ""

Remove double quotes. And if you MSUT double quote there (for some reason) then use what you have been using before and stay consistent: char(34)

Removing that helped, but for some reason it does not accept the values from my combo boxes. Instead I get a popup asking to input parameter. Any ideas?
 

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
Removing that helped, but for some reason it does not accept the values from my combo boxes. Instead I get a popup asking to input parameter. Any ideas?

When you open the properties for each field(design view, click field, F4 key) and click the other tab the name property needs to match "filter1", "filter2" and so on.
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
When you open the properties for each field(design view, click field, F4 key) and click the other tab the name property needs to match "filter1", "filter2" and so on.

All are set that way. It runs through the filter names properly. Do they need to have a control source set?
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
For example Filter 1 properties are:

Name: Filter1
Row Source Type: Table/Query
Row Source: SELECT Main.[Item #], Main.Date FROM Main;
 

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
Are you familiar with stepping through code in vba? You may want to see what strSQL is ending up looking like before setting it to the report's filter property.
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
Are you familiar with stepping through code in vba? You may want to see what strSQL is ending up looking like before setting it to the report's filter property.

Not at all. I haven't used VB since 10 years ago. I'm just trying to create a simple database with a filter option for the one of the reports.
 

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
Change Me("Filter" & intCounter).Tag to Me("Filter" & intCounter).Name and give it another try
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
How do I set the FilterOn expression in a report?

Edit: Nevermind figured filteron out. Still can't figure out why report does not filter.
 
Last edited:

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
PHP:
Private Sub Set_Filter_Click()
Dim strSQL As String, intCounter As Integer, first As Boolean
first = False

    If Not IsNull(Me.Filter1) Then
        first = True
        strSQL = "[ReportField1] = '" & Me.Filter1 & "'"
    End If
      
    If Not IsNull(Me.Filter2) Then
        If first Then
            strSQL = strSQL & " AND "
        Else
            first = True
        End If
        strSQL = strSQL & "[ReportField2] = '" & Me.Filter2 & "'"
    End If
    
    If Not IsNull(Me.Filter3) Then
        If first Then
            strSQL = strSQL & " AND "
        Else
            first = True
        End If
        strSQL = strSQL & "[ReportField3] = '" & Me.Filter3 & "'"
    End If
    
    If Not IsNull(Me.Filter4) Then
        If first Then
            strSQL = strSQL & " AND "
        Else
            first = True
        End If
        strSQL = strSQL & "[ReportField4] = '" & Me.Filter4 & "'"
    End If
    
    If strSQL <> "" Then
        Reports![date_report].Filter = strSQL
        Reports![date_report].FilterOn = True
    End If
End Sub

Try that out. You need to change each ReportField to be whatever field you want to filter by in the report. (ie change ReportField1 to [Item #] in the code above and the same thing for the other 3).
 
Last edited:

minendo

Elite Member
Aug 31, 2001
35,560
22
81
PHP:
Dim strSQL As String, intCounter As Integer, first As Boolean
first = False

    If Not IsNull(Me.Filter1) Then
        first = True
        strSQL = "[ReportField1] = '" & Me.Filter1 & "'"
    End If
      
    If Not IsNull(Me.Filter2) Then
        If first Then
            strSQL = strSQL & " AND "
        Else
            first = True
        End If
        strSQL = strSQL & "[ReportField2] = '" & Me.Filter2 & "'"
    End If
    
    If Not IsNull(Me.Filter3) Then
        If first Then
            strSQL = strSQL & " AND "
        Else
            first = True
        End If
        strSQL = strSQL & "[ReportField3] = '" & Me.Filter3 & "'"
    End If
    
    If Not IsNull(Me.Filter4) Then
        If first Then
            strSQL = strSQL & " AND "
        Else
            first = True
        End If
        strSQL = strSQL & "[ReportField4] = '" & Me.Filter4 & "'"
    End If
    
    If strSQL <> "" Then
        Reports![date_report].Filter = strSQL
        Reports![date_report].FilterOn = True
    End If

Try that out. You need to change each ReportField to be whatever field you want to filter by in the report. (ie change ReportField1 to [Item #] in the code above and the same thing for the other 3).

Gave me the following error: Data type mismatch in criteria expression.
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
Gave me the following error: Data type mismatch in criteria expression.

Will this allow me to filter by just one filter or must all filters have data selected? I am needed to filter by any combo (sometimes just 1, other times multiple).
 

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
Will this allow me to filter by just one filter or must all filters have data selected? I am needed to filter by any combo (sometimes just 1, other times multiple).

It will allow any combination of the combo boxes filled out. Are any of the combo boxes numeric values or dates?
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
It will allow any combination of the combo boxes filled out. Are any of the combo boxes numeric values or dates?

The first combo box is a date. Two are text, and one is linked to a Yes/No field.

Would it be easier if I were to send you the file to review? Right now it is just filled with bogus data until I get it working properly.
 

KLin

Lifer
Feb 29, 2000
30,950
1,076
126
The first combo box is a date. Two are text, and one is linked to a Yes/No field.

Would it be easier if I were to send you the file to review? Right now it is just filled with bogus data until I get it working properly.

That's fine. I'm at home sick and kinda bored. YHPM