Wouldn't you know it, I solved my problem shortly after asking for help.
Here is my solution for setting a default value from a database date column into an unbound date control. I'm sure there is a better way than using oCol.String
, and I'd be glad to hear a better way.
Sub Form_WhenLoading
' Default control dteDateFrom to SystemParam.DfltRptFrDate
' and control dteDateTo to SystemParam.DfltRptToDate
dim oForm as object
dim oControl as object
dim oResultSet as object
dim oContext as object
dim oDB as object
dim oStmt as object
dim sSQL as string
dim oCol as object ' Column of oResultSet
Dim aDate As New com.sun.star.util.Date
oForm = ThisComponent.DrawPage.Forms.getByIndex(0)
oController = ThisComponent.CurrentController
oContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
oDb = oContext.getByName("DatabaseName")
oConn = oDb.getConnection("","")
sSQL = "SELECT ""DfltRptFrDate"", ""DfltRptToDate"" from ""SystemParam"" WHERE ""SystemKey"" = '0';"
oStmt = oConn.createStatement()
oResultSet = oStmt.executeQuery(sSQL)
If Not IsNull(oResultSet) Then
oResultSet.next
End If
' dteDateFrom
oCol = oResultSet.Columns.getByName("DfltRptFrDate")
oControl = oForm.GetByName("dteDateFrom")
' OCol.String is YYYY-MM-DD
aDate.year = left(oCol.String, 4)
aDate.month = mid(oCol.String, 6,2)
aDate.day = right(oCol.String, 2)
If IsEmpty(oControl.Date) Then
oControl.Date = aDate
oControl.text = oCol.String
oControl.Commit()
End If
' dteDateTo
oCol = oResultSet.Columns.getByName("DfltRptToDate")
oControl = oForm.GetByName("dteDateTo")
aDate.year = left(oCol.String, 4)
aDate.month = mid(oCol.String, 6,2)
aDate.day = right(oCol.String, 2)
If IsEmpty(oControl.Date) Then
oControl.Date = aDate
oControl.text = oCol.String
oControl.Commit()
End If
End sub