All, using access 2003. I have a form with 3 unbound boxes. 1 for users to choose a division(named module) and the other two are date fields. I have a query to open and export to excel. When I choose the division and dates; click the excel export; I get type mismatched. The field for the dates in the query is formated mmddyyyy. Here's my code:
Can anyone tell me what I am doing wrong.
Code:
Private Sub cmdExport_Click()
On Error GoTo Err_cmdExport_Click
Dim strSQL As String, intCounter As Integer
Dim ctl As Control, strname As String, strnewquery As String
Set db = CurrentDb
'Build SQL String
For Each ctl In Me.Form
If ctl.Tag = "input" Then
strname = "me." & ctl.Name
If ctl.Value > "" Then
strSQL = strSQL & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "
End If
End If
Next ctl
' Set the value of the parameter.
Select Case Me.Module
Case SP
strnewquery = "qrySPReports"
Case LTL
strnewquery = "qryLTLReports"
Case DTF
strnewquery = "qryDTFReports"
End Select
If Me.cboFROM & vbNullString <> "" And Me.cboTO & vbNullString <> "" Then
strSQL = strSQL & " ([MONTHPAID] BETWEEN " & Me.cboFROM & " And " & Me.cboTO & ") And "
End If
stDocName = "Select strnewquery * FROM strnewquery"
If strSQL <> "" Then
strSQL = Left(strSQL, (Len(strSQL) - 5))
strnewquery = strnewquery & " WHERE " & strSQL & ";"
End If
stDocName = strnewquery
DoCmd.OpenQuery strnewquery, acViewNormal, strSQL
DoCmd.Close acForm, "frmREPORTBUILDER"
stDocName = "strnewquery"
DoCmd.OpenQuery stDocName, acPreview
DoCmd.OutputTo acOutputQuery, strnewquery, acFormatXLS
DoCmd.Close strnewquery
Exit_cmdExport_Click:
Exit Sub
Err_cmdExport_Click:
Select Case Err.Number
Case 2501 'OpenQuery action was cancelled
Resume Exit_cmdExport_Click
Case Else
MsgBox Err.Description
Resume Exit_cmdExport_Click
Resume
End Select
End Sub