473,714 Members | 3,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 27168
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(ByVa l pMyCode as Long)
DoCmd.SetWarnin gs False
DoCmd.RunSQL "DELETE * FROM tblMyTable WHERE [MyField] = " & pMyCode
& ";"
DoCmd.SetWarnin gs 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.L IANLI\ *
'* Desktop\db1.mdb *
'************** *************** ************* NEW
'* C:\Documents and Settings\mike.L IANLI\ *
'* 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.Del ete "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.Appe nd fld
SetPro fld, "AllowZeroLengt h", dbBoolean, False
SetPro fld, "DefaultVal ue", dbText, ""
SetPro fld, "OrdinalPositio n", 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.Appe nd fld
tdf.Indexes.App end 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.Ap pend o.CreatePropert y(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.Del ete "PrimaryKey "

' drop ID field of table Table1
set tdf = db.TableDefs ("Table1")
tdf.Fields.Dele te ("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.Appe nd fld
SetPro fld, "AllowZeroLengt h", dbBoolean, False
SetPro fld, "DefaultVal ue", dbText, ""
SetPro fld, "OrdinalPositio n", 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.Appe nd fld
tdf.Indexes.App end 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.Ap pend o.CreatePropert y(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.Execu te "DELETE * FROM tblMyTable WHERE [MyField] = 123"

No need for that Setwarnings stuff.
--
Terry Kreft

"Br@dley" <br**@usenet.or g> 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(ByVa l pMyCode as Long)
DoCmd.SetWarnin gs False
DoCmd.RunSQL "DELETE * FROM tblMyTable WHERE [MyField] = " & pMyCode &
";"
DoCmd.SetWarnin gs 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.Execu te "DELETE * FROM tblMyTable WHERE [MyField] = 123"

No need for that Setwarnings stuff.

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

"Br@dley" <br**@usenet.or g> 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(ByVa l pMyCode as Long)
DoCmd.SetWarnin gs False
DoCmd.RunSQL "DELETE * FROM tblMyTable WHERE [MyField] = " &
pMyCode & ";"
DoCmd.SetWarnin gs 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.SetWarnin gs 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.or g> 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.Execu te "DELETE * FROM tblMyTable WHERE [MyField] = 123"

No need for that Setwarnings stuff.

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

"Br@dley" <br**@usenet.or g> 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(ByVa l pMyCode as Long)
DoCmd.SetWarnin gs False
DoCmd.RunSQL "DELETE * FROM tblMyTable WHERE [MyField] = " &
pMyCode & ";"
DoCmd.SetWarnin gs 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.Del ete "PrimaryKey "

' drop ID field of table Table1
set tdf = db.TableDefs ("Table1")
tdf.Fields.Dele te ("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.Appe nd fld
SetPro fld, "AllowZeroLengt h", dbBoolean, False
SetPro fld, "DefaultVal ue", dbText, ""
SetPro fld, "OrdinalPositio n", 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.Appe nd fld
tdf.Indexes.App end 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.Ap pend o.CreatePropert y(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
7547
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 just need help with the removerow function. Thanks for any help you can offer me. Here is the code: <html> <head> <title>New Document</title> <script type="text/javascript">
3
2177
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 pointing to a derived class, but the whole thing is in the process of being destroyed. Interesting for me is also, that I get the same error if I call b->isA() in the constructor of Base.
0
2134
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 system. I actually import from two different named ranges from each workbook so for every import the following subroutine is used twice. The database is used to collate data collected from different surveys entered by remote users into an Excel...
0
1761
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 through a button in my access form, insert the bookmarks with data from the form and save the word document in the hard disk. I need to save/copy this document into a table as ole object, so the documents are kept within the recordset.
8
8472
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 thanks. Gerry Abbott
6
1887
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 imported from Lotus Approach. In Approach, to add another field, you just went to Design, and there was a simple process to add a field, including its label and what would go in the field. Example: you have a database, and want to add the field Work...
4
3143
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. Here's the SQL I'm using... UPDATE Addresses SET Addresses.RawData = Trim$(); "Microsoft Access didn't update 2353 field(s) due to a type conversion filter..." I wonder what kind of type conversion Access is trying to
0
1297
by: kalyanakrishna | last post by:
I am not able to create a trigger on Text field table Comments will be appriciatable.
9
1722
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 OTCUser.checklistcomm, but only the last result which are conform to the condition. So the fields in black shouldn't appair. Can someone help me ? SELECT MAX(p.Modifdatum) AS modifdatumv, OTCUser.checklistcomm.comm, p.Status, p.station,...
3
2829
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 primary key in my lookup table. e.g. If I input the number "6046" in the Material field I want it to populate the Material Type field with "Stainless Steel". I have setup my lookup table with the number relating to the material type as the primary...
0
8808
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8712
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9316
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9179
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3160
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.