Show months from date (1 Viewer)

JAQ

New member
Local time
Today, 04:27
Joined
Dec 15, 2021
Messages
9
Dear,

I am looking to get vba or such to show month name in sequence with coma like;

suppose dates are from 17 Jan 2021 to 03 April 2021 so i want months name to be shown like this;

Jan-21, Feb-21, Mar-21, Apr-21
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:27
Joined
May 7, 2009
Messages
19,245
use Format$() function:

Format$(theDateField, "mmm-yy")

you can wrapped this into a udf:

Code:
Public Function fncMonthYear(byval dteStart As Date, Optional Byval dteEnd As Date = 0)
Dim tmp As Date, Dim ret As String
If dteEnd = 0 Or dteStart = dteEnd Then
    fncMonthYear = Format$(dteStart, "mmm-yy")
    Exit Function
End If
dteStart = DateSerial(Year(dteStart), Month(dteStart), 1)
dteEnd = DateSerial(Year(dteEnd), Month(dteEnd), 1)
If dteEnd < dteStart Then
    tmp = dteStart
    dteStart = dteEnd
    dteEnd = tmp
End If
tmp = dteStart
Do Until tmp > dteEnd
    ret = ret & Format$(tmp, "mmm-yy") & ", "
    tmp = DateAdd("m", 1, tmp)
Loop
If Len(ret)<>0 then
    ret = Left$(ret, Len(ret)-2)
    fncMonthYear = ret
End If
End Function

To Call the function:

Dim strDate As String
strDate = fncMonthYear(#01/17/2021#, #04/03/2021#)

//Edit: thanks to oleronessoftwares
 
Last edited:

oleronesoftwares

Passionate Learner
Local time
Yesterday, 16:27
Joined
Sep 22, 2014
Messages
1,159
I am looking to get vba or such to show month name in sequence with coma like;

suppose dates are from 17 Jan 2021 to 03 April 2021 so i want months name to be shown like this;

Jan-21, Feb-21, Mar-21, Apr-21
type mmm- yy in the format property on the control in the form
 

oleronesoftwares

Passionate Learner
Local time
Yesterday, 16:27
Joined
Sep 22, 2014
Messages
1,159
Thanks for reply, this will show only one month name whereas i need months name in continuity from a date serial
So you need to be able to store begin date and end date in two separate columns.

then you can loop through the months from the begin date and end date.

One question, where will this result be shown, a form, report etc?
 
  • Like
Reactions: JAQ

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:27
Joined
Oct 29, 2018
Messages
21,474
Thanks for reply, this will show only one month name whereas i need months name in continuity from a date serial
Hi. Welcome to AWF!

Didn't you try the code @arnelgp posted earlier?
 

Users who are viewing this thread

Top Bottom