S
slprescott via AccessMonster.com
Hello Everyone,
I have a crosstab query in my database that I would like to filter based upon
user selections from combo boxes on an unbound form. I would like for the
users to be able to make a selection from four combo boxes (cboSelectWeek,
cboSelectMonth, cboSelectGroup and cboSelectFocal) and then push a button to
see a crosstab query reflecting their selections; I would like for the query
to export to excel and be saved as a file of the user's choosing. Also, the
user should be able to leave some of the combo boxes null.
I have been reading on this site (which has taught me a lot over the last few
months) for a solution, but haven't been able to get anything to work for me.
I am using some code from Allen Browne's website regarding building a filter
string, along with other bits and pieces.
I think I have just managed to confuse myself!!! I am still new to Access,
SQL and VBA, so a basic answer would be appreciated - but I'll take any help
I can get!!!!
Thanks in advance.
Here is the code I am currently trying (but it gives me a error saying there
are problems with the "TRANSFORM" statement):
Private Sub cmdProjectHoursWeekly_Click()
Dim cnnX As ADODB.Connection
Set cnnX = CurrentProject.Connection
Dim myrecordset As New ADODB.Recordset
myrecordset.ActiveConnection = cnnX
Dim strWhere As String
Dim strcStub As String
Dim strcTail As String
strcStub = "TRANSFORM Sum(WORK_TBL.Hours) AS Hrs"
strcStub = strcStub + " SELECT WORKPACKAGE_TBL.WPID, WORKPACKAGE_TBL.
WP_Title,WORKPACKAGE_TBL.TeamName, WORKPACKAGE_TBL.ANAEMFocal"
strcStub = strcStub + " FROM DB_Calendar (INNER JOIN WORK_TBL (INNER JOIN
USER_TBL ON WORK_TBL.User = USER_TBL.User) (INNER JOIN WORKPACKAGE_TBL ON
WORK_TBL.WPID = WORKPACKAGE_TBL.WPID) ON DB_Calendar.DATE = WORK_TBL.Workdate)
"
strcStub = strcStub + " WHERE ((DB_Calendar.Year) > ((Format(Date, 'yyyy')) -
1)) And ((WORK_TBL.WPID) Is Not Null)) And "
strcTail = " GROUP BY WORKPACKAGE_TBL.WPID, WORKPACKAGE_TBL.WP_Title,
WORKPACKAGE_TBL.TeamName, DB_Calendar.YEAR, WORKPACKAGE_TBL.ANAEMFocal"
strcTail = strcTail + " ORDER BY WORKPACKAGE_TBL.WPID, DB_Calendar.WEEK"
strcTail = strcTail + " PIVOT DB_Calendar.WEEK"
'Build the filter string.
If Not IsNull(Me.cboSelectWeek) Then
strWhere = strWhere & "([WEEK] = " & Me.cboSelectWeek.Value & ") AND
"
End If
If Not IsNull(Me.cboSelectFocal) Then
strWhere = strWhere & "([ANAEMFocal] = """ & Me.cboSelectFocal.Value
& """) AND "
End If
If Not IsNull(Me.cboSelectTeam) Then
strWhere = strWhere & "([TeamName] = """ & Me.cboSelectTeam.Value &
""") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
Dim mySQL As String
mySQL = strcStub & strWhere & strcTail
Dim strFile As String
strFile = "S:\Temp\MyFile.xls"
myrecordset.Open mySQL
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, myrecordset,
strFile, True
End If
End Sub
I have a crosstab query in my database that I would like to filter based upon
user selections from combo boxes on an unbound form. I would like for the
users to be able to make a selection from four combo boxes (cboSelectWeek,
cboSelectMonth, cboSelectGroup and cboSelectFocal) and then push a button to
see a crosstab query reflecting their selections; I would like for the query
to export to excel and be saved as a file of the user's choosing. Also, the
user should be able to leave some of the combo boxes null.
I have been reading on this site (which has taught me a lot over the last few
months) for a solution, but haven't been able to get anything to work for me.
I am using some code from Allen Browne's website regarding building a filter
string, along with other bits and pieces.
I think I have just managed to confuse myself!!! I am still new to Access,
SQL and VBA, so a basic answer would be appreciated - but I'll take any help
I can get!!!!
Thanks in advance.
Here is the code I am currently trying (but it gives me a error saying there
are problems with the "TRANSFORM" statement):
Private Sub cmdProjectHoursWeekly_Click()
Dim cnnX As ADODB.Connection
Set cnnX = CurrentProject.Connection
Dim myrecordset As New ADODB.Recordset
myrecordset.ActiveConnection = cnnX
Dim strWhere As String
Dim strcStub As String
Dim strcTail As String
strcStub = "TRANSFORM Sum(WORK_TBL.Hours) AS Hrs"
strcStub = strcStub + " SELECT WORKPACKAGE_TBL.WPID, WORKPACKAGE_TBL.
WP_Title,WORKPACKAGE_TBL.TeamName, WORKPACKAGE_TBL.ANAEMFocal"
strcStub = strcStub + " FROM DB_Calendar (INNER JOIN WORK_TBL (INNER JOIN
USER_TBL ON WORK_TBL.User = USER_TBL.User) (INNER JOIN WORKPACKAGE_TBL ON
WORK_TBL.WPID = WORKPACKAGE_TBL.WPID) ON DB_Calendar.DATE = WORK_TBL.Workdate)
"
strcStub = strcStub + " WHERE ((DB_Calendar.Year) > ((Format(Date, 'yyyy')) -
1)) And ((WORK_TBL.WPID) Is Not Null)) And "
strcTail = " GROUP BY WORKPACKAGE_TBL.WPID, WORKPACKAGE_TBL.WP_Title,
WORKPACKAGE_TBL.TeamName, DB_Calendar.YEAR, WORKPACKAGE_TBL.ANAEMFocal"
strcTail = strcTail + " ORDER BY WORKPACKAGE_TBL.WPID, DB_Calendar.WEEK"
strcTail = strcTail + " PIVOT DB_Calendar.WEEK"
'Build the filter string.
If Not IsNull(Me.cboSelectWeek) Then
strWhere = strWhere & "([WEEK] = " & Me.cboSelectWeek.Value & ") AND
"
End If
If Not IsNull(Me.cboSelectFocal) Then
strWhere = strWhere & "([ANAEMFocal] = """ & Me.cboSelectFocal.Value
& """) AND "
End If
If Not IsNull(Me.cboSelectTeam) Then
strWhere = strWhere & "([TeamName] = """ & Me.cboSelectTeam.Value &
""") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
Dim mySQL As String
mySQL = strcStub & strWhere & strcTail
Dim strFile As String
strFile = "S:\Temp\MyFile.xls"
myrecordset.Open mySQL
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, myrecordset,
strFile, True
End If
End Sub