Hello,
I'm a bit stuck on an export of a query and I feel like what I'm missing is probably obvious, but since I'm here I clearly can't see what it is I've missed. I have a form to export records based on two sets of combo boxes. One drop down chooses the field, the second allows you to input the search criteria. You can then select AND/OR check boxes to further filter the data. Then there is an Export button. I'm able to get the first filter to work based on the 1st combo and text box, but it doesn't include records using the second combo and text box. Help would greatly be appreciated
Thanks
Private Sub cmdExport_Click()
Dim strOrder As String
Dim strFile As String
Dim strWhere As String
Dim strJoinType As String
Dim strFilter As String
Dim strInputFileName As String
Dim i As Integer
Dim j As Integer
Dim dbs As DAO.Database
Dim qdfTemp As DAO.QueryDef
Dim strSQL As String, strQDF As String
Set dbs = CurrentDb
On Error GoTo ErrorHandler
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)", "*.xls")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=False, _
DialogTitle:="Save as ...", _
Flags:=ahtOFN_HIDEREADONLY)
Const conMAXCONTROLS = 2
strSQL = "Select * "
For i = 0 To conMAXCONTROLS - 1
strJoinType = vbNullString
If Me("cbxFld" & i).Enabled Then
If i > 0 Then
If Me("opgClauseType" & i) = 1 Then
strJoinType = " OR "
Else
strJoinType = " AND "
End If
End If
End If
strOrder = "ORDER BY " & Me.cbxOrder & ";"
strWhere = strWhere & strJoinType & Me("cbxFld" & i) & " LIKE '" & Me("txtSearch" & i) & "'"
strSQL = "select * from qry015T010GenotypeSearch where " & strWhere & strOrder
strQDF = "_TempQuery_"
Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
qdfTemp.Close
Set qdfTemp = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, strQDF, strInputFileName, True, "Sheet1"
dbs.QueryDefs.Delete strQDF
dbs.Close
Set dbs = Nothing
Next
ErrorHandler:
If err = cdlCancel Then
DoCmd.CancelEvent
End If
End Sub
I'm a bit stuck on an export of a query and I feel like what I'm missing is probably obvious, but since I'm here I clearly can't see what it is I've missed. I have a form to export records based on two sets of combo boxes. One drop down chooses the field, the second allows you to input the search criteria. You can then select AND/OR check boxes to further filter the data. Then there is an Export button. I'm able to get the first filter to work based on the 1st combo and text box, but it doesn't include records using the second combo and text box. Help would greatly be appreciated
Private Sub cmdExport_Click()
Dim strOrder As String
Dim strFile As String
Dim strWhere As String
Dim strJoinType As String
Dim strFilter As String
Dim strInputFileName As String
Dim i As Integer
Dim j As Integer
Dim dbs As DAO.Database
Dim qdfTemp As DAO.QueryDef
Dim strSQL As String, strQDF As String
Set dbs = CurrentDb
On Error GoTo ErrorHandler
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)", "*.xls")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=False, _
DialogTitle:="Save as ...", _
Flags:=ahtOFN_HIDEREADONLY)
Const conMAXCONTROLS = 2
strSQL = "Select * "
For i = 0 To conMAXCONTROLS - 1
strJoinType = vbNullString
If Me("cbxFld" & i).Enabled Then
If i > 0 Then
If Me("opgClauseType" & i) = 1 Then
strJoinType = " OR "
Else
strJoinType = " AND "
End If
End If
End If
strOrder = "ORDER BY " & Me.cbxOrder & ";"
strWhere = strWhere & strJoinType & Me("cbxFld" & i) & " LIKE '" & Me("txtSearch" & i) & "'"
strSQL = "select * from qry015T010GenotypeSearch where " & strWhere & strOrder
strQDF = "_TempQuery_"
Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
qdfTemp.Close
Set qdfTemp = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, strQDF, strInputFileName, True, "Sheet1"
dbs.QueryDefs.Delete strQDF
dbs.Close
Set dbs = Nothing
Next
ErrorHandler:
If err = cdlCancel Then
DoCmd.CancelEvent
End If
End Sub