UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Vb Error On Filter    
 
   
sp215
post Jul 1 2011, 06:06 AM
Post #1

UtterAccess Enthusiast
Posts: 79



Hi,

I have created a report and a form filter, when I select an option from the combo box on the form filter I get the run=time error '13' Type mismatch

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

This is the complete code

Option Compare Database

Private Sub Clear_Click()
Dim intCounter As Integer
For intCounter = 1 To 4
Me("Filter" & intCounter) = ""
Next

End Sub

Private Sub Close_Click()
DoCmd.Close acForm, Me.Form.Name

End Sub

Private Sub Form_Close()
DoCmd.Close acReport, "Rpt_Trade" 'Close the Trades report.
DoCmd.Restore 'Restore the window size

End Sub

Private Sub Form_Open(Cancel As Integer)
DoCmd.OpenReport "Rpt_Trades", A_PREVIEW 'Open Trades Report.
DoCmd.Maximize 'Maximize the report window.

End Sub

Private Sub Set_Filter_Click()
Dim strSQL As String, intCounter As Integer

' Build SQL String.
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
' Strip Last " And ".
strSQL = Left(strSQL, (Len(strSQL) - 5))

' Set the Filter property.
Reports![Rpt_Trades].Filter = strSQL
Reports![Rpt_Trades].FilterOn = True
End If

End Sub


Would appreciate any help where I am going wrong
Go to the top of the page
 
+
yvesdekort
post Jul 1 2011, 06:15 AM
Post #2

UtterAccess VIP
Posts: 1,606
From: Belgium



Have you looked at a

debug.print strSQL

in the immediate window.

Or insert msgbox (strSQL) before the opening of the report.

Is any of the filters numeric?
Go to the top of the page
 
+
sp215
post Jul 1 2011, 06:16 AM
Post #3

UtterAccess Enthusiast
Posts: 79



Yes the Log No is an autonum
Go to the top of the page
 
+
yvesdekort
post Jul 1 2011, 06:17 AM
Post #4

UtterAccess VIP
Posts: 1,606
From: Belgium



If its numeric you don't need the quotes in the expression.
Go to the top of the page
 
+
sp215
post Jul 1 2011, 06:20 AM
Post #5

UtterAccess Enthusiast
Posts: 79



Sorry very new to this which part of the expression would I take the quotes out
Go to the top of the page
 
+
sp215
post Jul 1 2011, 06:20 AM
Post #6

UtterAccess Enthusiast
Posts: 79



Sorry very new to this which part of the expression would I take the quotes out
Go to the top of the page
 
+
yvesdekort
post Jul 1 2011, 06:37 AM
Post #7

UtterAccess VIP
Posts: 1,606
From: Belgium



You have:

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

If I put this on 1 line and clean up a bit, you'd get:

strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] = '" & Me("Filter" & intCounter) & "' And "

Notice I replaced the chr(34) with a single quote (which is positioned within a part enclosed with double quotes; confusing isn't it).
I also deleted 1 double quote at the end.
The above syntax will work if you're comparing strings.

If you need to compare numeric values, you need to get rid off the single quotes, so you'd get:
strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] = " & Me("Filter" & intCounter) & " And "

Instead of using a for...next loop, you might want to consider (in this case) to get rid of the loop and write the code out explicitly (Filter1, filter2, ...)

put in a msgbox (strSQL) after completing the construction of it, so you can check if it's ok.

This post has been edited by yvesdekort: Jul 1 2011, 06:38 AM
Go to the top of the page
 
+
sp215
post Jul 1 2011, 08:17 AM
Post #8

UtterAccess Enthusiast
Posts: 79



Great that worked perfectly, how would I have to change the code if the user wanted to filter on a date?
Go to the top of the page
 
+
yvesdekort
post Jul 1 2011, 08:30 AM
Post #9

UtterAccess VIP
Posts: 1,606
From: Belgium



strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] = #" & Me("Filter" & intCounter) & "# And "
Go to the top of the page
 
+
yvesdekort
post Jul 5 2011, 04:03 PM
Post #10

UtterAccess VIP
Posts: 1,606
From: Belgium



QUOTE
Hi, sorry I didn't realise you had helped me with the previous thread, what I require assistance with now is that I would like the user to open a form and thee is a range of combo boxes for them to select different options, once they have done this then they would click on a "search" button and this would then open a report only showing the records that matched their search criteria based on the combo choices. I currently have the report opening at the same time as the form and it refreshes when they click the search button but does not look nice on screen.


Ok, in order to do this:
- you open the filter-form the same way you are already opening it (through a button on another form, double-clicking the form in the formslist, ...)

- you delete the following line from the Form_Open event:
DoCmd.OpenReport "Rpt_Trades", A_PREVIEW 'Open Trades Report
this will prevent that the report opens at the same time as the form

