Solved How to convert a month into a number in MS Access Query

nector

Member
Local time
Today, 13:27
Joined
Jan 21, 2020
Messages
516
I want to a convert a month written like , 31/01/2025 into a number example = 1 etc , is there a function that I can use at MS Access Query level

I tried this below its not doing what I want:

Code:
Private Function GetMonthNumber(pstrMonthName As String) As Integer
Dim i As Long
For i = 1 To 12
  If pstrMonthName = MonthName(i) Then
     GetMonthNumber = i
     Exit Function
  End If
Next
End Function
 
In queries:
Code:
SELECT  ..... ,   Month(your_date_field) As YourFieldName From Your Table Where YourFilter

In vba :
Code:
yourVariable = Month(YourDate)
 
you may try:
Code:
month(datevalue("31/01/2025"))
 
I'm a little surprised you need to ask this as it has been a standard Function in Access since day1.
Maybe get a copy of the Access Language Reference, which I'm sure will be helpful?

When someone starts a new computer language, I always suggest that they read the language reference if only to compare equivalent functions to improve understanding and familiarity. There are some Functions used more than others and the syntax is often different.
 
Day() , Month() and Year() are inbuilt functions that will extract the numeric values of those parts of a date field.
I too am amazed you couldn't find them via Google!

 
There are many useful VBA functions, Do a search for Access VBA functions and bookmark or print out the function list grouped by category. The alpha list is useless unless you know the name of the function you need. The category list shows ALL date related functions together where it is very easy to pick out the one you want. This is what I use when I can't remember the name of a function I don't use regularly.
 
Another quick and easy reference

 

Users who are viewing this thread

Back
Top Bottom