Setting the field attribute 
Author Message
 Setting the field attribute

Hi Norm,
really thanks for replying.. thought nobody was reading my scream for help.

My problem is not to make a table and then make a field in it , but setting
the attribute of a field.
When i make a field in a table i can choose if it is a
"nummeric/text/memo/etc".  but not "autonum" i discovered that it is the
attribute "17" that makes it "autonum".
I included the code so you can see what i am doing.
Everything works (perhaps not the example, i ripped it out of my code and
rewrote it to make it more readable)

The code makes a Table in another DB, "New Table"
Then it creates a field in the table, "New AutoNumField"
In the For ... Next loop it "looks" for the table "New Table" and then it
should set the attribute of all fields to 17
BUT IT DOES NOT...
The strange thing is that i can change the name of all the fields, i can
READ the attribute, but cannot CHANGE the attribute... please help.

Dim dbsNew As database, tdf As TableDef, fld,fld2 As Field
Set dbsNew = OpenDatabase("c:\newdb.mdb")

Set tdf = dbsNieuw.CreateTableDef("New Table")
Set fld = tdf.CreateField("New AutoNumField" , 4, 10)
tdf.Fields.Append fld
tdf.Fields.Refresh
dbsNieuw.TableDefs.Append tdf
dbsNieuw.TableDefs.Refresh

Set tdf = nothing

For Each tdf In dbsNew.TableDefs
            If tdf.Attributes = 0 And tdf.Name = "New Table" Then
                For Each fld In tdf.Fields
                    fld.Attributes = 17
                Next fld
            End If
Next tdf

I can't make it work

Rinke



Sat, 20 Oct 2001 03:00:00 GMT  
 Setting the field attribute
Hi Rinke,

Um.. I thought you could only have on Autonumber per table...  Why are you
attempting to make all fields autonumber?

    -- Dev


: Hi Norm,
: really thanks for replying.. thought nobody was reading my scream for
help.
:
: My problem is not to make a table and then make a field in it , but
setting
: the attribute of a field.
: When i make a field in a table i can choose if it is a
: "nummeric/text/memo/etc".  but not "autonum" i discovered that it is the
: attribute "17" that makes it "autonum".
: I included the code so you can see what i am doing.
: Everything works (perhaps not the example, i ripped it out of my code and
: rewrote it to make it more readable)
:
: The code makes a Table in another DB, "New Table"
: Then it creates a field in the table, "New AutoNumField"
: In the For ... Next loop it "looks" for the table "New Table" and then it
: should set the attribute of all fields to 17
: BUT IT DOES NOT...
: The strange thing is that i can change the name of all the fields, i can
: READ the attribute, but cannot CHANGE the attribute... please help.
:
:
: Dim dbsNew As database, tdf As TableDef, fld,fld2 As Field
: Set dbsNew = OpenDatabase("c:\newdb.mdb")
:
: Set tdf = dbsNieuw.CreateTableDef("New Table")
: Set fld = tdf.CreateField("New AutoNumField" , 4, 10)
: tdf.Fields.Append fld
: tdf.Fields.Refresh
: dbsNieuw.TableDefs.Append tdf
: dbsNieuw.TableDefs.Refresh
:
: Set tdf = nothing
:
: For Each tdf In dbsNew.TableDefs
:             If tdf.Attributes = 0 And tdf.Name = "New Table" Then
:                 For Each fld In tdf.Fields
:                     fld.Attributes = 17
:                 Next fld
:             End If
: Next tdf
:
:
: I can't make it work
:
:
: Rinke
:
:
:
:



Sat, 20 Oct 2001 03:00:00 GMT  
 Setting the field attribute
Pehaps because you may only have 1 autonumber field in a table. Your loop
intimates that there is more than 1 field. The code is probably being
rejected by the Jet engine.

I prefer using SQL in a DDL query:

CREATE TABLE tblContacts
([ContactID] counter,
[LastName] text,
[FirstName] text,
[ContactDate] date,
[Notes] memo,
CONSTRAINT [ContactID] PRIMARY KEY ([ContactID]));

