473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

VBA to Add or Remove a Field From a Table?

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
7 27122
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve S | last post by:
I need some help modifying this code. The code was orginally setup to add/remove a single table row. I modified it to add 2 (two) rows with one column that spans both rows. That part works. I...
3
by: Martin Koller | last post by:
Hi list, I have a small example which gives me "pure virtual function called". I'm calling a virtual function in the destructor of the base class, where the pointer used is effectively...
0
by: me here | last post by:
I have a VBA subroutine that links an MS Excel spreadsheet and copies the data into a local table. This process is controlled by a form that allows users to select the spreadsheet from the file...
0
by: beau | last post by:
Please help me ! Anybody can help me with a code to embed an ms word file into an access table ? I have ms word templates with bookmarks in my hard disk. So far I can open the template...
8
by: Gerry Abbott | last post by:
Could someone please tell me is there a simple way to refresh a table open in standard table view, without having to either close and re-open, or open in design than back to datasheet view. Many...
6
by: Nhmiller | last post by:
I can find nothing about how to do this simple operation of adding a field so that it can be chosen to appear in any view of the database, such as a report. I already have a data base that I...
4
by: MLH | last post by:
My table has an autonumber field and a text field named . Many records have unnecessary spaces padding the front and end of - at least, I think they're spaces. The look like spaces to me. ...
0
by: kalyanakrishna | last post by:
I am not able to create a trigger on Text field table Comments will be appriciatable.
9
whoops
by: whoops | last post by:
Hi, I' ve been struggling for a few days with following problem. I have a query with 2 tables (below). They are joined by one key field checknr. I would only like to see field comm from table...
3
greeni91
by: greeni91 | last post by:
I am currently in the process of creating a database to record problems at my work. I am trying to AutoFill a field (Material Type) if another field (Material) has a number exactly like the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.