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