Function / Update Query - change Month text to Month Number (Aug -> 8) (1 Viewer)

Rx_

Nothing In Moderation
Local time
Today, 06:52
Joined
Oct 22, 2009
Messages
2,803
Data imports with a column of Year and column for Month where month is "Jan", "Feb", ...
Use an Update Query with a custom VBA Formual to convert. Add a new coloumn "MO" for the numeric version of Month.
(See attachment)

Code:
Function MonthText2Number(sMonthName As String) As Integer
      ' in Access table, create field MO - Then use this in Update Query
      ' In an Access Create a Module with this function
      ' sMonthName = "Oct"   ' October will also work  October returns a 10
10    MonthText2Number = Month(DateValue("01-" & sMonthName & "-2000"))
20    Exit Function
ErrorTrap:
30        MonthText2Number = 0
End Function
 

Attachments

  • MonthToMO.gif
    MonthToMO.gif
    48.9 KB · Views: 249

boblarson

Smeghead
Local time
Today, 05:52
Joined
Jan 12, 2001
Messages
32,059
There is also this method (courtesy of Bob Askew - Raskew on the forum here):
Code:
[FONT=arial]'*******************************************[/FONT][FONT=times new roman][/FONT]
[FONT=arial]Public Function RtnMonthNum(pstrMonName As String) As Integer
'*******************************************
'Purpose:   Returns month number (1 - 12)
'           when provided a complete or partial
'           month name.
'Coded by:  raskew
'Input:     From debug (immediate) window:
'           1) ? RtnMonthNum("April")
'           2) ? RtnMonthNum("Sep")
'Output: 1) 4
'           2) 9
'*******************************************[/FONT][FONT=times new roman][/FONT]
[FONT=times new roman] [/FONT]
[FONT=arial]Dim strHold  As String
Dim strMonth As String
Dim intMonth As Integer[/FONT][FONT=times new roman][/FONT]
[FONT=times new roman] [/FONT]
[FONT=arial]   strMonth = "JanFebMarAprMayJunJulAugSepOctNovDec"
   strHold = Left(pstrMonName, 3)
   intMonth = InStr(strMonth, strHold)
   RtnMonthNum = intMonth \ 3 + 1[/FONT][FONT=times new roman][/FONT]
[FONT=times new roman] [/FONT]
[FONT=arial]End Function[/FONT]
 

Users who are viewing this thread

Top Bottom