By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,490 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,490 IT Pros & Developers. It's quick & easy.

VBA to Add or Remove a Field From a Table?

P: n/a
Dear Access 2003 users,

Can anyone assist me with creating either code (preferred) or a query
that would remove a single field (called ID) from a table? And as a
bonus question, can anyone assist with adding a field (called ID) to a
table and then make it an Autonumber data type? I've been successful
in adding the field via code, but cannot make it an autonumber field.
Thanks a million in advance!

Kevin
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Br
No Spam wrote:
Dear Access 2003 users,

Can anyone assist me with creating either code (preferred) or a query
that would remove a single field (called ID) from a table?
This is pretty basic SQL.....

DELETE * FROM tblMyTable WHERE [MyField] = 123;

One method of running it from code:

Function DeleteCode(ByVal pMyCode as Long)
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblMyTable WHERE [MyField] = " & pMyCode
& ";"
DoCmd.SetWarnings True
Exit Function

Oh hang on... or do you want to remove the actual field not just the
data???

Why?

Hint: look up the TableDef object in the Access help.
And as a
bonus question, can anyone assist with adding a field (called ID) to a
table and then make it an Autonumber data type?
I've been successful
in adding the field via code,
Code?
but cannot make it an autonumber field.
Thanks a million in advance!

Kevin


Please explain the scenario... ie. why are you trying to do this in
code?
--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 14 '05 #2

P: n/a
Option Explicit

'******************************************
'* UpgradeDB subroutine generated by *
'* Compare'EM on 11/14/2005 *
'****************************************** OLD
'* C:\Documents and Settings\mike.LIANLI\ *
'* Desktop\db1.mdb *
'****************************************** NEW
'* C:\Documents and Settings\mike.LIANLI\ *
'* Desktop\db2.mdb *
'******************************************
'* Compare'EM version 1.0c (PRO) *
'* Copyright 2005, Mike Noel *
'******************************************

Private Sub UpgradeDB()

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim idx As DAO.Index
Dim rel As DAO.Relation

'********************************************
' GIVE SERIOUS THOUGHT! - is next line OK ??
' 'db' defines the database to which changes
' will be applied. Do you really want that
' to be the same as the one where this code
' will run??
'
Set db = CurrentDb
'
'********************************************

' drop index PrimaryKey of table Table1
set tdf = db.TableDefs ("Table1")
tdf.Indexes.Delete "PrimaryKey"

' Create new field ID2 of table Table1
set tdf = db.TableDefs ("Table1")
set fld = tdf.CreateField("ID2", dbLong)
SetPro fld, "Attributes", dbLong, 17
tdf.Fields.Append fld
SetPro fld, "AllowZeroLength", dbBoolean, False
SetPro fld, "DefaultValue", dbText, ""
SetPro fld, "OrdinalPosition", dbLong, 1
SetPro fld, "Required", dbBoolean, False

' create index ID2 of table Table1
set tdf = db.TableDefs ("Table1")
set idx = tdf.CreateIndex("ID2")
set fld = idx.CreateField("ID2")
idx.Fields.Append fld
tdf.Indexes.Append idx
End Sub

'******************************************
'* SetPro subroutine supporting VBA code *
'* generated by Compare'EM *
'******************************************
'* Compare'EM version 1.0c (PRO) *
'* Copyright 2005, Mike Noel *
'******************************************

Private Sub SetPro(o As Object, s As String, t As DataTypeEnum, v As
Variant)
On Error GoTo Problems
o.Properties(s) = v
Exit Sub
Problems:
If Err = 3270 Then
o.Properties.Append o.CreateProperty(s, t, v)
Resume ProblemsX
End If
On Error GoTo 0
Resume
ProblemsX:
End Sub

No Spam wrote:
Dear Access 2003 users,

Can anyone assist me with creating either code (preferred) or a query
that would remove a single field (called ID) from a table? And as a
bonus question, can anyone assist with adding a field (called ID) to a
table and then make it an Autonumber data type? I've been successful
in adding the field via code, but cannot make it an autonumber field.
Thanks a million in advance!

Kevin

Nov 14 '05 #3

P: n/a
Sorry for earlier messed up post, here is results from free tool that
automates creation of the code you want. You can find the tool at
http://home.gci.net/~mike-noel/CompareEM.htm

Option Explicit

'******************************************
'* UpgradeDB subroutine generated by *
'* Compare'EM on 11/14/2005 *
'******************************************
'* Requires Microsoft DAO Object Library *
'****************************************** OLD
'* I:\db1.mdb *
'****************************************** NEW
'* I:\db2.mdb *
'******************************************
'* Compare'EM version 1.0c (PRO) *
'* Copyright 2005, Mike Noel *
'******************************************

Private Sub UpgradeDB()

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim idx As DAO.Index
Dim rel As DAO.Relation

