Greetings.
I have VBA code running from a combobox's AfterUpdate event which sets a querydef to a particular SQL statement based on the selection. Reason: rather than saving a bunch of queries, I just have one called "qry_reporting_ all" and simply change the QueryDef through VBA. Works like a charm.
Now, for aesthetic reasons, I wanted to set the query's columns for each field to 2 inches (2880 twips) wide--per my data, 2 inches looks good on the screen. So, I found some code to loop through all the fields in the query (which vary from SQL statement to SQL statement), and set those column widths. See below.
This works for fields "SiteName" and "EventName" , but when it gets to field name "EventCount ", it throws an Error 3270, "Property not found" on the fld.Properties! ColumnWidth line.
While testing, it looks like this code works for all my SQL statements that aren't Summary Queries...it seems whenever I hit a calculated field, this fails. I've tried aliasing other, non-caculated fields like so:
...and get no errors, so it's not about the name.
I added a watch to myField to be safe and it's returning "EventCount ", which is what the SQL defines. When I open qry_reporting_a ll, the fields are labeled as expected and the data is correct.
I'm guessing it has to do with Count(). Any ideas?
I have VBA code running from a combobox's AfterUpdate event which sets a querydef to a particular SQL statement based on the selection. Reason: rather than saving a bunch of queries, I just have one called "qry_reporting_ all" and simply change the QueryDef through VBA. Works like a charm.
Now, for aesthetic reasons, I wanted to set the query's columns for each field to 2 inches (2880 twips) wide--per my data, 2 inches looks good on the screen. So, I found some code to loop through all the fields in the query (which vary from SQL statement to SQL statement), and set those column widths. See below.
Code:
Dim dbo As DAO.Database Dim qdf As DAO.QueryDef Dim fld As DAO.Field Dim wid As DAO.Property Dim strSQL As String Dim myField As String strSQL = "SELECT tbl_calendar.SiteName, tbl_calendar.EventName, Count(tbl_calendar.FullName) AS EventCount " & _ "From tbl_calendar " & _ "GROUP BY tbl_calendar.SiteName, tbl_calendar.EventName;" Set dbo = CurrentDb() Set qdf = dbo.QueryDefs("qry_reporting_all") qdf.sql = strSQL 'set column widths For Each fld In qdf.Fields myField = fld.Name Set fld = qdf.Fields(myField) Set wid = fld.Properties!ColumnWidth '<--Errors wid = 2880 Next
While testing, it looks like this code works for all my SQL statements that aren't Summary Queries...it seems whenever I hit a calculated field, this fails. I've tried aliasing other, non-caculated fields like so:
Code:
strSQL = "SELECT tbl_calendar.SiteName as Test...
I added a watch to myField to be safe and it's returning "EventCount ", which is what the SQL defines. When I open qry_reporting_a ll, the fields are labeled as expected and the data is correct.
I'm guessing it has to do with Count(). Any ideas?
Comment