- you put a commandbutton on the form, and in the on click event for this button you put:

...
code that creates the strSQL-string based on the filtering combos
...

DoCmd.OpenReport "Rpt_Trades", acViewPreview, , strSQL

Go to the top of the page
 
+
sp215
post Jul 7 2011, 10:32 AM
Post #11

UtterAccess Enthusiast
Posts: 79



Ok I added it to my search button which runs the code below but then I get a Run-time error '2451' The report name 'Rpt_Trades' you entered is misspelled or refers to a report that isn't open or doesn't exist

Private Sub Set_Filter_Click()
Dim strSQL As String, intCounter As Integer

' Build SQL String.
For intCounter = 1 To 5
If Me("Filter" & intCounter) <> "" Then

strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] = '" & Me("Filter" & intCounter) & "' And "

End If
Next

If strSQL <> "" Then
' Strip Last " And ".
strSQL = Left(strSQL, (Len(strSQL) - 5))

' Set the Filter property.
Reports![Rpt_Trades].Filter = strSQL
Reports![Rpt_Trades].FilterOn = True
End If

DoCmd.OpenReport "Rpt_Trades", acViewPreview, , strSQL


End Sub

This post has been edited by sp215: Jul 7 2011, 10:34 AM
Go to the top of the page
 
+
yvesdekort
post Jul 7 2011, 10:39 AM
Post #12

UtterAccess VIP
Posts: 1,606
From: Belgium



Try changing it to:
CODE
Private Sub Set_Filter_Click()
   Dim strSQL As String, intCounter As Integer

   ' Build SQL String.
   For intCounter = 1 To 5
      If Me("Filter" & intCounter) <> "" Then
         strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] = '" & Me("Filter" & intCounter) & "' And "
      End If
   Next

   If strSQL <> "" Then
      ' Strip Last " And "
      strSQL = Left(strSQL, (Len(strSQL) - 5))
      
      DoCmd.OpenReport "Rpt_Trades", acViewPreview, , strSQL
   End If
End Sub
Go to the top of the page
 
+
sp215
post Jul 7 2011, 10:41 AM
Post #13

UtterAccess Enthusiast
Posts: 79



Thats perfect thank you very much
Go to the top of the page
 
+
sp215
post Jul 7 2011, 10:49 AM
Post #14

UtterAccess Enthusiast
Posts: 79



Now one more question is it possible to put a command button onto the Report Preview so that the user has the option to print or return to the main menu?
Go to the top of the page
 
+
yvesdekort
post Jul 7 2011, 11:05 AM
Post #15

UtterAccess VIP
Posts: 1,606
From: Belgium



This is possible in Access 2010, don't know if it's possible in previous versions.

In the "print-button":

DoCmd.PrintOut

In the "go back button":

DoCmd.Close acReport, Me.Name
Go to the top of the page
 
+
sp215
post Jul 8 2011, 04:18 AM
Post #16

UtterAccess Enthusiast
Posts: 79



Ok, I am running 2002 so don't have the option to add the buttons, so how could I still use the filter form and then link that to a subform that will show the records and allow me to add the additional buttons, what would I need to change in the sql statement.

Many Thanks
Go to the top of the page
 
+
yvesdekort
post Jul 8 2011, 06:46 AM
Post #17

UtterAccess VIP
Posts: 1,606
From: Belgium



- create a continuous form (let's name it frmTradePreview) which is based on the same data as your report (include same fields)

- on this form add a hidden textbox (name it txtSQL)

- in the filterform change the code for the command button into (this will open frmTradePreview with the filter like the report):
CODE
Private Sub Set_Filter_Click()
   Dim strSQL As String, intCounter As Integer

   ' Build SQL String.
   For intCounter = 1 To 5
      If Me("Filter" & intCounter) <> "" Then
         strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] = '" & Me("Filter" & intCounter) & "' And "
      End If
   Next

   If strSQL <> "" Then
      ' Strip Last " And "
      strSQL = Left(strSQL, (Len(strSQL) - 5))
      
      DoCmd.OpenForm "frmTradePreview", acNormal, , strSQL
      
      'puts the filter in the form for future use
      frmTradePreview.txtSQL = strSQL
   End If
End Sub


- in the frmTradePreview add 2 commandbuttons:
one to print the report (name it cmdPrint):
CODE
Private Sub cmdPrint_Click()
   Dim strSQL As String
   'get the filter strSQL from the form
   strSQL = Me.txtSQL

   'print the report
   DoCmd.OpenReport "Rpt_Trades", , , strSQL
End Sub


the other button you can use to close frmTradePreview and go back to main menu, filter form ot whatever you want to.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th June 2013 - 04:58 AM