大神请教一个问题?ACCESS 中,一个文本框输入数字金额,另一个文本框显示大写金额?怎么实现?
1)为ACCESS数据库新建一个“模块1”(已经有了可以免掉此步),在该模块下编写下列自定义公有大写转换函数
Public Function SumInWordsRMB _
(SumInFigures As Variant, Optional WithoutHeadRMB As Boolean) As String
Dim a As Double, b As Byte
Dim Str As String, Strg As String
Dim strInteger As String
Dim strInt As String
Dim strDecimal
Dim strDecml
Dim Char As String
Dim DigtWord As String
Dim Pos As Byte
Dim i As Byte, Lns As Byte, ii
Dim canAddZero As Boolean
Dim AddZero As Boolean
Dim UseFullDigtWord As Boolean
If Not IsNumeric(SumInFigures) Then
SumInWordsRMB = ""
Exit Function
Else
a = CDbl(SumInFigures)
If a < 0 Or a > 999999999999999# Then
SumInWordsRMB = ""
Exit Function
End If
End If
Pos = 0
Strg = CStr(SumInFigures)
For ii = 1 To Len(Strg)
Char = Mid(Strg, ii, 1)
If Char = "." Then
Pos = ii
If IsMissing(Str) Then Str = "0"
Exit For
End If
If Char = "0" Then
If AddZero = True Then Str = Str & Char
Else
Str = Str & Char
AddZero = True
End If
Next ii
If Pos > 0 And Mid(Strg, Pos + 1, 3) <> "" Then
a = 0
b = 0
If Val(Mid(Strg, Pos + 3, 1)) >= 5 Then b = 1
b = b + Val(Mid(Strg, Pos + 2, 1))
b = 100 + Val(Mid(Strg, Pos + 1, 1)) * 10 + b
If b >= 200 Then
a = Str
a = a + 1
Str = a
End If
If b <> 0 Then
If Val(Right(b, 1)) > 0 Then
Str = Str & "." & Mid(b, 2)
Else
If Val(Mid(b, 2, 1)) <> 0 Then Str = Str & "." & Mid(b, 2, 1)
End If
End If
End If
AddZero = False
If Str = "0" Or Str = "" Then
If WithoutHeadRMB Then
SumInWordsRMB = "零元整"
Else
SumInWordsRMB = "人民币零元整"
End If
Exit Function
End If
Strg = ""
For i = 1 To Len(Str)
Select Case Mid(Str, i, 1)
Case "0"
Strg = Strg & "零"
Case "1"
Strg = Strg & "壹"
Case "2"
Strg = Strg & "贰"
Case "3"
Strg = Strg & "叁"
Case "4"
Strg = Strg & "肆"
Case "5"
Strg = Strg & "伍"
Case "6"
Strg = Strg & "陆"
Case "7"
Strg = Strg & "柒"
Case "8"
Strg = Strg & "捌"
Case "9"
Strg = Strg & "玖"
Case "."
Strg = Strg & "."
End Select
Next i
Pos = 0
Pos = InStr(1, Strg, ".", vbTextCompare)
If Pos > 0 Then
strInteger = Left(Strg, Pos - 1)
strDecimal = Mid(Strg, Pos + 1)
Else
strInteger = Strg
End If
If strInteger = "零" Then
strInt = ""
Else
strInteger = StrReverse(strInteger)
Lns = Len(strInteger)
For i = 1 To Lns
Select Case i
Case 1
DigtWord = "元"
Case 2
DigtWord = "拾"
Case 3
DigtWord = "佰"
Case 4
DigtWord = "仟"
Case 5
DigtWord = "万"
Case 6
DigtWord = "拾万"
Case 7
DigtWord = "佰万"
Case 8
DigtWord = "仟万"
Case 9
DigtWord = "亿"
Case 10
DigtWord = "拾亿"
Case 11
DigtWord = "佰亿"
Case 12
DigtWord = "仟亿"
Case 13
DigtWord = "万亿"
Case 14
DigtWord = "拾万亿"
Case 15
DigtWord = "佰万亿"
End Select
Char = Mid(strInteger, i, 1)
If Char = "零" Then
If i = 1 Then strInt = "元"
UseFullDigtWord = True
If canAddZero = True Then AddZero = True
Else
If UseFullDigtWord = True Then
If AddZero = False Then
strInt = Char & DigtWord & strInt
Else
If i >= 6 And i <= 8 Then
If Mid(strInteger, 5, 1) <> "零" Then
strInt = Char & Left(DigtWord, 1) & "零" & strInt
Else
strInt = Char & DigtWord & "零" & strInt
End If
ElseIf i >= 10 And i <= 12 Then
If Mid(strInteger, 9, 1) <> "零" Then
strInt = Char & Left(DigtWord, 1) & "零" & strInt
Else
strInt = Char & DigtWord & "零" & strInt
End If
ElseIf i >= 14 And i <= 15 Then
If Mid(strInteger, 13, 1) <> "零" Then
strInt = Char & Left(DigtWord, 1) & "零" & strInt
Else
strInt = Char & DigtWord & "零" & strInt
End If
Else
strInt = Char & DigtWord & "零" & strInt
End If
End If
Else
If AddZero = False Then
strInt = Char & Left(DigtWord, 1) & strInt
Else
strInt = Char & Left(DigtWord, 1) & "零" & strInt
End If
End If
canAddZero = True
UseFullDigtWord = False
AddZero = False
End If
Next i
End If
If Pos = 0 Then strDecml = "整"
If Pos > 0 Then
If Len(strDecimal) = 1 Then strDecml = strDecimal & "角整"
If Len(strDecimal) = 2 Then
If Left(strDecimal, 1) = "零" Then
If strInt = "" Then
strDecml = Right(strDecimal, 1) & "分"
Else
strDecml = "零" & Right(strDecimal, 1) & "分"
End If
Else
strDecml = Left(strDecimal, 1) & "角"
strDecml = strDecml & Right(strDecimal, 1) & "分"
End If
End If
End If
If WithoutHeadRMB = False Then
strInt = "人民币" & strInt
End If
SumInWordsRMB = strInt & strDecml
End Function
2)在ACCESS窗体里使用SumInWordsRMB函数。每当在文本框TEXT1输入小写金额并回车或跳到其它控件时,文本框TEXT2会显示相应的人民币大写金额。请于窗体设计窗口,文本框TEXT2内,填入公式"=SumInWordsRMB([TEXT1])" 即可实现。
有关代码中自定义函数的说明
SumInWordsRMB(SumInFigures, WithoutHeadRMB ) 返回一个字符串类型数据,此函数可将函数右边括号里的阿拉伯数字小写金额转换为符合规范的汉字大写金额。
参数:SumInFigures
指定需要转换的阿拉伯数字小写金额。为了强化函数的易用性,数据类型设定为Variant变体变量,有关转换算法因此变得相对复杂。用户可以为该参数指定一个任何类型的数据包括空值(Null),但是只有数字才会被转换成人民币大写金额。可被转换的数字范围由最小的0至最大的999999999999999(达百万亿级别,已经超出了货币数据类型的最大值),如果参数中含有小于“分”的数值,此函数会对其四舍五入保留到“分”。对于不能予以转换的参数(包括负数)此函数一律返回零长度字符串 "",这里强调一下,这时函数返回的是“零长度字符串”而非空值(NULL)。
如果需要将负数转换为大写可以先对参数取绝对值然后再予以转换。
如果不想函数返回值因四舍五入功能而导致大小写金额不能精确对应(例如开具支票时),则须确保小写金额不多于两位小数。
当对转换速度有特别高的要求时(例如一次需要转换成千上万个数字),我们可以将参数的数据类型设置为数字,并减小可被转换的数字范围,跟着便可以使用较为简单的转换算法从而提高转换速度。有兴趣的朋友不妨自己试一下,但是这样一来函数的用户友好度会变差,某些情况下使用起来可能会比较麻烦。
参数:WithoutHeadRMB
指定是否在大写金额前面添加“人民币”字头。函数默认为添加“人民币”字头,如果不需要添加字头,请指定该参数为“真”(True)。
例如SumInWordsRMB(100,True) 返回 “壹佰元整”;SumInWordsRMB(100)或 SumInWordsRMB(100,False) 返回 “人民币壹佰元整”。