I just need to preface this to say that I am a complete noob with Macro’s, BASIC, and VBA programming, and I humbly apologize.
I am attempting to port a Microsoft Access database to LibreOffice Base as I will soon lose access to the Microsoft application. A while ago, I located a function in VBA code that I have working in MS Access, which is obviously not directly compatible with LibreOffice Base BASIC. I have been looking and reading for the past couple of weeks trying to do this myself, but I just do not understand and have been running into roadblocks every step of the way.
The Situation/Requirements
- I have 2 date fields (same table and same form), formatted as DD/MM/YYYY.
- The time difference between these date fields needs to be calculated in years, months and days (at this point, it doesn’t matter about 0 values).
- The resulting string (shown on the form in a read-only field) needs to be updated any time one of these dates change.
LibreOffice Information
Version: 7.6.7.2 (x86) / LibreOffice Community
Build ID: dd47e4b30cb7dab30588d6c79c651f218165e3c5
CPU threads: 6; OS: Windows 10.0 Build 19045; UI render: Skia/Vulkan; VCL: win
Locale: en-AU (en_AU); UI: en-GB
Calc: CL threaded
For those that may be familiar with VBA, this is the working code:
Option Compare Database
Option Explicit
Function YMD(StartDate As Date, EndDate As Date) As String
Dim TotalMonths As Integer
Dim Years As Integer
Dim Months As Integer
Dim Days As Integer
Dim DaysDiff As Integer
Dim eomStart As Boolean, eomEnd As Boolean
TotalMonths = DateDiff("m", [StartDate], [EndDate])
DaysDiff = DatePart("d", [EndDate]) - DatePart("d", [StartDate])
'find out if StartDate is End of the Month
If DatePart("m", DateAdd("d", 1, StartDate)) = DatePart("m", DateAdd("m", 1, StartDate)) Then
eomStart = True
Else
eomStart = False
End If
'find out if EndDate is End of the Month
If DatePart("m", DateAdd("d", 1, EndDate)) = DatePart("m", DateAdd("m", 1, EndDate)) Then
eomEnd = True
Else
eomEnd = False
End If
Years = Int(TotalMonths / 12)
Months = TotalMonths - (Years * 12)
Days = DateDiff("d", DateAdd("m", TotalMonths, StartDate), EndDate + 1)
If DaysDiff = -1 Then
If eomStart Xor eomEnd Then
Months = Months - 1
Days = DateDiff("d", DateAdd("m", TotalMonths - 1, StartDate), EndDate)
Else
Days = DateDiff("d", DateAdd("m", TotalMonths, StartDate), EndDate + 1)
End If
ElseIf DaysDiff < -1 Then
If eomStart And eomEnd Then
Days = DateDiff("d", DateAdd("m", TotalMonths, StartDate), EndDate)
Else
Months = Months - 1
Days = DateDiff("d", DateAdd("m", TotalMonths - 1, StartDate), EndDate + 1)
End If
End If
YMD = (Years & "Y " & Months & "M " & Days & "D")
End Function
Thanks in advance for your attention and your patience. Any assistance would be very much appreciated.