Jace K
Jace K

Reputation: 11

How to filter results in listbox (VB)?

I'm using visual studio, and I'm using a column from a database as DataSource and DisplayMember. So, the listbox I have on my form has a list of Names found in my database. In this same database (Microsoft Access Database 2003), I have a column called Date. I want the listbox to only show the Names corresponding to today's date (the date is in number form; only the day of the month (1 to 31). I can used Today.Day on vb to check today's day of month). Please help!

Upvotes: 0

Views: 368

Answers (1)

Johnny Bones
Johnny Bones

Reputation: 8402

Your listbox's DataSource should be

SELECT [Name] FROM MyTable WHERE [Date] = Day(GetDate());

You may have to change that to

SELECT [Name] FROM MyTable WHERE [Date] = Day(Now());

In the first case, that will work on SQL Server. In the second case, that will work in VBA. I wasn't quite clear on where this was run based on your question.

Also, just a suggestion moving forward. Avoid "Reserved words" in field names. For instance, Date is a reserved word. When you use it as a field name and try to reference it in a query, you must enclose it in brackets or it won't work. It's more of a hassle than it's worth. What I do is call date fields "MyDate" or "StDate" or something, and then I never have to worry about tripping over brackets. Same with Name, which is also a reserved word.

Upvotes: 1

Related Questions