如何在Access报告中将日期显示为文本

时间:2018-12-27 04:42:41

标签: ms-access

我正在尝试让Access为我创建证书。除日期外,我几乎所有事情都解决了。我希望日期显示为文本。

我可以使用Excel中的模块来完成此操作,但是当我将其作为函数输入到Access中时,它会给我一个#Name吗?错误。这是我在函数中所拥有的(同样,这在Excel中有效):

Function DateToWords(ByVal xRgVal As Date) As String
   Dim xYear As String
   Dim Hundreds As String
   Dim Decades As String
   Dim xTensArr As Variant
   Dim xOrdArr As Variant
   Dim xCardArr As Variant
   xOrdArr = Array("1st", "2nd", "3rd", "4th", "5th", "6th", _
               "7th", "8th", "9th", "10th", "11th", "12th", _
               "13th", "14th", "15th", "16th", "17th", "18th", _
               "19th", "20th", "21st", "22nd", _
               "23rd", "24th", "25th", "26th", _
               "27th", "28th", "29th", "30th", "31st")
   xCardArr = Array("", "one", "two", "three", "tour", _
               "five", "six", "seven", "eight", "nine", _
               "ten", "eleven", "twelve", "thirteen", _
               "fourteen", "fifteen", "sixteen", _
               "seventeen", "eighteen", "nineteen")
   xTensArr = Array("twenty", "thirty", "forty", "fifty", _
           "sixty", "seventy", "eighty", "ninety")
   xYear = CStr(Year(xRgVal))
   Decades = Mid$(xYear, 3)
   If CInt(Decades) < 20 Then
    Decades = xCardArr(CInt(Decades))
   ElseIf CInt(Decades) Like "*0" Then
    Decades = xTensArr(CInt(Left$(Decades, 1)) - 2)
   Else
    Decades = xTensArr(CInt(Left$(Decades, 1)) - 2) & "-" & _
            xCardArr(CInt(Right$(Decades, 1)))
   End If
    Hundreds = Mid$(xYear, 2, 1)
   If CInt(Hundreds) Then
    Hundreds = xCardArr(CInt(Hundreds)) & " hundred "
   Else
    Hundreds = ""
   End If
   DateToWords = "This " & xOrdArr(Day(xRgVal) - 1) & " day of" & _
              Format$(xRgVal, " mmmm, ") & _
              xCardArr(CInt(Left$(xYear, 1))) & _
              " thousand " & Hundreds & Decades
End Function

Access中调用该函数的代码。这是在报告的文本框中:

=DateToWords([Forms]![FrmMakeCert]![Date])

n Excel,它采用我在单元格中指定的日期(例如12/27/18)并将其转换为:

“ 12月27日,即2000年1月18日”

这就是我要在Access报告中完成的,要从表单中提取日期。

2 个答案:

答案 0 :(得分:0)

使用步骤。

  • 首先在Access中创建一个模块,然后粘贴您的代码。
  • 假设在报告中您要在文本框名称txtDateText中显示文本的日期
  • txtDateText数据源中输入以下内容

    =DateToWords([Forms]![Form1]![txtDate])

记住: Form1必须打开并包含有效日期,否则它将显示错误。 Form1是表单名称,因此您的情况将有所不同。还有文本框名称。

File download link

enter image description here

答案 1 :(得分:0)

问题是由用户定义的函数和模块使用相同的名称引起的,即DateToWords。 您应该使用其他名称。 将模块重命名为例如mdlDateToWords。