ABCdatos
Programas y tutoriales
que hablan tu idioma
ABCdatos tu página de inicio Inicio      ¡Enlázanos! ¡Enlázanos!
PATROCINADOR
¡Descarga desde cualquier parte del mundo los programas más buscados!
¡Descarga los programas más buscados!
¡Descargar ahora!

Configura la auto numeración

     
Fuente: Microsoft Corporation
Web: http://www.microsoft.com/
     
  • Descripción
  • Recomendar

¿Cómo forzar en Access 2000 a que la auto numeración comience en un valor específico?

El truco es importar un registro que tenga un número menos que el número en que deseamos inicie la auto numeración, y luego borrarlo.

El siguiente procedimiento Sub realiza esta operación. Por ejemplo, para forzar la tabla "tblClient" a empezar en la numeración desde 7500: Call Set AutoNumber("tbClient",7500).

Sub SetAutoNumber(sTable As String, ByVal lNum As Long)
On Error GoTo Err_SetAutoNumber
' Purpose: set the AutoNumber field in sTable to begin at lNum.
' Arguments: sTable = name of table to modify.
' lNum = the number you wish to begin from.
' Sample use: Call SetAutoNumber("tblInvoice", 1000)
Dim db As Database ' Current db.
Dim tdf As TableDef ' TableDef of sTable.
Dim i As Integer ' Loop counter Dim fld As Field ' Field of sTable.
Dim sFieldName As String ' Name of the AutoNumber field.
Dim vMaxID As Variant ' Current Maximum AutoNumber value.
Dim sSQL As String ' Append/Delete query string.
Dim sMsg As String ' MsgBox string.
lNum = lNum - 1 ' Assign to 1 less than desired value.
' Locate the auto-incrementing field for this table.
Set db = CurrentDb()
Set tdf = db.TableDefs(sTable)
For i = 0 To tdf.Fields.Count - 1
Set fld = tdf.Fields(i)
If fld.Attributes And dbAutoIncrField Then
sFieldName = fld.name
vExit For
End If
Next
If Len(sFieldName) = 0 Then
sMsg = "No AutoNumber field found in table """ & sTable & """."
MsgBox sMsg, vbInformation, "Cannot set AutoNumber"
Else
vMaxID = DMax(sFieldName, sTable)
If IsNull(vMaxID) Then vMaxID = 0
If vMaxID>= lNum Then
sMsg = "Supply a larger number. """ & sTable & "." & sFieldName & """ already
contains the value " & vMaxID
MsgBox sMsg, vbInformation, "Too low."
Else
' Insert and delete the record.
sSQL = "INSERT INTO " & sTable & " ([" & sFieldName & "]) SELECT " & lNum & " AS lNum;"
db.Execute sSQL, dbFailOnError
sSQL = "DELETE FROM " & sTable & " WHERE " & sFieldName & " = " & lNum & ";" db.Execute sSQL, dbFailOnError
End If
End If
Exit_SetAutoNumber:
Exit Sub
Err_SetAutoNumber:
MsgBox "Error " & Err.Number & ": " & Err.Description, , "SetAutoNumber()"
Resume Exit_SetAutoNumber
End Sub

¿Te gusta este truco? ¡Recomiéndaselo a un amigo!

     
     
     
     
     
     
Las direcciones de correo electrónico que se proporcionan en este servicio, solamente serán utilizadas con la finalidad de enviar la recomendación al destinatario. Ni tu dirección de correo ni la de tu amigo/a serán utilizadas para ningún otro propósito.

Copyright © 1999-2008 ABCdatos.com. Todos los derechos reservados.
Para información sobre privacidad, consulte la declaración de política de privacidad.
Contactar con ABCdatos.