'********************************************
' GIVE SERIOUS THOUGHT! - is next line OK ??
' 'db' defines the database to which changes
' will be applied. Do you really want that
' to be the same as the one where this code
' will run??
'
Set db = CurrentDb
'
'********************************************

' drop index PrimaryKey of table Table1
set tdf = db.TableDefs ("Table1")
tdf.Indexes.Delete "PrimaryKey"

' drop ID field of table Table1
set tdf = db.TableDefs ("Table1")
tdf.Fields.Delete ("ID")

' Create new field ID2 of table Table1
set tdf = db.TableDefs ("Table1")
set fld = tdf.CreateField("ID2", dbLong)
SetPro fld, "Attributes", dbLong, 17
tdf.Fields.Append fld
SetPro fld, "AllowZeroLength", dbBoolean, False
SetPro fld, "DefaultValue", dbText, ""
SetPro fld, "OrdinalPosition", dbLong, 1
SetPro fld, "Required", dbBoolean, False

' create index ID2 of table Table1
set tdf = db.TableDefs ("Table1")
set idx = tdf.CreateIndex("ID2")
set fld = idx.CreateField("ID2")
idx.Fields.Append fld
tdf.Indexes.Append idx
End Sub

'******************************************
'* SetPro subroutine supporting VBA code *
'* generated by Compare'EM *
'******************************************
'* Compare'EM version 1.0c (PRO) *
'* Copyright 2005, Mike Noel *
'******************************************

Private Sub SetPro(o As Object, s As String, t As DataTypeEnum, v As
Variant)
On Error GoTo Problems
o.Properties(s) = v
Exit Sub
Problems:
If Err = 3270 Then
o.Properties.Append o.CreateProperty(s, t, v)
Resume ProblemsX
End If
On Error GoTo 0
Resume
ProblemsX:
End Sub

No Spam wrote:
Dear Access 2003 users,

Can anyone assist me with creating either code (preferred) or a query
that would remove a single field (called ID) from a table? And as a
bonus question, can anyone assist with adding a field (called ID) to a
table and then make it an Autonumber data type? I've been successful
in adding the field via code, but cannot make it an autonumber field.
Thanks a million in advance!

Kevin

Nov 14 '05 #4

P: n/a
The OP wanted to remove/add the field not delete records.

BTW The SQL you quote can be executed as a one liner

currentdb.Execute "DELETE * FROM tblMyTable WHERE [MyField] = 123"

No need for that Setwarnings stuff.
--
Terry Kreft

"Br@dley" <br**@usenet.org> wrote in message
news:z9******************@news-server.bigpond.net.au...
No Spam wrote:
Dear Access 2003 users,

Can anyone assist me with creating either code (preferred) or a query
that would remove a single field (called ID) from a table?


This is pretty basic SQL.....

DELETE * FROM tblMyTable WHERE [MyField] = 123;

One method of running it from code:

Function DeleteCode(ByVal pMyCode as Long)
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblMyTable WHERE [MyField] = " & pMyCode &
";"
DoCmd.SetWarnings True
Exit Function

Oh hang on... or do you want to remove the actual field not just the
data???

Why?

Hint: look up the TableDef object in the Access help.
And as a
bonus question, can anyone assist with adding a field (called ID) to a
table and then make it an Autonumber data type?
I've been successful
in adding the field via code,


Code?
but cannot make it an autonumber field.
Thanks a million in advance!

Kevin


Please explain the scenario... ie. why are you trying to do this in code?
--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response

Nov 14 '05 #5

P: n/a
Br
Terry Kreft wrote:
The OP wanted to remove/add the field not delete records.
I realised that half way through responding.
BTW The SQL you quote can be executed as a one liner

currentdb.Execute "DELETE * FROM tblMyTable WHERE [MyField] = 123"

No need for that Setwarnings stuff.

Sure. I just quoted one, perhaps older, method.

"Br@dley" <br**@usenet.org> wrote in message
news:z9******************@news-server.bigpond.net.au...
No Spam wrote:
Dear Access 2003 users,

Can anyone assist me with creating either code (preferred) or a
query that would remove a single field (called ID) from a table?


This is pretty basic SQL.....

DELETE * FROM tblMyTable WHERE [MyField] = 123;

One method of running it from code:

Function DeleteCode(ByVal pMyCode as Long)
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblMyTable WHERE [MyField] = " &
pMyCode & ";"
DoCmd.SetWarnings True
Exit Function

Oh hang on... or do you want to remove the actual field not just the
data???

Why?

Hint: look up the TableDef object in the Access help.
And as a
bonus question, can anyone assist with adding a field (called ID)
to a table and then make it an Autonumber data type?
I've been successful
in adding the field via code,


Code?
but cannot make it an autonumber field.
Thanks a million in advance!

Kevin


Please explain the scenario... ie. why are you trying to do this in
code? --
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response


--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 15 '05 #6

P: n/a
The method you quoted is still valid. The reason I don't like it is because
it has an inherent danger in that it is too easy to setwarnings to false and
then forget to switch it back on. This then means that a whole bunch of
rather important warnings are switched off.

Try this for an example:-
create a table
fill it with some records
open the immediate window
type docmd.SetWarnings False and hit the enter key
switch to the database window
select your table
press the delete key.

Goodbye table and the associated records without any warning or chance to
back out of it.

--
Terry Kreft

"Br@dley" <br**@usenet.org> wrote in message
news:AY*****************@news-server.bigpond.net.au...
Terry Kreft wrote: <SNIP>
BTW The SQL you quote can be executed as a one liner

currentdb.Execute "DELETE * FROM tblMyTable WHERE [MyField] = 123"

No need for that Setwarnings stuff.

Sure. I just quoted one, perhaps older, method.

"Br@dley" <br**@usenet.org> wrote in message
news:z9******************@news-server.bigpond.net.au...
No Spam wrote:
Dear Access 2003 users,

Can anyone assist me with creating either code (preferred) or a
query that would remove a single field (called ID) from a table?

This is pretty basic SQL.....

DELETE * FROM tblMyTable WHERE [MyField] = 123;

One method of running it from code:

Function DeleteCode(ByVal pMyCode as Long)
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblMyTable WHERE [MyField] = " &
pMyCode & ";"
DoCmd.SetWarnings True
Exit Function

Oh hang on... or do you want to remove the actual field not just the
data???

Why?

Hint: look up the TableDef object in the Access help.

And as a
bonus question, can anyone assist with adding a field (called ID)
to a table and then make it an Autonumber data type?
I've been successful
in adding the field via code,

Code?

but cannot make it an autonumber field.
Thanks a million in advance!

Kevin

Please explain the scenario... ie. why are you trying to do this in
code? --
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response


--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response

Nov 15 '05 #7

P: n/a

Thanks Mike (and everyone else who helped out)! I adapted the code
and it works perfectly!

Kevin

On Mon, 14 Nov 2005 13:10:21 -0900, mike noel <mi*******@gci.net>
wrote:
Sorry for earlier messed up post, here is results from free tool that
automates creation of the code you want. You can find the tool at
http://home.gci.net/~mike-noel/CompareEM.htm

Option Explicit

'******************************************
'* UpgradeDB subroutine generated by *
'* Compare'EM on 11/14/2005 *
'******************************************
'* Requires Microsoft DAO Object Library *
'****************************************** OLD
'* I:\db1.mdb *
'****************************************** NEW
'* I:\db2.mdb *
'******************************************
'* Compare'EM version 1.0c (PRO) *
'* Copyright 2005, Mike Noel *
'******************************************

Private Sub UpgradeDB()

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim idx As DAO.Index
Dim rel As DAO.Relation

'********************************************
' GIVE SERIOUS THOUGHT! - is next line OK ??
' 'db' defines the database to which changes
' will be applied. Do you really want that
' to be the same as the one where this code
' will run??
'
Set db = CurrentDb
'
'********************************************

' drop index PrimaryKey of table Table1
set tdf = db.TableDefs ("Table1")
tdf.Indexes.Delete "PrimaryKey"

' drop ID field of table Table1
set tdf = db.TableDefs ("Table1")
tdf.Fields.Delete ("ID")

' Create new field ID2 of table Table1
set tdf = db.TableDefs ("Table1")
set fld = tdf.CreateField("ID2", dbLong)
SetPro fld, "Attributes", dbLong, 17
tdf.Fields.Append fld
SetPro fld, "AllowZeroLength", dbBoolean, False
SetPro fld, "DefaultValue", dbText, ""
SetPro fld, "OrdinalPosition", dbLong, 1
SetPro fld, "Required", dbBoolean, False

' create index ID2 of table Table1
set tdf = db.TableDefs ("Table1")
set idx = tdf.CreateIndex("ID2")
set fld = idx.CreateField("ID2")
idx.Fields.Append fld
tdf.Indexes.Append idx
End Sub

'******************************************
'* SetPro subroutine supporting VBA code *
'* generated by Compare'EM *
'******************************************
'* Compare'EM version 1.0c (PRO) *
'* Copyright 2005, Mike Noel *
'******************************************

Private Sub SetPro(o As Object, s As String, t As DataTypeEnum, v As
Variant)
On Error GoTo Problems
o.Properties(s) = v
Exit Sub
Problems:
If Err = 3270 Then
o.Properties.Append o.CreateProperty(s, t, v)
Resume ProblemsX
End If
On Error GoTo 0
Resume
ProblemsX:
End Sub

No Spam wrote:
Dear Access 2003 users,

Can anyone assist me with creating either code (preferred) or a query
that would remove a single field (called ID) from a table? And as a
bonus question, can anyone assist with adding a field (called ID) to a
table and then make it an Autonumber data type? I've been successful
in adding the field via code, but cannot make it an autonumber field.
Thanks a million in advance!

Kevin


Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.