Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
TRANSFORM Count(tblStuGrade.StuName) AS CountOfStuName
SELECT tblStuGrade.StuSex
FROM tblStuGrade
GROUP BY tblStuGrade.StuSex
PIVOT tblStuGrade.StuGrade;
Public Function basComboXTab()
'TRANSFORM Count(tblStuGrade.StuName) AS CountOfStuName
'SELECT tblStuGrade.StuSex
'FROM tblStuGrade
'GROUP BY tblStuGrade.StuSex
'PIVOT tblStuGrade.StuGrade;
End Function
Public Function basComboXTab(strTblName As String)
'The Argument declaration "strTblName as String" is
'how we send the table name to this porcedure.
'These are just the standard declarations
Dim dbs As Database 'The database
Dim qdf As QueryDef 'The query we reference/manipulate
Dim strSQL As String 'The string variable to build/rebuild the query
'Just standard instantiations of the objects
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qryXTab")
'_____________________________________________________________________________
'Actually do the work here. Note. The process is very straightforward.
'Mechanically, I just assigned each of the fragments of the original SQL
'statement we pasted here (as a comment) to the assignment statement,
'concatenating the pieces. Then, I Replaces each occurance of the table
'name (in my case 'tblStuGrade') with the string passed to the function.
'One of two points. since thes individual lines will be concatenated into
'a single string, you need to be careful to include spaces at the end of
'most of the lines - BUT NOT the last one. You need to NOT include
'extra spaces within the string.
'TRANSFORM Count(tblStuGrade.StuName) AS CountOfStuName
strSQL = "TRANSFORM Count(" & strTblName & ".StuName) AS CountOfStuName "
'SELECT tblStuGrade.StuSex
strSQL = strSQL & "SELECT " & strTblName & ".StuSex "
'FROM tblStuGrade
strSQL = strSQL & "FROM " & strTblName & " "
'GROUP BY tblStuGrade.StuSex
strSQL = strSQL & "GROUP BY " & strTblName & ".StuSex "
'PIVOT tblStuGrade.StuGrade;
strSQL = strSQL & "PIVOT " & strTblName & ".StuGrade;"
'This is just for debug purposes. Put a breakpoint on the second Debug
'statement and examine the handiwork in the debug Window.
Debug.Print strSQL
Debug.Print
'This is what it is all about.
qdf.SQL = strSQL
'Since I do not know the remainder of your process, I will STOP here.
'You call this function with the name of any of the tables with the
'same structure for example:
'basComboXTab("tblStuGrade") would essientially "Do Nothing" as it would
'simply replace the original code.
'You should, however, be able to just execute the query from the database
'window and see the different results sets.
End Function