Advertisement
Dotterbart

Make Refference

Mar 3rd, 2024 (edited)
1,463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Function MakeRef(dat_Date As Date) As String
  2. Dim DB As DAO.Database
  3. Set DB = CurrentDb
  4. Dim RS As DAO.Recordset
  5. Set RS = DB.OpenRecordset("SELECT * FROM Tab_DateRefs ORDER BY Data_Ref;")
  6. Dim strRef As String
  7. Dim intMon As Integer
  8. Dim strMon As String
  9. Dim strSQL As String
  10. Dim strTemp As String
  11.  
  12. strRef = Right(Year(dat_Date), 2)
  13.  
  14. intMon = Month(dat_Date)
  15. strMon = Chr(64 + intMon) 'ASCII 65 = A
  16.  
  17. strRef = strRef & strMon
  18.  
  19. strSQL = "Data_Ref LIKE '" & strRef & "*'" 'Tab_DateRefs is my Table Data_Ref is where the refferences are stored
  20. RS.MoveFirst
  21. RS.FindLast strSQL
  22.  
  23. If RS.NoMatch Then 'If your DB is empty or a new month begins the RS will not find anything
  24.    MakeRef = strRef & "01"
  25. Else
  26.     If CInt(Right(RS!Data_Ref, 2) + 1) < 10 Then
  27.         MakeRef = Left(RS!Data_Ref, 4) & CStr(CInt(Right(RS!Data_Ref, 2) + 1)) ' 4 = with the leading zero for 01-09
  28.    Else
  29.         MakeRef = Left(RS!Data_Ref, 3) & CStr(CInt(Right(RS!Data_Ref, 2) + 1)) ' 3 = without the leading zero for 10 to 99
  30.    End If
  31. 'Alternativ or for more then 2 digits
  32.        'If RS.NoMatch Then
  33.        '    MakeRef = strRef & "01" 'Use "001" for 3 Digits
  34.        'Else
  35.        '    MakeRef = Left(RS!Data_Ref, 3) & Format(CStr(CInt(Right(RS!Data_Ref, 2) + 1)), "00") 'Use "000" for 3 Digits
  36.        'End If
  37. End If
  38.  
  39. RS.Close
  40. DB.Close
  41. Set DB = Nothing
  42. Set RS = Nothing
  43. End Function
Tags: ACCESS CODE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement