Microsoft Access
--
Questions
--
Followers
Top Experts
At least initially, the queries will all be based on the same table. So it is just a matter of defining what fields to include and what criteria for each field.
On the form, I am using radio buttons, check boxes and text boxes.
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
How to use the query by form (QBF) technique in Microsoft Access
http://support.microsoft.com/default.aspx?scid=kb;en-us;304428
Have another listbox which contains the fields the user wants then a create button.
This will either create a new query each time or if you dont want to do that, just keep modifying an existing query
STEP 1
Say the two listboxes you have are called this lstSelectableFields and lstSelectedFields. These two listboxes will be of type Value List as we are specifying the values
This is how you would populate lstSelectableFields
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim i As Integer
lstSelectableFields.RowSou
Set db = CurrentDb
Set tbl = db.TableDefs("mytable")
For i = 0 To tbl.Fields.Count - 1
If lstSelectableFields.RowSou
lstSelectableFields.RowSou
Next i






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I will put on hold the rest of my code sample, unless you feel it will be useful to you and you want me to
Name each radio button the same as the field name. Start to build your SQL by cycling through the controls on the form and check to see if it is a radio button and see if it is selected for use. (SELECT [USERID], [NAME],...)
Name the text boxes the same as each field name. continue building your SQL by cycling though the controls onthe form and check to see if it is text box and then create the where clause based on the text fields. (WHERE [USERID] = 'D3984D' AND [NAME] = 'JOE')
This would take some code to do but I would go this route.
That seems closest to what I had in mind, but I still don't quite understand how the SQL will be generated each time.
My basic set up is I'm giving the user the option of including or excluding 5 fields. The table has many more fields, but they aren't relevant to what is trying to be generated from the query. The selection of whether or not to include will ne made through the checking of a radio button. If the radio button is checked, then I want the query to then look for criteria for that field. For the most part I am just using check boxes for the criteria. So for example, I have a region radio box and below it four check boxes one for each region (WHEM, EMEA, etc.). If the user checks the radio box for region, I would want the SQL to select that field and then use a WHERE clause for what ever regions are checked off.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
But pgerman likes your approach so I think you should go with it.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Include time period & corresponding reveunes? (Radio button)
If yes:
Choose time period: ( The table has about 3 years of months as field names with the corresponding revenues for those months going down throughout the records)
preset or custom? (radio buttons)
If preset, choose from drop down (for example Q1 2008)
If custom, set range by selecting from drop downs. From [Month] & [Year] through [Month]
& [Year].
Set criteria for time period: (Select check boxes)
Large (>5mm for the time period)
Medium(between 1 and 5 mm for the time period)
Small(<1mm for the time period)
Other
If other, enter a minimum revenue value and a maximum into textboxes
Include segment field? (radio button)
If Yes:
choose what segments to include (set criteria): (select checkboxes)
Asset managers
Banks
Central Banks
etc. (13 total segment choices)
Include hierarchy level, or whether or not to show customer names and at what level, (radio button)
If Yes:
Select what field to choose (in this case they are all separate fields) (check boxes)
Ultimate Parent Name
Customer Name
Group Account number
Include a specific sub product specification? (Radio button)
If Yes:
Set Subproduct criteria by selecting check boxes:
Full Discretionary
Exclusives only
etc. (5 total options for sub product)
Include Region field? (Radio button)
If Yes:
Set region criteria by selecting check boxes
WHEM
EMEA
ASIA
LATAM

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Radio button name syntax 'OB_<fieldname>'
Each check box name syntax for this perticular radio button '<fieldname>_CB_<criteria value>'
the code used these to match up the correct check box with the correct radio button and build the correct sql.
Digest this while I am at lunch.
Can you email this file to me, I am un able to directly open it from the site from my work computer.
peter.c.german@jpmorgan.co
many thanks
The only two that need more work is: Include time period & corresponding reveunes (radio button)
and Include hierarchy level, or whether or not to show customer names and at what level, (radio button)
The others are covered in the example. The example assumes that each criteria is going to be text and based on your one long reply they are. when you being mixing and matching datatypes the code has to account count it.
Questions about these other two...

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Include hierarchy level, or whether or not to show customer names and at what level, (radio button)
If Yes:
Select what field to choose (in this case they are all separate fields) (check boxes)
Ultimate Parent Name
Customer Name
Group Account number
Are these just a set of fields that can be chosen?
user selects HierarchyLevel then can select any or all of the three fields you see here?
Is hierarchy level a field or just a grouping title?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Choose time period: ( The table has about 3 years of months as field names with the corresponding revenues for those months going down throughout the records)
preset or custom? (radio buttons)
If preset, choose from drop down (for example Q1 2008)
If custom, set range by selecting from drop downs. From [Month] & [Year] through [Month]
& [Year].
Set criteria for time period: (Select check boxes)
Large (>5mm for the time period)
Medium(between 1 and 5 mm for the time period)
Small(<1mm for the time period)
Other - If other, enter a minimum revenue value and a maximum into textboxes
This table has Q1 2007, Q2 2007 ... and so on as field names?
Do you want each of these field names to be displayed in the sql?
I'm not sure what you mean by 'displayed in SQL'
another though ... the criteria, do you want it applied to each field (jan 07, Feb 07, ...)?
What kind of data is in these fields? Text or numbers?

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
BTW, what type of data is the Group account number, because the code only accounts for text fields?
I am a little confused with how to proceed.
Do I need to set each check box equal to a default value, in the properties of the check boxes?
Also I do not understand the significance of the numbers in the code, like 4, -1, etc. Also do not know what 'Len' is.
Len is a built in function that defines the character length of a variable. I am using it in conjunction with left, right, and mid functions which parse a piece of text into different pieces. This is where the field names and criteria are coming from. If you have more questions about these put your curser on LEN in the Code screen and press F1, help will give you a lot of info about them.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
What kind of data is in these fields? Text or numbers?

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
The Classifications (Large, Medium, Small and Other) all have definitions. Large = >5000000, Medium = >1000000 AND <5000000, Small = <1000000. Other relies on the text boxes. These would be the criteria for the revenue/ month fields.
I am focusing on the 'Preview Custom Report' Button for now. This is what will pull up the query.
Group Account # is 'CAS Acct #' in the masterdata table.
Region is 'UltP Geo Region Code'
Also once we get this figured out, is there a way to assign a unique report number to each query and then create a repository of all queries ran, which can be refered back to by the user, by maybe entering the report number, which they will be provided upon running the query?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Segment on Form = 'UltP Segment' in masterdata table
Customer Name on Form = 'Cust Name' in masterdata table
Ultimate Parent Name on Form = 'UltP Name' in masterdata table

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Under the custom area for time period, there are two tables that are linked to the four drop downs. One for each possible year 2007-2009 and one for each possible month.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
When looking for the revenue data do you want to see all data with
Jan07 >=5000000 and Feb07 >= 5000000 and Mar07 >= 5000000
that equates to seeing all customers with revenue above 5mil for those three months (first qtr)
or
Jan07 >=5000000 or Feb07 >= 5000000 or Mar07 >= 5000000
that equates to seeing all customers with revenue above 5mil for either three months (first qtr)

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Accordingly, Medium would be defined as any client generating between 1mm and 5mm for the year, or between 83333.3333 and 416666.6667 for the month or between 250000 and 1250000 for the quarter.
Small would be less than 83333.3333 for the month, less than 250000 for the quarter and less than 1mm for the year.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
You can try to use EasyQuery library: http://devtools.korzh.com/eq/
They have several editions for different platforms including ActiveX (which should be suitable for Access Forms I think) and .NET controls (in case you are developing Windows Forms application or ASP.NET web-site).
To see this solution at work take a look at the demo page: http://www.easyquerydemo.com

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Sorry for not getting back to you yesterday, I had to jump to another project. Have you made any additional updates to the db?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
You will see that I renamed most of the controls on the form so I could utilize there name. Others still have to changed to match the query criteria for each box and also naming convention
Option Compare Database
Option Explicit
Private Sub Form_Open(Cancel As Integer)
'load the combo boxes with possible months
Call LoadComboBoxes
'set the combo boxes, check boxes, and text fields to locked
'until the specific group button is pressed
Me.PresetTimePeriod_CmBox_2007.Locked = True
Me.PresetTimePeriod_CmBox_2008.Locked = True
Me.PresetTimePeriod_CmBox_2009.Locked = True
Me.FromMonth.Locked = True
Me.FromYear.Locked = True
Me.ToMonth.Locked = True
Me.ToYear.Locked = True
Me.Large_Criteria_Time_Period.Locked = True
Me.Medium_Criteria_Time_Period.Locked = True
Me.Small_Criteria_Time_Period.Locked = True
Me.Other_Criteria_Time_Period.Locked = True
Me.TxBox_Other_Min.Locked = True
Me.TxBox_Other_Max.Locked = True
End Sub
Function LoadComboBoxes()
'used to load the fromMonth FromYear ToMonth ToYear
'combo boxes based on the table data
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As Field
Dim dteTemp As Date
Dim strQtr As String
Dim blnFound As Boolean
Dim strTblName As String
Dim strCreateSQL As String
Dim strWriteSQL As String
'create a temporary table which will hold all available data for the combo boxes
'this data is created based on the masterdata field names
strTblName = "tmptblComboBoxData"
Call EraseTable(strTblName)
'this statement create the needed sql to create the table
strCreateSQL = "CREATE TABLE " & strTblName & " (" & _
"FieldName TEXT(25), " & _
"FieldDate Date, " & _
"FieldMonth Integer, " & _
"FieldYear Integer, " & _
"FieldQtr TEXT(25))"
CurrentDb.Execute (strCreateSQL)
strTblName = "MasterData"
'cycle through all the fields in the strTblName (MasterData) table to create the needed
'data for the combo boxes
Set db = CurrentDb
Set tbl = db.TableDefs(strTblName)
For Each fld In tbl.Fields
'if the field name is a valid date
If IsDate(Left(fld.Name, 2) & "-01-" & _
Right(fld.Name, 2)) = True Then
'assigne the combined value to a variable for use
'data format is assumed <Month><YEAR> with leading zero (0107,0207,0307...)
dteTemp = DateValue(Left(fld.Name, 2) & "-01-" & _
Right(fld.Name, 2))
'establish which quarter the date ends up in.
'assumes fiscal year starts in jan
Select Case Month(dteTemp)
Case 1, 2, 3
strQtr = "1Q " & Year(dteTemp)
Case 4, 5, 6
strQtr = "2Q " & Year(dteTemp)
Case 7, 8, 9
strQtr = "3Q " & Year(dteTemp)
Case 10, 11, 12
strQtr = "4Q " & Year(dteTemp)
End Select
'create the needed sql to append the data to the temporary table
strWriteSQL = "INSERT INTO tmptblComboBoxData ( FieldName, FieldDate, FieldMonth, FieldYear, FieldQtr ) " & _
"SELECT '" & fld.Name & _
"', #" & dteTemp & _
"#, " & Left(fld.Name, 2) & _
", " & Right(fld.Name, 2) & _
", '" & strQtr & "';"
CurrentDb.Execute (strWriteSQL)
End If
Next fld
End Function
Private Sub Preview_Cstm_Rpt_Btn_Click()
Dim db As DAO.Database
Dim qry As DAO.QueryDef
Dim frm As Form
Dim ctl As Control
Dim ctlTmp As Control
Dim prp As Property
Dim varItm As Variant
Dim strTblName As String
Dim strSelectSQL As String
Dim strWhereSQL As String
Dim strSQL As String
Dim dteStart As Date
Dim dteEnd As Date
'set up a sub routine to ensure the user has filled in the needed
'fields to execute the query builder
If blnCheckForm = True Then
'these variables will be used to help establish the sql statement
strTblName = "MasterData"
strSelectSQL = "SELECT "
strWhereSQL = "WHERE "
strSQL = "FROM " & strTblName & " "
'set the value of the form with the correct form name
Set frm = Forms![CRA_rpt_bldr_custom1]
'cycle through each control on the form
For Each ctl In frm.Controls
'find the specific control which is an option button. Each of the groups
'have an option button that must be pressed to get to the needed criteria
If ctl.Properties("ControlType") = acOptionButton Then
'some of the option buttons are not fields to add to the sql but are only
'group headings. if the control name does not start with OB then it
'is not a field to be added only a group heading
If ctl.Value = True And Left(ctl.Name, 2) = "OB" Then
'add the found control to the select statement
Call BuildSelect(strSelectSQL, ctl)
'add the found controls to the where statement
Call BuildWhere(strWhereSQL, frm, ctl)
Else
'if the custom time period is selected
If ctl.Name = "CustomTimePeriod" And ctl.Value = True Then
dteStart = DateValue(Me.FromMonth & "-1-" & Me.FromYear)
dteEnd = DateValue(Me.ToMonth & "-1-" & Me.ToYear)
'then add the needed field names to the select and where statement
Call GetFieldNames(strTblName, strSelectSQL, strWhereSQL, dteStart, _
dteEnd, frm, GetCriteriaValue(frm))
End If
'if the preset time period is selected
If ctl.Name = "PresetTimePeriod" And ctl.Value = True Then
'cycle through the controls on the form again to find the right ones
For Each ctlTmp In frm.Controls
'if the control matches then pull then
If Left(ctlTmp.Name, Len(ctl.Name)) = ctl.Name And ctlTmp.Name <> ctl.Name Then
If Nz(ctlTmp.Column(0)) <> "" Then
'populate the needed variables to get the field names and criteria
dteStart = DateValue(ctlTmp.Column(1))
dteEnd = DateValue(ctlTmp.Column(2))
'then add the needed field names to the select and where statement
Call GetFieldNames(strTblName, strSelectSQL, strWhereSQL, dteStart, _
dteEnd, frm, GetCriteriaValue(frm))
End If
End If
Next ctlTmp
End If
End If
End If
Next ctl
'make sure something was selected and clean up the select statement
If Len(strSelectSQL) > 7 Then
strSelectSQL = Left(strSelectSQL, Len(strSelectSQL) - 2) & " "
Else
strSelectSQL = strSelectSQL & "* "
End If
strSQL = strSelectSQL & strSQL
'make sure something was selected and clean up the where statement
If Len(strWhereSQL) > 8 Then
strSQL = strSQL & Left(strWhereSQL, Len(strWhereSQL) - 5) & ";"
Else
strSQL = Left(strSQL, Len(strSQL) - 1) & ";"
End If
End If
Debug.Print strSQL
Set db = CurrentDb
EraseQuery ("tmpqrySQLBuilder")
db.CreateQueryDef "tmpqrySQLBuilder", strSQL
DoCmd.OpenQuery "tmpqrySQLBuilder"
End Sub
Function GetCriteriaValue(frm As Form) As String
'this function is used to find the correct criteria based on the user
'input from the by classifications section of the form
'the definitions of the size is stored in the tblClassificationsDefined table
Dim ctlTemp As Control
Dim blnFound As Boolean
Dim rsRead As DAO.Recordset
Dim strReadSQL As String
'loop through all the controls to find the correct set of check boxes
For Each ctlTemp In frm.Controls
'if the control matches the criteria_time_period
If Right(ctlTemp.Properties("Name"), 20) = "Criteria_Time_Period" Then
'if the control is selected
If ctlTemp.Value = True Then
'this sql statement gets the data for the specific size client the user choose
strReadSQL = "SELECT tblClassificationsDefined.* " & _
"FROM tblClassificationsDefined " & _
"WHERE (((tblClassificationsDefined.CName)='" & _
ctlTemp.Properties("Name") & "'));"
'this statement establishes a record set in memory which can be gone through
'using this code below. this recordset can be manipulated as well (though this is not)
Set rsRead = CurrentDb.OpenRecordset(strReadSQL)
'make sure something was returned in the sql statement
If rsRead.RecordCount > 0 Then
'these if statements are set up based on the way the table is established
'if the lower bound of the criteria is not blank
If Nz(rsRead("CNumberSmall")) <> "" Then
'if the upper bound of the criteria is not blank
If Nz(rsRead("CNumberBig")) <> "" Then
'the code assumes the criteria is between the upper and lower
GetCriteriaValue = "Between " & rsRead("CNumberSmall") & _
" AND " & rsRead("CNumberBig")
Else
'if the upper bound is blank
'the code assumes the criteria is greather or equal to the small number
GetCriteriaValue = ">= " & rsRead("CNumberSmall")
End If
Else
'if the lowe bound is blank and
'if the upper bound of the criteria is not blank
If Nz(rsRead("CNumberBig")) <> "" Then
'the code assumes the criteria is less than or equal to the big number
GetCriteriaValue = "<= " & rsRead("CNumberBig")
Else
'if both boxes are empty something is wrong
MsgBox "Sorry, nothing found to match selected criteria in the tblClassificationsDefined table", vbCritical
End If
End If
Else ' check to see if the user selected other values
If Left(ctlTemp.Properties("Name"), 5) = "Other" Then
GetCriteriaValue = "Between " & Me.TxBox_Other_Min & _
" AND " & Me.TxBox_Other_Max
Else
'if both boxes are empty something is wrong
MsgBox "Sorry, nothing found to match selected criteria in the tblClassificationsDefined table", vbCritical
End If
End If
rsRead.Close
End If
End If
Next ctlTemp
End Function
Function GetFieldNames(strTblName As String, strSelectSQL As String, strWhereSQL As String, dteStart As Date, dteEnd As Date, frm As Form, strValue As String)
'this function goes through the strTblName and looks for any field
'which will qualify as a date field
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As Field
Dim dteTemp As Date
Dim blnFound As Boolean
blnFound = False
'add the needed open perethecies to the sql
strWhereSQL = strWhereSQL & "("
'establish the variables need to access the fields collection
Set db = CurrentDb
Set tbl = db.TableDefs(strTblName)
'establish a loop through the fields of this table strTblName
For Each fld In tbl.Fields
'if the field falls into the criteria of a date then <month><Year> with leading zero (0107,0207,0307...)
If IsDate(Left(fld.Name, 2) & "-01-" & Right(fld.Name, 2)) = True Then
'assigne the field name a real date based on the first day of the month
dteTemp = DateValue(Left(fld.Name, 2) & "-01-" & _
Right(fld.Name, 2))
'if the field name real date falls equal to or between the passed in start and dend date then
If dteTemp >= dteStart And dteTemp <= dteEnd Then
'build the segment for the select statement
Call BuildSelectTimePeriod(strSelectSQL, fld.Name)
'build the segment for the where statement
Call BuildWhereTimePeriod(strWhereSQL, frm, fld, strValue)
blnFound = True
End If
End If
Next fld
'clean up the sql as needed
If blnFound = True Then
strWhereSQL = Left(strWhereSQL, Len(strWhereSQL) - 4) & ") AND "
Else
strWhereSQL = Left(strWhereSQL, Len(strWhereSQL) - 1)
End If
End Function
Function BuildSelectTimePeriod(strSQL As String, strVal As String)
'used to build the required select portion of the sql statement for the time
'period specific fields
strSQL = strSQL & "[" & strVal & "], "
End Function
Function BuildWhereTimePeriod(strSQL As String, frm As Form, fld As Field, strValue As String)
'used to build the required where segment for the sql statement for the time
'period specific fields
strSQL = strSQL & "[" & fld.Name & "] " & strValue & _
" AND " 'by changing this and to an or the query is changed from each fielding matching to
'criteria to any field matching the criteria
End Function
Function BuildSelect(strSQL As String, ctl As Control)
'used to build the required select portion of the sql statement
strSQL = strSQL & "[" & Mid(ctl.Name, 4, Len(ctl.Name) - 3) & "], "
End Function
Function BuildWhere(strSQL As String, frm As Form, ctl As Control)
'used to build the required where segment for the sql statement
Dim ctlTemp As Control
Dim blnFound As Boolean
blnFound = False
strSQL = strSQL & "("
'loop through all the controls to find the correct set of check boxes
For Each ctlTemp In frm.Controls
'make sure the criteria selections on the form are check boxes and name correctly
If ctlTemp.Properties("ControlType") = acCheckBox Then
'if the control name contains an under score continue
If InStr(1, ctlTemp.Properties("name"), "_") > 0 Then
'if the first part of the control is named the same as the group name continue
If Left(ctlTemp.Properties("name"), InStr(1, ctlTemp.Properties("name"), "_") - 1) _
= Mid(ctl.Name, 4, Len(ctl.Name) - 3) Then
'if the correct control is checked then add it to the where clause
If ctlTemp.Value = True Then
strSQL = strSQL & "[" & Left(ctlTemp.Name, InStr(1, ctlTemp.Name, "_") - 1) & _
"] = '" & Right(ctlTemp.Name, Len(ctlTemp.Name) - InStrRev(ctlTemp.Name, "_")) & "' OR "
blnFound = True
End If
End If
End If
End If
Next ctlTemp
'clean up the sql as needed
If blnFound = True Then
strSQL = Left(strSQL, Len(strSQL) - 4) & ") AND "
Else
strSQL = Left(strSQL, Len(strSQL) - 1)
End If
End Function
Function blnCheckForm() As Boolean
'write code to ensure the user has entered all the data needed to run the sql builder
'specificly make sure the time period has four valid entries.
blnCheckForm = True
End Function
Private Sub Hierarchy_AfterUpdate()
'lock the needed controls so the user can not select them without the group
If Me.Hierarchy = True Then
Me.OB_UltP_Name.Locked = False
Me.OB_Cust_Name.Locked = False
Me.OB_CAS_ACCT_NUMBER.Locked = False
Me.OB_UltP_Name = False
Me.OB_Cust_Name = False
Me.OB_CAS_ACCT_NUMBER = False
Else
Me.OB_UltP_Name.Locked = True
Me.OB_Cust_Name.Locked = True
Me.OB_CAS_ACCT_NUMBER.Locked = True
Me.OB_UltP_Name = Null
Me.OB_Cust_Name = Null
Me.OB_CAS_ACCT_NUMBER = Null
End If
End Sub
Private Sub CustomTimePeriod_AfterUpdate()
'lock the needed controls so the user can not select them without the group
If Me.CustomTimePeriod = True Then
Me.FromMonth.Locked = False
Me.FromMonth = ""
Me.FromYear.Locked = False
Me.FromYear = ""
Me.ToMonth.Locked = False
Me.ToMonth = ""
Me.ToYear.Locked = False
Me.ToYear = ""
Me.PresetTimePeriod = False
Call PresetTimePeriod_AfterUpdate
Me.Large_Criteria_Time_Period.Locked = False
Me.Large_Criteria_Time_Period = False
Me.Medium_Criteria_Time_Period.Locked = False
Me.Medium_Criteria_Time_Period = False
Me.Small_Criteria_Time_Period.Locked = False
Me.Small_Criteria_Time_Period = False
Me.Other_Criteria_Time_Period.Locked = False
Me.Other_Criteria_Time_Period = False
Else
Me.FromMonth.Locked = True
Me.FromMonth = ""
Me.FromYear.Locked = True
Me.FromYear = ""
Me.ToMonth.Locked = True
Me.ToMonth = ""
Me.ToYear.Locked = True
Me.ToYear = ""
Me.Large_Criteria_Time_Period.Locked = True
Me.Large_Criteria_Time_Period = False
Me.Medium_Criteria_Time_Period.Locked = True
Me.Medium_Criteria_Time_Period = False
Me.Small_Criteria_Time_Period.Locked = True
Me.Small_Criteria_Time_Period = False
Me.Other_Criteria_Time_Period.Locked = True
Me.Other_Criteria_Time_Period = False
End If
End Sub
Private Sub PresetTimePeriod_AfterUpdate()
'lock the needed controls so the user can not select them without the group
If Me.PresetTimePeriod = True Then
Me.PresetTimePeriod_CmBox_2007.Locked = False
Me.PresetTimePeriod_CmBox_2007 = ""
Me.PresetTimePeriod_CmBox_2008.Locked = False
Me.PresetTimePeriod_CmBox_2008 = ""
Me.PresetTimePeriod_CmBox_2009.Locked = False
Me.PresetTimePeriod_CmBox_2009 = ""
Me.CustomTimePeriod = False
Call CustomTimePeriod_AfterUpdate
Me.Large_Criteria_Time_Period.Locked = False
Me.Large_Criteria_Time_Period = False
Me.Medium_Criteria_Time_Period.Locked = False
Me.Medium_Criteria_Time_Period = False
Me.Small_Criteria_Time_Period.Locked = False
Me.Small_Criteria_Time_Period = False
Me.Other_Criteria_Time_Period.Locked = False
Me.Other_Criteria_Time_Period = False
Else
Me.PresetTimePeriod_CmBox_2007.Locked = True
Me.PresetTimePeriod_CmBox_2007 = ""
Me.PresetTimePeriod_CmBox_2008.Locked = True
Me.PresetTimePeriod_CmBox_2008 = ""
Me.PresetTimePeriod_CmBox_2009.Locked = True
Me.PresetTimePeriod_CmBox_2009 = ""
Me.Large_Criteria_Time_Period.Locked = True
Me.Large_Criteria_Time_Period = False
Me.Medium_Criteria_Time_Period.Locked = True
Me.Medium_Criteria_Time_Period = False
Me.Small_Criteria_Time_Period.Locked = True
Me.Small_Criteria_Time_Period = False
Me.Other_Criteria_Time_Period.Locked = True
Me.Other_Criteria_Time_Period = False
End If
End Sub
Private Sub ToMonth_AfterUpdate()
'make sure the date values selected by the user are correct
If Nz(Me.ToMonth) <> "" And Nz(Me.ToYear) <> "" And Nz(Me.FromMonth) <> "" And _
Nz(Me.FromYear) <> "" Then
'make sure the from date is less than or equal to the to date
If DateValue("01-" & Me.ToMonth & "-" & Me.ToYear) < DateValue("01-" & Me.FromMonth & _
"-" & Me.FromYear) Then
MsgBox "To date selection must be later or equal to from date.", vbCritical
End If
End If
End Sub
Private Sub ToYear_AfterUpdate()
'make sure the date values selected by the user are correct
If Nz(Me.ToMonth) <> "" And Nz(Me.ToYear) <> "" And Nz(Me.FromMonth) <> "" And _
Nz(Me.FromYear) <> "" Then
'make sure the from date is less than or equal to the to date
If DateValue("01-" & Me.ToMonth & "-" & Me.ToYear) < DateValue("01-" & Me.FromMonth & _
"-" & Me.FromYear) Then
MsgBox "To date selection must be later or equal to from date.", vbCritical
End If
End If
End Sub
Private Sub FromMonth_AfterUpdate()
'make sure the date values selected by the user are correct
If Nz(Me.FromMonth) <> "" And Nz(Me.FromYear) <> "" And Nz(Me.FromMonth) <> "" And _
Nz(Me.FromYear) <> "" Then
'make sure the from date is less than or equal to the to date
If DateValue("01-" & Me.FromMonth & "-" & Me.FromYear) < DateValue("01-" & Me.FromMonth & _
"-" & Me.FromYear) Then
MsgBox "From date selection must be later or equal From from date.", vbCritical
End If
End If
End Sub
Private Sub FromYear_AfterUpdate()
'make sure the date values selected by the user are correct
If Nz(Me.FromMonth) <> "" And Nz(Me.FromYear) <> "" And Nz(Me.FromMonth) <> "" And _
Nz(Me.FromYear) <> "" Then
'make sure the from date is less than or equal to the to date
If DateValue("01-" & Me.FromMonth & "-" & Me.FromYear) < DateValue("01-" & Me.FromMonth & _
"-" & Me.FromYear) Then
MsgBox "From date selection must be later or equal From from date.", vbCritical
End If
End If
End Sub
Private Sub Large_Criteria_Time_Period_AfterUpdate()
'lock the needed controls so the user can not select them without the group
If Me.Large_Criteria_Time_Period = True Then
Me.Medium_Criteria_Time_Period = False
Me.Small_Criteria_Time_Period = False
Me.Other_Criteria_Time_Period = False
Call Other_Criteria_Time_Period_AfterUpdate
End If
End Sub
Private Sub Medium_Criteria_Time_Period_AfterUpdate()
'lock the needed controls so the user can not select them without the group
If Me.Medium_Criteria_Time_Period = True Then
Me.Large_Criteria_Time_Period = False
Me.Small_Criteria_Time_Period = False
Me.Other_Criteria_Time_Period = False
Call Other_Criteria_Time_Period_AfterUpdate
End If
End Sub
Private Sub Small_Criteria_Time_Period_AfterUpdate()
'lock the needed controls so the user can not select them without the group
If Me.Small_Criteria_Time_Period = True Then
Me.Large_Criteria_Time_Period = False
Me.Medium_Criteria_Time_Period = False
Me.Other_Criteria_Time_Period = False
Call Other_Criteria_Time_Period_AfterUpdate
End If
End Sub
Private Sub Other_Criteria_Time_Period_AfterUpdate()
'lock the needed controls so the user can not select them without the group
If Me.Other_Criteria_Time_Period = True Then
Me.Large_Criteria_Time_Period = False
Me.Medium_Criteria_Time_Period = False
Me.Small_Criteria_Time_Period = False
Me.TxBox_Other_Max.Locked = False
Me.TxBox_Other_Max = ""
Me.TxBox_Other_Min.Locked = False
Me.TxBox_Other_Min = ""
Else
Me.TxBox_Other_Max.Locked = True
Me.TxBox_Other_Max = ""
Me.TxBox_Other_Min.Locked = True
Me.TxBox_Other_Min = ""
End If
End Sub
Private Sub OB_Sub_Prod_Name_AfterUpdate()
'used to lock the check boxes for this group
If Me.OB_Sub_Prod_Name = True Then
Call LockControls(Me.Form, "Sub Prod Name", False)
Else
Call LockControls(Me.Form, "Sub Prod Name", True)
End If
End Sub
Private Sub OB_UltP_Geo_Region_Code_AfterUpdate()
'used to lock the check boxes for this group
If Me.OB_UltP_Geo_Region_Code = True Then
Call LockControls(Me.Form, "UltP Geo Region Code", False)
Else
Call LockControls(Me.Form, "UltP Geo Region Code", True)
End If
End Sub
Private Sub OB_UltP_Segment_AfterUpdate()
'used to lock the check boxes for this group
If Me.OB_UltP_Segment = True Then
Call LockControls(Me.Form, "UltP Segment", False)
Else
Call LockControls(Me.Form, "UltP Segment", True)
End If
End Sub
Function LockControls(frm As Form, strCtlName As String, blnValue As Boolean)
'lock or unlock all controls passed through
Dim ctl As Control
'cycle through each control on the form
For Each ctl In frm.Controls
If Left(ctl.Properties(1), Len(strCtlName)) = strCtlName Then
ctl.Locked = blnValue
ctl = False
End If
Next ctl
End Function
I am getting the error:
"The expression On Open you entered as the event property setting produced the following error: Procedure declaration does not match description of event or procedure having the same name."
I think this has to do with the Form_open code

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
The only thing I can think of is you have two on open routines.
When you hit ok you should get a debug option.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
First thing you need to do is add the 'Microsoft DAO 3.6 Object 2.1 Library' to your references as shown on posting
Date:04/16/09 05:18 PM
The error you are getting seems to be caused by the Preview_Cstm_Rpt_Btn_DBLCl
So...
The second thing you need to do is change the event property of the Preview_Cstm_Rpt_Btn from a double click to a single click. Then copy the code into the single click routine and delete the double click routine.
If case you are wondering how I figured out it was this perticular routine ... I opened the VBA code window and compiled the code. This shows you all errors that happen and where, just like debug.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Function EraseTable(TableName As String)
'Delete the table specified
If CheckTable(TableName) = True Then DoCmd.DeleteObject acTable, TableName
End Function
Function CheckTable(TableName As String) As Boolean
On Error GoTo subexit:
If CurrentDb.TableDefs(TableName).Name = TableName Then
CheckTable = True
End If
subexit:
End Function
Function EraseQuery(QueryName As String)
'Delete the Query specified
If CheckQuery(QueryName) = True Then DoCmd.DeleteObject acQuery, QueryName
End Function
Function CheckQuery(QueryName As String) As Boolean
On Error GoTo subexit:
If CurrentDb.QueryDefs(QueryName).Name = QueryName Then
CheckQuery = True
End If
subexit:
End Function






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
"If Left(ctlTemp.Properties("n
This is because a check routine needs to be written to ensure the form is filled out correctly. Example is when custom time period is choosen the combo boxes for month and year must be filled out before the preview button is selected.
The only thing I can think of that would cause your error is if it cannot find the option bubble or check box you selected or the check box is named wrong. In this perticular line is checking to see if the first part of the control(option bubble for each group) is named the same as the field(check boxes).
Can you tell me exactly what you are selecteding to get the error?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
There are some modifications I need to make.
For the preset time period, I want the user to be able to select any individual month or quarter for each of the years, or the entire year.
What might get tricky is defining the classifications as small, medium or large. If just one month is selected, then large clients will be defined as those who generated more than $416.6667k. If a quarter is selected, large clients will be >1.25mm and if the full year is selected it will be back to >5mm. If custom time period is selected, the user will be forced to define an 'other' classification. I would also like to have the 'show all' option in which no classification setting is required.
Another issue is with the date field names. The filed names are based on a particular reporting system and are not the actual time periods. So 0209 is actually January data. In the table, the field will still be 0209, but to the user it will be January. Is there a way to deal with this?
First, the preset time period is defined by the LoadComboBoxes function. In this function you can add code to include month or year. Basicly it creates a temporary table which contains the lable (what the user sees) and then a start date and end date. So for a specific month the data would be March, 3/1/09, 3/31/09.
Second, the classifications can be redefined base on other input, it would just have to be code that way in the that would have to be coded in the GetCriteriaValue function. The classifications are defined in a table tblClassificationsDefined and this table could hold more values or you could select different values based on user input (like qtr, month, or year).
Third, the best way to deal with this would be to keep the coding the way it is (for the most part) and just subtract or add a month as needed.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Select Case Month(dteTemp)
Case 1, 2, 3
strQtr = "1Q " & Year(dteTemp)
Case 4, 5, 6
strQtr = "2Q " & Year(dteTemp)
Case 7, 8, 9
strQtr = "3Q " & Year(dteTemp)
Case 10, 11, 12
strQtr = "4Q " & Year(dteTemp)
' End Select
' Select Case Month(dteTemp)
Case 1
strQtr = "Jan " & Year(dteTemp)
Case 2
strQtr = "Feb " & Year(dteTemp)
Case 3
strQtr = "Mar " & Year(dteTemp)
Case 4
strQtr = "Apr " & Year(dteTemp)
Case 5
strQtr = "May " & Year(dteTemp)
Case 6
strQtr = "Jun " & Year(dteTemp)
Case 7
strQtr = "Jul " & Year(dteTemp)
Case 8
strQtr = "Aug " & Year(dteTemp)
Case 9
strQtr = "Sep " & Year(dteTemp)
Case 10
strQtr = "Oct " & Year(dteTemp)
Case 11
strQtr = "Nov " & Year(dteTemp)
Case 12
strQtr = "Dec " & Year(dteTemp)
'End Select
' Select Case Month(dteTemp)
Case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
strQtr = "Full Year " & Year(dteTemp)
End Select
But it is just picking up on the first part, so it still only displays the quarters
Add this after each end select. That gets you most of the way there. The segment that writes the data for each month will have to modified a little to grab the last day of the month as well.
'create the needed sql to append the data to the temporary table
strWriteSQL = "INSERT INTO tmptblComboBoxData ( FieldName, FieldDate, FieldMonth, FieldYear, FieldQtr ) " & _
"SELECT '" & fld.Name & _
"', #" & dteTemp & _
"#, " & Left(fld.Name, 2) & _
", " & Right(fld.Name, 2) & _
", '" & strQtr & "';"
CurrentDb.Execute (strWriteSQL)






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
HAVING(Sum([0209]+[0109])
and the second part which would add on any criteria looking like:
AND [UltP Segment] = 'AM'
I have attached another sample db so you can get an idea of where I am.
Thank you so much again for your help

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Look at the attached copy of the database.
I took the code back to where it was the last time I uploaded it and added the code needed to do the following:
I created another field that is generated automaticly based on the preset criteria or custom criteria.
Also I added a couple of fields to the combo boxes so it sorts the way you want it.
One thing. Before you zip up and upload your database make sure you have compacted and repaired the database. The last upload was 73.1M. In reality the db size is about 600K.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
At this point, the easiest is the second option:
To do the second option all you have to do is comment out this line of code in the getFieldNames function
Call BuildSelectTimePeriod(strS

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
on this line
If Nz(ctlTmp.Column(0)) <> "" Then
SELECT Sum([0108]+[0208]+[0308]+[
FROM MasterData
GROUP BY MasterData.[UltP Segment], MasterData.[UltP Name]
HAVING (((Sum([0108]+[0208]+[0308






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I added a specific client section. So here, the user would enter the name of the UltP and the query would return only data for that client. I put in a button that will pull up a list of the UltPs so the user will be able to just copy and paste from that list into the textboxes.
Also in the second box on the form (upper right quadrant) I added 'show all' which appears twice and added 'other' an additional time. The idea here is that the user can select from large medium or small if they are choosing a preset time period, but if they have chosen custom, they are only able to define an 'other' revenue range. Selecting 'Show all' will simply not add any criteria to the revenue field, but I want it to be so that if it is selected, you can't select Large medium or small.
One small thing: once the user clicks in the 'other' check box, the cursor should move to the max and min entry boxes and blink extra visibly.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I don't want to make extensive changes if you are working on the same things, but if you re unable to look at it anymore today, let me know and i will continue trying different ideas.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
You added other and all for custom and you added all for preset.
The way it is coded night now, the user selects either custom or preset on the left and whatever they chose for classification applies to preset or custom (depending on what they choose).
So do you need classification for custom by its self?
Emailed as well






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I think I got it. I just needed to switch the 2 to a 7 in the above line

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Also, what do you think the best way is for me to go about re defining the classifications small, medium and large to be more specific? If you click on the button on the form below s m & l it explains how I am trying to define them.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Do you think this is possible?

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
All other thoughts about what it will take to complete this db at this time:
I will add a check box to the main form that will be similar to 'other' and 'show all'. If the lost box is checked then the query will have to execute all the given criteria on the sheet just like any other query, but the results will just be lost clients during the specified time period. Lost will for now simply be defined as any client that has 0 revenue in the last month of the time period selected. So if you selected preset, full year 2008, lost, ultp name, you should get any client that had zero revenue as of december 2008, but if you selected a custom time period (say Jan 2008 to March 2009), lost and ultp name and one of the clients that had zero rev in dec 08 picked up again in q1 09, the client would no longer be considered lost. The one problem is that we also wouldnt consider a client that had 0 during the entire time period lost, so the query should first eliminate all records with all 0 rev from its considerations of potential new clients.
But actually if we just defined an 'other' revenue class and set the minimum to anything above $0.00, this should handle the issue.
So if we just add the lost check box and add that if checked, it gets added to the where clause that the end month must equal 0, the queries should then return still a sum of the time period, so even though lost in the time period, the query would show what ever revenue the client had until it was lost which would indicate the total impact to revenue in the next time period.
On thing that I would need to add would be having the option of showing all months in the time period, like we did at first. This would allow the user to see exactly what month the client dropped off. If we have this as an option, I assume we could have it as an option for even if lost is not selected.
Maybe this could be done by innputting two more option buttons next to 'choose the time period' on the form. The options would be: Detail(show all months in the time period) and summary ( give the total for the time period)
Then when that is figured out, I just need to keep testing and testing to make sure very combination works. Some little things that should be easy like locking segment, region and sub product criteria selections, so that they cannot be checked unless their corresponding field selector option button is selected.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
1. Have the option of selecting a WHERE clause, even if the field option button isn't selected.
2. The summary and detail options.
3. The lost clients query as mentioned above in a previous post.
#1 This would allow the user to select criteria for a field that would not show in the query, the logic of a where clause. Right now on the main form if you don't select the field in the 3rd box, you can not set criteria for that field in the 4th box. I want to change this so that if the user doesnt select the field in the third box, they can still set criteria for the field, there just won't be any grouping at that level so the criteria will be combined together and grouped at the other level selected.
#2 The summary and detail options. Basically right now the form is in summary mode. We discussed this before and agreed that it would be best to have one column of data as the sum of the time period selected and not show every individual month of the time period selected. If detail mode is selected, I want the queries to return all the individual months and a total column. I beleive the detail mode will be necessary for the lost clients query.
To address the second one, I would do simply check to see of the detail button is checked and then include the fields as part of the select statement.
Do you need help doing these changes?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Here is my latest db
As for the lost clients query, If I understand what you want, all you must do is change the where statement to include any client with a zero balance during your time period.
As for the lost clients query, it would need to show those clients that have 0 for the last month of the time period selected, not the whole period

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Microsoft Access
--
Questions
--
Followers
Top Experts
Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.