Option Compare Database
Option Explicit
Public Function sqlTipo(ByVal valor As Variant) As String
If Nz(valor, "") = "" Then
sqlTipo = "NULL"
ElseIf IsNumeric(valor) Then
sqlTipo = "NUMBER"
ElseIf IsDate(valor) Then
sqlTipo = "DATE"
Else
sqlTipo = "STRING"
End If
End Function
Public Function sqlLiteral( _
ByVal valor As Variant, _
Optional ByVal tipo As String = "AUTO" _
) As String
Const COMA = ","
Const PUNTO = "."
Const COMILLA = "'"
If Nz(valor, "") = "" Then
tipo = "NULL"
ElseIf tipo = "AUTO" Then
tipo = sqlTipo(valor)
End If
Select Case tipo
Case "NULL":
sqlLiteral = ""
Case "NUMBER":
sqlLiteral = Replace(Nz(valor, 0), COMA, PUNTO)
Case "DATE":
sqlLiteral = Format(CDate(valor), "\#mm/dd/yyyy\#")
Case "STRING":
sqlLiteral = COMILLA & Replace(valor, COMILLA, COMILLA & COMILLA) & COMILLA
End Select
End Function
Public Function sqlParametrizar(ByVal sql As String, ParamArray parametros()) As String
Dim elemento As Variant
Dim parametro As String
For Each elemento In parametros
parametro = ObtenerPrimerParametro(sql)
If parametro = "" Then
Exit For
Else
sql = Replace(sql, parametro, elemento)
End If
Next
sqlParametrizar = sql
End Function
Public Function sqlParametrizarLiterales(ByVal sql As String, ParamArray parametros()) As String
Dim elemento As Variant
Dim parametro As String
For Each elemento In parametros
parametro = ObtenerPrimerParametro(sql)
If parametro = "" Then
Exit For
Else
sql = Replace(sql, parametro, sqlLiteral(elemento))
End If
Next
sqlParametrizarLiterales = sql
End Function
Private Function ObtenerPrimerParametro(ByVal sql As String) As String
Dim pos1 As Integer: pos1 = InStr(sql, "«")
Dim pos2 As Integer: pos2 = InStr(sql, "»")
If pos1 > 0 Or pos2 > 0 Then ObtenerPrimerParametro = Mid(sql, pos1, pos2 - pos1 + 1)
End Function
Public Function sqlEjecutar(ByVal sql As String) As Boolean
On Error GoTo Errores
CurrentDb.Execute sql, dbFailOnError
sqlEjecutar = True
Salida:
Exit Function
Errores:
MsgBox Err.Description, vbCritical, "Error"
Resume Salida
End Function
Public Function sqlEditarInteractivo(ByVal sql As String, ByVal id As Long, ByVal valor_actual As String) As Boolean
Dim valor As String
valor = entrarValor(valor_actual)
If valor = "" Then Exit Function
sql = sqlParametrizar(sql, sqlLiteral(valor, "STRING"), id)
sqlEditarInteractivo = sqlEjecutar(sql)
End Function
Public Function sqlBorrarInteractivo(ByVal sql As String, ByVal id As Long, ByVal valor_actual As String) As Boolean
If confirmarBorrado(valor_actual) Then
sqlBorrarInteractivo = sqlBorrar(sql, id)
End If
End Function
Public Function sqlBorrar(ByVal sql As String, ByVal id As Long) As Boolean
sql = sqlParametrizar(sql, id)
sqlBorrar = sqlEjecutar(sql)
End Function
Public Function sqlAgregarInteractivo(ByVal sql As String, ParamArray extra()) As Boolean
Dim valor As String
Dim aux As Variant
valor = entrarValor()
If valor = "" Then Exit Function
sql = sqlParametrizar(sql, sqlLiteral(valor, "STRING"))
For Each aux In extra
sql = sqlParametrizarLiterales(sql, aux)
Next
sqlAgregarInteractivo = sqlEjecutar(sql)
End Function
Public Function entrarValor(Optional ByVal omision As String = "") As String
entrarValor = Replace(Trim(Nz(InputBox("Introduce el valor:", Default:=omision), "")), "'", "''")
End Function
Public Function esId(ByVal id As Variant) As Boolean
If IsNumeric(id) Then
esId = id <> 0
End If
End Function
Public Function confirmarBorrado(ByVal valor As String) As Boolean
confirmarBorrado = vbYes = MsgBox("¿Borrar «" & valor & "» ?", vbQuestion + vbYesNo + vbDefaultButton2)
End Function