{"id":487,"date":"2019-02-24T14:49:03","date_gmt":"2019-02-24T22:49:03","guid":{"rendered":"https:\/\/doubleecpu.com\/?page_id=487"},"modified":"2019-06-26T00:39:34","modified_gmt":"2019-06-26T07:39:34","slug":"vba-access-combo-box-updating","status":"publish","type":"page","link":"https:\/\/doubleecpu.com\/index.php\/microsoft\/visual-basic-for-applications\/vba-access-combo-box-updating\/","title":{"rendered":"VBA Access Combo Box Updating"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">Option Compare Database<br> Option Explicit<br> ''<em>''<br> ' Name:<br> ' Date: Created Date<br> ' Author: Gilbert Medel<br> ' Current Version: 1.0<br> '   Called by:<br> '<br> ''<\/em>''<br> ' Notes:<br> ' SRpt_Data references subreport object that must be on form<br> ' Query_Selected_1, Query_Selected_2, Query_Default should be designed to include all data for the Reports and drop down<br> ' Report_Based_on_Query_1, Report_Based_on_Query_2 , Report_Based_on_Query_Default should be designed to display the data with no filters<br> ' Data filtering will be accomplished by generating the SQL after changing the combo box values.<br> ' Combo_Box_Query_Select Will let users select the Query Data<br> ' Combo_Box1 and Combo_Box2 will let the user select data from Query Data that is filtered after selecting other Combo_Box values<br> ' After Combo_Box is updated the Control.RowSource Queries will be updated to filter other combo box values <br> ' Filter_combo_Box# will hold each parameter for filtering. The operator will be AND for filtering the data <br> ' Sub Generate_SQL will concatenate Query Name and Filter_'s To produce the SQL for the SRpt<br> ' The SRpt_Data.SourceObject is reassigned and the Form will update.<br>''<em>''<br>' Public Variables<br>''<\/em>''<br>' Private Variables<br>Private SRpt_Recset As Object<br>Private Filter_Query_Select As Variant<br>Private Filter_Combo_Box1 As Variant<br>Private Filter_Combo_Box2  As Variant<br>Private Filter_ShowArchives As Variant<br>Private Query_Name As Variant<br>Private Sub_Report_Name As Variant<br>Private Temp_Query As Variant<br>''<em>''<br>' Public properties<br>''<\/em>''<br>' Public Functions that return values<br>''<em>''<br>' Public Sub Routines<br>''<\/em>''<br>' Private Functions<br>''<strong><em>*<\/em><\/strong>''<br>' Private Subs<br>Private Sub Form_Open(Cancel As Integer)<br>     'Clears data until user selects combo box values<br>     Clear_Data_Report<br> End Sub<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub Combo_Box_Query_Select_AfterUpdate()<br>     Select Case Combo_Query_Select<br>         Case \"Query 1 Name\"<br>             Query_Name = \"Query_Selected_1\"<br>             Sub_Report_Name = \"Report.Report_Based_on_Query_1\"<br>             Form_Title.Caption = \"Report Name Query 1<br>         Case \"Query 2 Name\"<br>             Query_Name = \"Query_Selected_2\"<br>             Sub_Report_Name = \"Report.Report_Based_on_Query_2\"<br>             Form_Title.Caption = \"Report Name Query 2<br>         Case Else<br>             If IsNull(Combo_Query_Select) Then<br>                 Combo_Query_Select = \"Query_Default\"<br>             End If<br>             Query_Name = \"Q_Query_Default\"<br>             Sub_Report_Name = \"Report.Report_Based_on_Query_Default\"<br>             Form_Title.Caption = \"Report Name Default\"<br>     End Select<br>     If Combo_Query_Select = \"Archive\" Then<br>         Filter_ShowArchives = \"(( \" &amp; Query_Name &amp; \".ShowArchives)=False) \"<br>     Else<br>         Filter_ShowArchives = \"(( \" &amp; Query_Name &amp; \".ShowArchives)=True) \"<br>     End If<br>     Clear_Data_Report<br>     Combo_Box1 = Null<br>     Combo_Box2 = Null<br>     Store_Form_Values<br>     Generate_SQL<br>     'Generate_Report<br> End Sub<br><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">  Private Sub Combo_Box1_AfterUpdate()<br>     Clear_Data_Report<br>     Combo_Box2 = Null<br>     Store_Form_Values<br>     Generate_SQL<br>     Generate_Report<br> End Sub<br><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"> <br> Private Sub Combo_Box2_AfterUpdate()<br>     Clear_Data_Report<br>     Store_Form_Values<br>     Generate_SQL<br>     Generate_Report<br> End Sub<br><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"> Private Sub Clear_Data_Report()<br>     SRpt_Data.SourceObject = \"\"<br> End Sub  <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub Store_Form_Values()<br>\n    If IsNull(Combo_Query_Select) Then<br>\n        Filter_Query_Select = \" ((\" &amp; Query_Name &amp; \".From_Query_Name) IS NOT NULL) \"<br>\n    Else<br>\n        Filter_Query_Select = \"((\" &amp; Query_Name &amp; \".From_Query_Name)= \" &amp; Chr(39) &amp; Combo_Query_Select &amp; Chr(39) &amp; \") \"<br>\n        Generate_SQL_For_Combo_Box1<br>\n    End If<br>\n    If IsNull(Combo_SWUPGRADE) Then<br>\n        Filter_Combo_Box1 = \" ((\" &amp; Query_Name &amp; \".[Combo_Box1_Control_Field_Name]) IS NOT NULL) \"<br>\n        Generate_SQL_For_Combo_Box2<br>\n    Else<br>\n        Filter_Combo_Box1 = \" ((\" &amp; Query_Name &amp; \".[Combo_Box1_Control_Field_Name])= \" &amp; Chr(39) &amp; Combo_Box1 &amp; Chr(39) &amp; \") \"<br>\n        Generate_SQL_For_Combo_Box2<br>\n    End If<br>\n    If IsNull(Combo_Box2) Then<br>\n        Filter_Combo_Box2 = \" ((\" &amp; Query_Name &amp; \".[Combo_Box2_Control_Field_Name]) IS NOT NULL) \"<br>\n    Else<br>\n        Filter_Combo_Box2 = \" ((\" &amp; Query_Name &amp; \".[Combo_Box2_Control_Field_Name])= \" &amp; Chr(39) &amp; Combo_Box2 &amp; Chr(39) &amp; \") \"<br>\n    End If<br>\nEnd Sub<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub Generate_SQL()<br>     Set SRpt_Recset = CurrentDb.QueryDefs(\"Q_Data_From_Query_Name\")<br>     Temp_Query = \" SELECT \" &amp; Query_Name &amp; \".* \" &amp; _<br>                                         \"FROM \" &amp; Query_Name &amp; \" \" &amp; _<br>                                         \"WHERE (\" &amp; Filter_Query_Select &amp; \" AND \" &amp; Filter_Combo_Box1 &amp; \" AND \" &amp; Filter_Combo_Box2 &amp; \") ORDER BY [Field_Name_For_Sorting];\"<br>     SRpt_Recset.SQL = Temp_Query<br>End Sub <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"> Private Sub Generate_Report()<br>     SRpt_Data.SourceObject = Sub_Report_Name<br> End Sub <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"> Private Sub Generate_SQL_For_Combo_Box1()<br>     Dim Only_Active As Variant<br>     Combo_Box1.RowSource = \"SELECT DISTINCT \" &amp; Query_Name &amp; \".[Combo_Box1_Control_Field_Name], \" &amp; _<br>                                                                  Query_Name &amp; \".ShowArchives, \" &amp; Query_Name &amp; \".From_Query_Name\" &amp; _<br>                                                                  \" FROM \" &amp; Query_Name &amp; _<br>                                                                  \" WHERE (\" &amp; Filter_ShowArchives &amp; \" AND \" &amp; Filter_Query_Select &amp; \");\"<br> End Sub<br> Private Sub Generate_SQL_For_Combo_Box2()<br>     Combo_Box2.RowSource = \"SELECT DISTINCT \" &amp; Query_Name &amp; \".[Combo_Box2_Control_Field_Name], \" &amp; _<br>                                                                  Query_Name &amp; \".ShowArchives, \" &amp; Query_Name &amp; \".From_Query_Name\" &amp; _<br>                                                                  \" FROM \" &amp; Query_Name &amp; _<br>                                                                  \" WHERE (\" &amp; Filter_ShowArchives &amp; \" AND \" &amp; Filter_Query_Select &amp; \" AND \" &amp; Filter_Combo_Box1 &amp; \");\"<br> End Sub<br> ''<strong><em>*<\/em><\/strong>''<br> 'End Code<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Option Compare Database Option Explicit &#8221;&#8221; &#8216; Name: &#8216; Date: Created Date &#8216; Author: Gilbert Medel &#8216; Current Version: 1.0 &#8216; Called by: &#8216; &#8221;&#8221; &#8216; Notes: &#8216; SRpt_Data references subreport object that must be on form &#8216; Query_Selected_1, Query_Selected_2, Query_Default should be designed to include all data for the Reports and drop down &#8216; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/doubleecpu.com\/index.php\/microsoft\/visual-basic-for-applications\/vba-access-combo-box-updating\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;VBA Access Combo Box Updating&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":591,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-487","page","type-page","status-publish","hentry"],"featured_media_urls":[],"_links":{"self":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/comments?post=487"}],"version-history":[{"count":0,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/487\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/591"}],"wp:attachment":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/media?parent=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}