Debes tener una referencia a:
• Microsoft ActiveX Data Objects 2.0 Library
REG: es el nombre del recordset
NUM_MAXIMO: es el nombre del campo temporal (o alias) que va a tener el valor máximo del campo consultado con la instrucción SQL
CONEXIÓN: es la conexión a la base de datos
COD: es el nombre del campo de la base de datos que estamos consultando
Te dejo un ejemplo conectándome a la base de datos de Neptuno en Access:
Código vb:
Ver originalDim REG As ADODB.Recordset
Dim CONEXION As ADODB.Connection
Set REG = New ADODB.Recordset
Set CONEXION = New ADODB.Connection
'ESTABLECER LA CONEXION CON LA BASE DE DATOS
CONEXION.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Neptuno.mdb;" _
& "Persist Security Info=False"
CONEXION.Open
'BUSCAR EL CODIGO MAXIMO
REG.Open "Select Max(Cod) as NUM_MAXIMO from TABLA", CONEXION, adOpenStatic, adLockOptimistic
If REG.EOF = False Then
'HAY DATOS
txtNUMERO.Text = Format(REG!NUM_MAXIMO + 1, "000")
Else
'NO HAY DATOS
txtNUMERO.Text = Empty
End If
'CERRAMOS LA CONEXION Y LIBERAMOS LOS RECURSOS
REG.Close
CONEXION.Close
Set REG = Nothing
Set CONEXION = Nothing
Espero haberte despejado las dudas