How to filter a combo box? (1 Viewer)

cheberdy

Member
Local time
Today, 09:38
Joined
Mar 22, 2023
Messages
77
If I have a query and I want to feed a combobox with these values to sort a form. How do you do this?
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:38
Joined
Sep 21, 2011
Messages
14,319
Use the query as the source for the combo?
 

cheberdy

Member
Local time
Today, 09:38
Joined
Mar 22, 2023
Messages
77
Use the query as the source for the combo?
If dayvalues are the values that are available for selection and are from the Day query. Then
SELECT DISTINCT dayvalues FROM Day;
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:38
Joined
Feb 19, 2002
Messages
43,302
@cheberdy You are very new here so allow me to explain how forums work. There are sections so you can decide where your question might be most appropriate. This adds others when they are searching for answers to narrow their search. The people who answer your questions normally read new posts in all sections unless they have no relevant expertise. I, for example, almost never read the Macro section since I don't write macros, I write VBA and so I limited knowledge to share but I even answer there if the top question seems like I can help.

Therefore, it is not necessary, nor is it advisable to keep starting new threads on basically the same topic. If you know exactly what you are doing and really have only a syntax problem, then post the code/query and we'll help to work it out. But you seem to be struggling with combos. Maybe if you did an overview of what your goal is - not the code - the goal, we would be better placed to provide viable help.
 

cheberdy

Member
Local time
Today, 09:38
Joined
Mar 22, 2023
Messages
77
@cheberdy You are very new here so allow me to explain how forums work. There are sections so you can decide where your question might be most appropriate. This adds others when they are searching for answers to narrow their search. The people who answer your questions normally read new posts in all sections unless they have no relevant expertise. I, for example, almost never read the Macro section since I don't write macros, I write VBA and so I limited knowledge to share but I even answer there if the top question seems like I can help.

Therefore, it is not necessary, nor is it advisable to keep starting new threads on basically the same topic. If you know exactly what you are doing and really have only a syntax problem, then post the code/query and we'll help to work it out. But you seem to be struggling with combos. Maybe if you did an overview of what your goal is - not the code - the goal, we would be better placed to provide viable help.
I understand. Can you help me. I have a problem with the code, so that two comboboxes should filter a form. Year is the table for the form. By month should be filtered. Start month is the first combo box.End month is the second combox. With both you define a range for filtering.
I have debugged it and strSQL is always empty

Code:
Private Sub startmonth_AfterUpdate()
    Dim strSQL As String
    
    
    'Build the SQL query to search for the value
    strSQL = "SELECT * FROM year WHERE [month] = month Between '" & Me.startmonth.Value & " ' And '" & Me.endmonth.Value & "'"
    
    'Execute the query
    Me.RecordSource = strSQL
    
    'Refresh the form
    Me.Refresh
End Sub
Private Sub endmonth_AfterUpdate()
    Dim strSQL As String
    
    'Build the SQL query to search for the value
    strSQL = "SELECT * FROM year WHERE [month] = month Between '" & Me.startmonth.Value & " ' And '" & M.endmonth.Value & "'"
    
    'Execute the query
    Me.RecordSource = strSQL
    
    'Refresh the form
    Me.Refresh
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:38
Joined
Sep 21, 2011
Messages
14,319
Dates need # surrounding them? Also in mm/dd/yyyy or yyyy-mm-dd format.
Strings need single quotes, unless a single quote is in the string. Numerics need nothing surrounding them.
You appear to be trying to use Access without any attempt to find out anything yourself.? :(
Very soon I myself, at least, will stop responding, as most of your queries can be solved with just a simple google search. :(

I say that as you have only joined just last Wednesday, but posted over 30 questions? :(

Start learning to debug and walk through your code with F8 and use debug.print and immediate window to see what you actually have, and not what you *think*you have.
See my signature on debugging.
 
Last edited:

cheekybuddha

AWF VIP
Local time
Today, 08:38
Joined
Jul 21, 2014
Messages
2,280
One separate thing: since you are using VBA code, ensure every code module has Option Explicit declared at the top (either above or below Option Compare Database).

This will help you catch mistakes with undeclared variables in your code which are missed and will cause unexpected behaviour otherwise.
Code:
' ...
    'Build the SQL query to search for the value
    strSQL = "SELECT * FROM year WHERE [month] = month Between '" & Me.startmonth.Value & " ' And '" & M.endmonth.Value & "'"
'                                                                                                      ^
'                                                                                                      |
' ...

Also, your SQL is incorrect - you will want to end up with:
SELECT * FROM [Year] WHERE [Month] BETWEEN #(start date)# AND #(end date)#
(Note: NOT [month] = month Between ...)

But first, you probably ought to look at your table design, and definitely naming of fields.

Post your tables and their structure (field names, datatypes) here and folks will help you review.
 

Users who are viewing this thread

Top Bottom