Condividi tramite

Access VBA: riconoscere campo a numerazione automatica

Anonimo
2019-04-06T13:03:58+00:00

Buongiorno,

in un programma Access vorrei costruire una istruzione "Insert into" in modo automatico, recuperando i campi dalla tabella (rendendo quindi il programma indipendente da eventuali variazioni dei nomi dei campi). Fin qui tutto bene. Il problema però sorge se nella tabella c'è un campo ID a numerazione automatica, nel qual caso - ovviamente - non posso assegnare io il valore. 

Come faccio a riconoscerlo? Ho visto i "Fields Attributes" ma mi dà come ritorno  49 e non dbAutoIncrField (16).

Grazie

Ivo

(https://docs.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/field-attributes-property-dao)

Microsoft 365 e Office | Accesso | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

Risposta accettata dall'autore della domanda

  1. Anonimo
    2019-04-06T14:32:30+00:00

    Ciao Ivo

    prova a vedere la risposta di Hans in questo post: Identify autonumber field in vba

    David

    La risposta è stata utile?

    2 persone hanno trovato utile questa risposta.
    0 commenti Nessun commento

2 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2019-04-06T19:19:11+00:00

    ciao Ivo,

    hai già la risposta di David, se vuoi prova ad effettuare uin test con ADOX, visto che espone la property autoincrement.

    Puoi testare direttamente nella finestra immediata con :

    ? fp2("nomeTabella")

    sostituisci nometabella con il nome di una tabella effettiva ed ottieni un msgbox con il nome del campo autonumber se esiste oppure un avviso che non c'è.

    Ovviamente il msgbox può essere eliminato in un ambiente di produzione in modo da ottenere solo il nome del campo autonumber dalla funzione esposta.

    Ricordati tra i riferimenti di spuntare Microsoft ADO ext XX.XX for DDL and Security.

    HTH.

    Ciao, Sandro.

    Public Function fp2(ByVal strTable As String) As String

    On Error GoTo errHandler

    Dim cat  As ADOX.Catalog

    Dim tbf  As ADOX.Table

    Dim fld  As ADOX.Column

    Dim strF As String

    Dim strM As String

    Set cat = New ADOX.Catalog

    Set tbf = New ADOX.Table

    Set fld = New ADOX.Column

    cat.ActiveConnection = CurrentProject.Connection

    Set tbf = cat.Tables(strTable)

    For Each fld In tbf.Columns

        If fld.Properties("autoincrement") Then

            strF = fld.Name

            Exit For

        End If

    Next

    strM = "Nessun contatore individuato"

    If Len(strF) > 0 Then

        strM = "Contatore individuato:" & strF

    End If

    VBA.MsgBox prompt:=strM, _

               buttons:=vbInformation + vbOKOnly, _

               Title:="Avviso"

    exit_here:

        Set cat = Nothing

        Set tbf = Nothing

        Set tbf = Nothing

        Exit Function

    errHandler:

        With Err

                    VBA.MsgBox "ERR#" & .Number _

                        & vbNewLine & .Description _

                    , vbOKOnly Or vbCritical

        End With

        Resume exit_here

    End Function

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2019-04-06T18:20:32+00:00

    Grazie, David.

    Certo che le "If bitwise" finora non le conoscevo! Fare un test più semplice, no?

    Sono usate da qualche altra parte?

    Buon fine settimana

    Ivo

    La risposta è stata utile?

    0 commenti Nessun commento