แปลงตัวเลขเป็นตัวอักษร ใน MS ACCESS
Wednesday, February 3rd, 2010เรามาลอง แปลงตัวเลขเป็นตัวอักษร ใน MS ACCESS กันคับ
Function <span style="color: #ff0000;">sBahtText</span>(ByVal sNum)
Dim sNumber, sDigit, sDigit10
Dim nLen, sWord, sWord2
Dim sByte, I, J
sNumber = Array("", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า")
sDigit = Array("", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน")
sDigit10 = Array("", "สิบ", "ยี่สิบ", "สามสิบ", "สี่สิบ", "ห้าสิบ", "หกสิบ", "เจ็ดสิบ", "แปดสิบ", "เก้าสิบ")
sNum = Replace(FormatNumber(sNum, 2), ",", "")
nLen = Len(sNum)
If sNum = ".00" Then sBahtText = "ศูนย์"
For I = 1 To nLen - 3
J = (15 + nLen - I) Mod 6
sByte = Mid(sNum, I, 1)
If sByte <> "0" Then
If J = 1 Then sWord = sDigit10(sByte) Else sWord = sNumber(sByte) & sDigit(J)
sBahtText = sBahtText & sWord
End If
If J = 0 And I <> nLen - 3 Then sBahtText = sBahtText & "ล้าน": sBahtText = Replace(sBahtText, "หนึ่งล้าน", "เอ็ดล้าน")
Next
If Left(sNum, 1) = "1" Then sBahtText = Replace(sBahtText, "เอ็ดล้าน", "หนึ่งล้าน")
If Left(sNum, 2) = "11" Then sBahtText = Replace(sBahtText, "สิบหนึ่งล้าน", "สิบเอ็ดล้าน")
If Len(sBahtText) > 0 Then sBahtText = sBahtText & "บาท"
If nLen > 4 Then sBahtText = Replace(sBahtText, "หนึ่งบาท", "เอ็ดบาท")
sNum = Right(sNum, 2)
If sNum = "00" Then
sBahtText = sBahtText & "ถ้วน"
Else
If Left(sNum, 1) <> "0" Then sBahtText = sBahtText & sDigit10(Left(sNum, 1))
If Right(sNum, 1) <> "0" Then sBahtText = sBahtText & sNumber(Right(sNum, 1))
sBahtText = sBahtText & "สตางค์"
If Left(sNum, 1) <> "0" Then sBahtText = Replace(sBahtText, "หนึ่งสตางค์", "เอ็ดสตางค์")
End If
End Function
เวลานำไปใช้ในแบบฟอร์มรายงานนะคับ

พิมพ์ค่า =sBahtText(ตัวแปร,ออบเจ็กค์,ที่ที่ต้องการแปลง) ลงในช่อง แหล่งควบคุม
ผลที่ได้ในรายงานคับ

สามารถนำไปประยุกต์ใช้กับการพิมพ์ใบเสร็จได้เป็นอย่างดีเลยคับ