ok, I've moved the three "if" before the "strSQL".. and the Runtime error changes.. now is 3075: parethesis in excess: ) in the expression of the query '(Cognome Like "**") AND (N_corso = 4) AND (N_modulo = )'.
I've entered only N_corso..
Code:
Private Sub ExportPDF_Click()
Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim strSQL As String
Dim strRptName As String
Dim sCriteria As String
Dim strWhere As String
Dim lngLen As Long
Set MyDB = CurrentDb
strRptName = "Attestati_2014"
If Not IsNull(Me.qcognome1) Then
strWhere = strWhere & "(Cognome Like ""*" & Me.qcognome1 & "*"") AND "
End If
If Not IsNull(Me.qncorso1) Then
strWhere = strWhere & "(N_corso = " & Me.qncorso1 & ") AND "
End If
If Not IsNull(Me.qnmodulo1) Then
strWhere = strWhere & "(N_modulo = " & Me.qnmodulo1 & ") AND "
End If
'See if the string has more than 5 characters (a trailing " AND ") to remove.
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
'Yep: there is something there, so remove the " AND " at the end.
strWhere = "WHERE " & Left$(strWhere, lngLen)
End If
strSQL = "Select Selezione_corso_2014.[Cognome], Selezione_corso_2014.[Data_modulo],Selezione_corso_2014.[Nome] From Selezione_corso_2014 " & strWhere & ";"
strSQL = strSQL & " WHERE Cognome ='" & Me.qcognome1 & "' AND N_corso =" & Me.qncorso1 & " AND N_modulo =" & Me.qnmodulo1 & ""
Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
With MyRS
Do While Not MyRS.EOF
sCriteria = "[Cognome]='" & ![Cognome] & "' and [Nome]='" & ![Nome] & "' and [Data_modulo] = #" & ![Data_modulo] & "#"
DoCmd.OpenReport strRptName, acViewPreview, , sCriteria
DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, "C:\Attestati\" & Format(![Cognome], ">") & Format(![Nome], "<") & "_" & Format(![Data_modulo], "YYYYMMDD") & ".pdf", False
DoCmd.Close acReport, strRptName
.MoveNext
Loop
End With
MyRS.Close
Set MyRS = Nothing
MsgBox "Done"
End Sub