I'm not very familiar with ADOX, so I can't point to your
error, but I may be able make a suggestion to save
further headaches in the future. In my experience,
autonumber fields tend to be, well, a royal pain.
Especially if you need to migrate the data from Access to
SQL, or you need to support multiple platforms.
At my place of business we generate key values randomly
and use a long integer field for the PK. We have a
general function to do this:
Public Function GenerateRandomIDNumber(pDBConnection As
ADODB.Connection, pTableName As String, pFieldName As
String, Optional pKeepDBConnOpen As Boolean = False) As
Long
Dim lngRandomID As Double, bolWasClosed As Boolean
Dim rstRandomTable As ADODB.Recordset
On Error GoTo GenerateRandomIDNumber_Err
if pDBConnection.state = adStateClosed then
pDBConnection.open
end if
TryAgain:
Set rstRandomTable = New ADODB.Recordset
rstRandomTable.CursorLocation = adUseClient
Randomize
lngRandomID = Format(((999999999#) * Rnd) + 1, 0)
'This checks to make sure the random ID doesn't
already exist
rstRandomTable.Open "SELECT " & pFieldName & " FROM "
& pTableName & " WHERE " & pFieldName & "=" &
lngRandomID, pDBConnection, adOpenDynamic,
adLockOptimistic, adCmdText
If rstRandomTable.EOF = False And rstRandomTable.bof
= False Then
'already exists try again
rstRandomTable.Close
Set rstRandomTable = Nothing
GoTo TryAgain
End If
rstRandomTable.Close
Set rstRandomTable = Nothing
GenerateRandomIDNumber = dblRandomID
If pKeepDBConnOpen = False Then
pDBConnection.Close
End If
Exit Function
GenerateRandomIDNumber_Err:
GenerateRandomIDNumber = 0
End Function
HTH
>hi,
>I am using Access 2000
>Filed Name: autono (It's an AutoNumber field)
>I want to assign an autonumber in this field. is it
possible ?
>I was try to ADOX, but i have received a error message:
>Run-time error '3265':
>Item connot be found in the collection corresponding to
the requested name
>or ordinal.
> Dim cat As New ADOX.Catalog
> Dim col As ADOX.Column
> Dim ctno As Long
> Set col = New ADOX.Column
> Set col.ParentCatalog = cat
> ctno = 5 ' user define value
> cat.ActiveConnection = cn
> Set col = cat.Tables("TableName").Columns("autono")
> With col
> .Properties("Seed").Value = ctno ' Assign the
autonumber filed
> End With
> Set cat = Nothing: Set col = Nothing
>Please let me know, How it can be done.
>Thanks in Advance!
>Zahid Qamar
>http://www.zahidq.cjb.net
>.