Visual Basic Foro
Programación => Visual Basic 6 => Mensaje iniciado por: parauno en Octubre 20, 2021, 07:48:19 am
-
Que tal colegas?
Tengo la siguiente porción de código, que deberia de obtener sin generar error alguno el max(id) de la tabla que se pasa por argumento, a modo de interactuar con la comunidad ¿que mejoras implementarian? Desde ya, gracias!
Public Function getMaxId(id_tmp As String, tbl_tmp As String, vacio As Boolean, max As Double)
' esta funcion comprueba que la tabla que se pasa por argumento NO este vacia
' en caso que SI este vacia, maneja el valor NULL para evitar error al consultar el MAX(id)
' devuelve un incremento del MAX(id) obtenido
Dim sSQL As String
Dim registros As Variant
Dim id As Double
Dim rsPrueba As Recordset
Set rsPrueba = New Recordset
sSQL = "SELECT COUNT(*) AS total FROM " & tbl_tmp & ""
EjecutarOpen rsPrueba, sSQL
registros = rsPrueba!total
If registros = 0 Then
id = registros
vacio = True
Else
Set rsPrueba = Nothing
Set rsPrueba = New Recordset
EjecutarOpen rsPrueba, "SELECT MAX(" & id_tmp & ") AS max_id FROM " & tbl_tmp & ""
If IsNull(rsPrueba!max_id) Then
vacio = True
registros = "Null"
Else
vacio = False
id = rsPrueba!max_id
End If
End If
Set rsPrueba = Nothing
'max = id + 1
max = val(id) + 1
getMaxId = max
End Function
-
¿Pq te generaría error?, te estas complicando con ese codigo.
Si haces un max(tucampo) as Maximo y no hay registros la respuesta sera un recordset con null, no un error
solo tendrías que hacer un Maximo= IIf(IsNull(rs!Maximo), 0, rs!Maximo) desde vb6 y ya
¿Que buscas hacer? un campo autogenerado