Convertir Números a letras en un Informe

Tengo un campo numérico en una Consulta con BD. Deseo que en mi reporte se vea en letras el valor del número, incluido el valor monetario

1 Respuesta

Respuesta
1
Bien pues ahí te va un código que copié... espero que te sirva.
Código a pegar en un modulo:
Public Function NumerosConComaALetras(ByVal NumberStr As String) As String
Dim nTemp
nTemp = InStr(NumberStr, ",")
If nTemp = 0 Then
NumerosConComaALetras = NumerosALetras(NumberStr, False)
Else
NumerosConComaALetras = NumerosALetras(Left(NumberStr, nTemp - 1), False) & " coma " & _
NumerosALetras(Mid(NumberStr, nTemp + 1), False)
End If
End Function
Public Function NumerosEurosALetras(ByVal NumberStr As String, Optional lFemenino = False, Optional cMoneda = "euro", Optional cCentimos = "céntimo") As String
Dim nTemp, nDecimales
nTemp = InStr(NumberStr, ",")
If nTemp = 0 Then
NumerosEurosALetras = NumerosALetras(NumberStr, lFemenino, cMoneda)
Else
nDecimales = Len(Mid(NumberStr, nTemp + 1))
Select Case nDecimales
Case 0
NumerosEurosALetras = NumerosALetras(NumberStr, lFemenino, cMoneda)
Case 1, 2 'centimos'
If nDecimales = 1 Then
NumberStr = NumberStr & "0"
End If
NumerosEurosALetras = NumerosALetras(Left(NumberStr, nTemp - 1), lFemenino, cMoneda) & " con" & _
NumerosALetras(Mid(NumberStr, nTemp + 1), lFemenino, cCentimos)
Case Else
NumerosEurosALetras = NumerosALetras(Left(NumberStr, nTemp - 1), False) & " coma " & _
NumerosALetras(Mid(NumberStr, nTemp + 1), False) & " " & cMoneda & _
IIf(InStr("aeio", Right(cMoneda, 1)) = 0, "e", "") & "s"
End Select
End If
End Function
Public Function NumerosALetras(ByVal NumberStr As String, Optional lFemenino = True, Optional cMoneda = "") As String
Dim z As String, x As String, Temp As String, c As String
Dim a As Integer, b As Integer, i As Integer
Dim iPos As Integer
Dim data(9, 3) As String
data(0, 0) = "uno"
data(1, 0) = "dos"
data(2, 0) = "tres"
data(3, 0) = "cuatro"
data(4, 0) = "cinco"
data(5, 0) = "seis"
data(6, 0) = "siete"
data(7, 0) = "ocho"
data(8, 0) = "nueve"
data(9, 0) = "diez"
data(0, 1) = "cien"
data(1, 1) = "diez"
data(2, 1) = "veinte"
data(3, 1) = "treinta"
data(4, 1) = "cuarenta"
data(5, 1) = "cincuenta"
data(6, 1) = "sesenta"
data(7, 1) = "setenta"
data(8, 1) = "ochenta"
data(9, 1) = "noventa"
data(0, 3) = "diez"
data(1, 3) = "once"
data(2, 3) = "doce"
data(3, 3) = "trece"
data(4, 3) = "catorce"
data(5, 3) = "quince"
data(6, 3) = "dieciséis"
data(7, 3) = "diecisiete"
data(8, 3) = "dieciocho"
data(9, 3) = "diecinueve"
'remove redundant spaces
'NumberStr = Trim( Replace(NumberStr, ",", ""))
NumberStr = Trim(NumberStr)
a = Len(NumberStr)
Temp = NumberStr
If Val(NumberStr) = 0 Then
NumerosALetras = "cero"
Exit Function
End If
'get rid of any decimals
iPos = InStr(Temp, ",")
If iPos > 0 Then Temp = Left(Temp, iPos - 1)
While ((a Mod 3) <> 0)
Temp = "0" & Temp
a = Len(Temp)
Wend
NumberStr = Temp
For i = a - 2 To 1 Step -3
b = b + 1
Temp = Mid(NumberStr, i, 3)
z = ""
' "Intelligent" routines
'------------------------
If Temp <> "000" Then
If Temp = "100" Then
z = "cien"
Else
c = Left(Temp, 1)
If c <> "0" Then
If c = "1" Then
z = "ciento"
ElseIf c = "5" Then
z = " quinientos"
ElseIf c = "7" Then
z = " setecientos"
ElseIf c = "9" Then
z = " novecientos"
Else
z = " " & data(Val(c) - 1, 0) & "cientos"
End If
End If
c = Mid(Temp, 2, 1)
If c <> "0" Then
If c <> "1" Then
z = z & " " & data(Val(c), 1)
Else
z = z & " " & data(Val(Right(Temp, 2)) - 10, 3)
End If
End If
End If
If Right(Temp, 1) <> "0" And Mid(Temp, 2, 1) <> "1" Then
z = z & IIf(z = "", "", " y ") & data(Val(Right(Temp, 1)) - 1, 0)
End If
End If
'------------------------
If z <> "" Then
Select Case b
Case 1:
x = z
Case 2:
If z = "uno" Then
x = "mil" & x
Else
x = z & " mil" & x
End If
Case 3:
If z = "uno" Then
x = "un millón " & x
Else
x = z & " millones " & x
End If
Case 4:
If z = "uno" Then
x = "mil millones " & x
Else
x = z & " mil millones " & x
End If
Case 5:
If z = "uno" Then
x = "un billón " & x
Else
x = z & " billones " & x
End If
Case Else:
Exit Function
End Select
End If
Next
'correción de genero
If x = "uno" Then
x = IIf(lFemenino, "una", "un")
ElseIf Right(x, 3) = "uno" Then
x = Mid(x, 1, Len(x) - 3) & IIf(lFemenino, "una", IIf(cMoneda = "", "uno", "un"))
ElseIf Right(x, 3) = "tos" And lFemenino Then
x = Mid(x, 1, Len(x) - 3) & "tas"
End If
If cMoneda <> "" Then
x = x & " " & IIf(Val(NumberStr) <= 1, cMoneda, cMoneda & IIf(InStr("aeio", Right(cMoneda, 1)) = 0, "e", "") & "s")
End If
NumerosALetras = x
End Function
Me sale Error
Me sale error en la ejecución en
z = " " & data(Val(c) - 1, 0) & "cientos"
La dar numerosEurosALetras(501.25, False,"peso","centavo")
usando el "." en vez de la coma
Saludos.
Miguel Paniza
Problema de la coma
Es un problema de la coma decimal.
a) Tienes dos soluciones. Poner los números entrecomillas y con "," como separador decimal:
numerosEurosALetras("501,25", False,"peso","centavo")
b) Cambiar en el programa las lineas
nTemp = InStr(NumberStr, ",")
iPos = InStr(Temp, ",")
por
nTemp = InStr(NumberStr, ".")
iPos = InStr(Temp, ".")
No olvides finalizar la pregunta..

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas