Libre Office의 양식 필드에 오늘 날짜가 기본값으로 필요합니다.

Libre Office의 양식 필드에 오늘 날짜가 기본값으로 필요합니다.

Libre Office Base의 새로운 기능입니다.

테이블의 기본값을 현재 날짜로 설정하는 방법을 찾았습니다. 이제 양식의 해당 필드에 현재 날짜를 표시해야 합니다.

"기본값" TODAY()CURRENT_DATE. 그들은 둘 다 어떤 이유로 1899년 11월 18일을 나에게 알려줍니다.

누구든지 이 작업을 수행하는 방법에 대한 아이디어가 있습니까?

답변1

이 매크로는 날짜 필드에 오늘 날짜를 기록합니다 myDateField.

Sub writeDate
    Dim today As New com.sun.star.util.Date
    today.Month = Month( Now )
    today.Day = Day( Now )
    today.Year = Year( Now )
    form = ThisComponent.DrawPage.Forms(0)  ' first form
    form.myDateField.BoundField.UpdateDate( today )
End Sub

작업을 할당하려면: 양식 탐색기 > myForm > 양식 속성 > 이벤트 > 예: 로드할 때

답변2

다음 매크로 코드를 사용하십시오. 여기서 테이블의 열(컨트롤 이름 아님)은 "MyDate"라고 합니다.

Sub DefaultDateInForm (oEvent As Object)
    oForm = oEvent.Source
    lDateCol = oForm.findColumn("MyDate")
    If oForm.getString(lDateCol) = "" Then
        dateStamp = Format(Now, "YYYY-MM-DD")
        oForm.updateString(lDateCol, dateStamp)
    End If
End Sub

양식을 편집하고 양식 속성에서 "레코드 변경 후" 이벤트에 매크로를 할당합니다.

양식 속성

이제 새 레코드가 시작될 때와 같이 레코드의 날짜가 비어 있을 때마다 날짜 필드는 기본적으로 현재 날짜로 설정되어야 합니다.

오픈오피스 포럼에는 이 주제에 관한 여러 토론이 있습니다:

답변3

에서짐 K의 답변:

매크로가 없으면 현재 날짜를 테이블의 기본값으로 정의할 수 있습니다. 날짜가 누락된 새 레코드를 저장할 때 삽입됩니다.

  • 메뉴:도구 -> SQL...
ALTER TABLE "tbl" ALTER COLUMN "col" DATE DEFAULT CURRENT_DATE

"tbl"과 "col"을 테이블과 열의 실제 이름으로 바꿉니다. [실행하다]

확인하고 완벽하게 작동합니다.

여기에 이미지 설명을 입력하세요

답변4

혹시 모르고 도움을 요청한 후 바로 문제를 해결했습니다.

다음은 데이터베이스 날짜 열의 기본값을 바인딩되지 않은 날짜 컨트롤로 설정하는 솔루션입니다. 를 사용하는 것보다 더 좋은 방법이 있을 것이라고 확신하며 oCol.String더 좋은 방법을 듣게 되어 기쁩니다.

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

관련 정보