Question Count from database culumn following a condition statement

Abdoul Rahim

Member
Joined
Oct 16, 2017
Messages
15
Programming Experience
Beginner
I have an access database with Four Columns (Name,Sex,DOB,Contact). I want to make a query from the database; to count the numbers of names in the column "Name" where Date of today minus Date of Birth is less than 15 (DateTime.Today - DOB < 15). That is, Find peoples with age less than 15. But i face difficulties in doing that. Please help.
 
But i received an Error Message concerning an error on the Query.

And you thought that the best course of action was to not inform those whom you would like to help what the error message was? You should learn the lesson now that you need to provide a FULL and CLEAR explanation of the problem at the outset, which includes what you're trying to achieve, how you're trying to achieve it (includes any relevant code) and what happens when you try (includes any error messages and where they occur). If you went to a doctor for your health or a mechanic for your car, would you purposely only tell them half the story?

Anyway, DateTime.Today is .NET code, so it's meaningless to your database. If you want to use the current date then you have to either get it in your C# code and then insert that into your SQL, which you would do using an ADO.NET parameter (follow the Blog link in my signature below for a post on that subject) or else use the function provided by your database to get that value.
 
You can get the current date from the database by calling a function in the query, what that function is called depends on the database system you're using which we don't know yet.
 
You can use Now() to get the current date & time, Date() to get the current date, & Time() to get the current time in Access queries, you can even use them in the Where and Order By clauses of queries.
Also if you're wanting to do a comparison between two dates you can use DateDiff to get the number of days, months, or years between them, more info: MS Access: DateDiff Function
 
Back
Top Bottom