In code, try something like:

Sub AddIncrementField()

Dim db As DATABASE
Dim tdf As TableDef
Dim fld As Field
Dim idx As index
    ' Return reference to current database.
Set db = CurrentDb
Set tdf = db.TableDefs![MyTable]
Set fld = tdf.CreateField("n", dbLong)
fld.Attributes = dbAutoIncrField

On Error Resume Next
tdf.Fields.Append fld
Set idx = tdf.CreateIndex("PrimaryKey")
idx.Primary = True
Set fld = idx.CreateField("n")
idx.Fields.Append fld   ' Append field and refresh collection.
tdf.Indexes.Append idx
tdf.Fields.Refresh

Set db = Nothing
End Sub
-----
Arvin Meyer

Quote:

>Hi Norm,
>really thanks for replying.. thought nobody was reading my scream for help.

>My problem is not to make a table and then make a field in it , but setting
>the attribute of a field.
>When i make a field in a table i can choose if it is a
>"nummeric/text/memo/etc".  but not "autonum" i discovered that it is the
>attribute "17" that makes it "autonum".
>I included the code so you can see what i am doing.
>Everything works (perhaps not the example, i ripped it out of my code and
>rewrote it to make it more readable)

>The code makes a Table in another DB, "New Table"
>Then it creates a field in the table, "New AutoNumField"
>In the For ... Next loop it "looks" for the table "New Table" and then it
>should set the attribute of all fields to 17
>BUT IT DOES NOT...
>The strange thing is that i can change the name of all the fields, i can
>READ the attribute, but cannot CHANGE the attribute... please help.

>Dim dbsNew As database, tdf As TableDef, fld,fld2 As Field
>Set dbsNew = OpenDatabase("c:\newdb.mdb")

>Set tdf = dbsNieuw.CreateTableDef("New Table")
>Set fld = tdf.CreateField("New AutoNumField" , 4, 10)
>tdf.Fields.Append fld
>tdf.Fields.Refresh
>dbsNieuw.TableDefs.Append tdf
>dbsNieuw.TableDefs.Refresh

>Set tdf = nothing

>For Each tdf In dbsNew.TableDefs
>            If tdf.Attributes = 0 And tdf.Name = "New Table" Then
>                For Each fld In tdf.Fields
>                    fld.Attributes = 17
>                Next fld
>            End If
>Next tdf

>I can't make it work

>Rinke



Sat, 20 Oct 2001 03:00:00 GMT  
 Setting the field attribute
Okay..ehh.... you are totally right !!!
In the example this is taken away to make it not too complex, but all the
different attributes are looked up in an table and then applied to the new
field in the new table.

Rinke



Sun, 21 Oct 2001 03:00:00 GMT  
 Setting the field attribute
Thanks Arvin,
It worked.. i looked at it for hours but could not see the error.. now i see
what i did wrong..
"fld.Attributes = ...." BEFORE "field.append fld"
I tried it the other way around... stupid stupid....

Thanks !!!!!!!!



Sun, 21 Oct 2001 03:00:00 GMT  
 Setting the field attribute
Whadaya mean, 'stupid, stupid'? I made the same mistake just a couple of
weeks ago! :-)

--
Brendan Reynolds

Quote:

>Thanks Arvin,
>It worked.. i looked at it for hours but could not see the error.. now i
see
>what i did wrong..
>"fld.Attributes = ...." BEFORE "field.append fld"
>I tried it the other way around... stupid stupid....

>Thanks !!!!!!!!



Sun, 21 Oct 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Access97 Field Attributes that are not in the Attributes collection of that Field

2. Setting Field Attributes

3. Setting Field Attributes

4. Setting Field Attributes

5. Programmatically setting field attributes with DAO

6. Setting field attribute dbHyperlinkField

7. Set File Attributes

8. setting procedure attribute NewEnum to -4??

9. Automated Utility to Set Excel Attributes

10. Set tables hidden attribute from code


 
Powered by phpBB® Forum Software © phpBB Group