PC Review


Reply
Thread Tools Rate Thread

Delete index using DAO

 
 
Bob Howard
Guest
Posts: n/a
 
      6th Feb 2010
I have a database containing a specifi field that has an index. It was
originally defined that way.

I now want to run some DAO code against that table to delete that field.
But I get an error because it says the field has an index.

So I want to implement a DAO method to delete the index from the field, and
then delete the field.

What method do I use to delete the index? Or is there another way to handle
this?

TIA..

bob


 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      6th Feb 2010
On Fri, 5 Feb 2010 18:42:24 -0800, "Bob Howard" <(E-Mail Removed)> wrote:

>I have a database containing a specifi field that has an index. It was
>originally defined that way.
>
>I now want to run some DAO code against that table to delete that field.
>But I get an error because it says the field has an index.
>
>So I want to implement a DAO method to delete the index from the field, and
>then delete the field.
>
>What method do I use to delete the index? Or is there another way to handle
>this?
>
>TIA..
>
>bob
>


I'm pretty sure that's not the problem, and that's not the error: to my
knowledge there is no block to deleting a field just because it's indexed
(Access will silently drop the index for you).

My guess is that it's objecting because you have a *relationship* with
referential integrity enforced to some other table, on this field. Do you?
What is the actual text of the error message?
--

John W. Vinson [MVP]
 
Reply With Quote
 
Bob Howard
Guest
Posts: n/a
 
      6th Feb 2010

"John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
news:(E-Mail Removed)...
> On Fri, 5 Feb 2010 18:42:24 -0800, "Bob Howard" <(E-Mail Removed)>
> wrote:
>
>>I have a database containing a specifi field that has an index. It was
>>originally defined that way.
>>
>>I now want to run some DAO code against that table to delete that field.
>>But I get an error because it says the field has an index.
>>
>>So I want to implement a DAO method to delete the index from the field,
>>and
>>then delete the field.
>>
>>What method do I use to delete the index? Or is there another way to
>>handle
>>this?
>>
>>TIA..
>>
>>bob
>>

>
> I'm pretty sure that's not the problem, and that's not the error: to my
> knowledge there is no block to deleting a field just because it's indexed
> (Access will silently drop the index for you).
>
> My guess is that it's objecting because you have a *relationship* with
> referential integrity enforced to some other table, on this field. Do you?
> What is the actual text of the error message?
> --
>
> John W. Vinson [MVP]


The error message is as follows: "Error # 3280 ... Cannot delete a field
that is part of an index or is needed by the system."

I checked, and the only field in that table that has a relationship is the
primary key field (which is an autonumber). Besides the primary key, there
are 7 or 8 other fields (all text). This table is used to hold addresses.
The field I'm trying to play with is ZIP code.

Here's the environment.

The database from which I'm trying to delete the field is in Access 2000
format. It's used as the back-end database of a front-end / back-end setup.

I need to expand the field (text) from 10 to 11 characters (the application
is used in many countries, and I just got my first user in Kenya ... which I
discovered has an 11-digit postal code that needs to be stored in this
field). So I sent the user a little mdb written in Access 2003 that
connects to the back-end and which will delete that field and redefine it as
11 characters. The "delete" is what's failing. [p.s. if I could just code
something to expand the length of the ZIP code field in place without having
to delete it and re-define it, that would be far more excellent.]

Anyway, the computer on which it's running (the user's computer) has Access
2007 Runtime installed (the latest version that MS updated, in April 2009 if
I recall).

bob


 
Reply With Quote
 
GeoffG
Guest
Posts: n/a
 
      8th Feb 2010
I don't know if you can run code in one database to change
the size of a text field in another database when the code
is executed in a runtime version of Access. As you know,
the Access runtime does not allow database modification at
the user interface, but I don't know whether that applies
when the modification is executed in code.

I recommend you try running the code below (or something
like it) on a typical target machine (with only Access
runtime installed) before distributing it. I'd be glad to
hear if it does, or doesn't work.

The database in which the code runs does not require a
linked table to the backend table.

If the database is not installed in the same folder on every
machine, there is a workaround, such as assuming both
databases are in the same folder.

You may like to examine Allen Browne's code examples at:
http://allenbrowne.com/tips.html
Follow the links for:
DDL Query
ModifyFieldDDL()
That code example applies to changing a field's size in the
current database.
The code below opens the backend database - in memory, not
in the user interface - and makes the required change using
DDL (Data Definition Language).


Copy and paste the following into a new blank module of a
new blank database.


Option Compare Database
Option Explicit

' This module requires a reference to
' Microsoft DAO 3.6 Object Library
' (In VBA editor, Tools > References).


Public Sub ChangeZipFieldSize()

' Purpose:
' To change the size of the Zip field
' in the backend database from 10
' characters to 11 characters.

Const strcBackendDBFullPath As String = _
"C:\My Documents\MyDatabaseName.mdb"
Const strcTableName As String = "MyTableName"
Const strcFieldName As String = "MyFieldName"
Const strcNewSize As String = "11"

Call ChangeZipField( _
strcBackendDBFullPath, _
strcTableName, _
strcFieldName, _
strcNewSize)

End Sub

Private Sub ChangeZipField( _
strBackendDBFullPath As String, _
strTableName As String, _
strFieldName As String, _
strNewSize As String)


Dim objWS As DAO.Workspace
Dim objDB As DAO.Database
Dim objTBL As DAO.TableDef
Dim objFLD As DAO.Field
Dim strSQL As String

' Initialize the SQL statement that will
' alter the field's size:
strSQL = "ALTER TABLE " & strTableName _
& " ALTER COLUMN " & strFieldName _
& " TEXT(" & strNewSize & ");"

' Open the database and point to the field
' that needs its size changed:
Set objWS = DBEngine.Workspaces(0)
Set objDB = objWS.OpenDatabase(strBackendDBFullPath)
Set objTBL = objDB.TableDefs(strTableName)
Set objFLD = objTBL.Fields(strFieldName)

' Ensure the field is a text field and,
' if it is, change its size:
If Not objFLD.Type = dbText Then
Call Msg1(strBackendDBFullPath, _
strTableName, strFieldName)
GoTo Exit_ChangeZipField
Else
objDB.Execute strSQL, dbFailOnError
Call Msg2(strBackendDBFullPath, _
strTableName, strFieldName, strNewSize)
GoTo Exit_ChangeZipField
End If

Exit_ChangeZipField:

On Error GoTo Abort_CleanUp_ChangeZipField

' Destroy DAO objects:
Set objFLD = Nothing
Set objTBL = Nothing
If Not objDB Is Nothing Then
objDB.Close
Set objDB = Nothing
End If
Set objWS = Nothing

Exit Sub

Abort_CleanUp_ChangeZipField:

Exit Sub

Error_ChangeZipField:

MsgBox "Error No: " & Err.Number _
& vbNewLine _
& "Description:" & vbNewLine _
& Err.Description, _
vbExclamation + vbOKOnly, _
"Error Information"

Resume Exit_ChangeZipField

End Sub

Private Sub Msg1(strDbName As String, _
strTableName As String, _
strFieldName As String)

MsgBox "Database:" & vbNewLine _
& strDbName & vbNewLine & vbNewLine _
& "Table: " & strTableName & vbNewLine _
& "Field: " & strFieldName _
& vbNewLine & vbNewLine _
& "The above field is not a Text field. " _
& "Therefore, the field's size " _
& "was not changed.", _
vbInformation + vbOKOnly, _
"Information"

End Sub

Private Sub Msg2(strDbName As String, _
strTableName As String, _
strFieldName As String, _
strNewSize As String)

MsgBox "Database:" & vbNewLine _
& strDbName & vbNewLine & vbNewLine _
& "Table:" & vbTab & strTableName _
& vbNewLine _
& "Field:" & vbTab & strFieldName _
& vbNewLine _
& "New Size:" & vbTab & strNewSize _
& vbNewLine & vbNewLine _
& "The size of the above text field " _
& "has been changed.", _
vbInformation + vbOKOnly, _
"Information"

End Sub


Geoff.






> snipped.
>
> The error message is as follows: "Error # 3280 ... Cannot
> delete a field that is part of an index or is needed by
> the system."
>
> I checked, and the only field in that table that has a
> relationship is the primary key field (which is an
> autonumber). Besides the primary key, there are 7 or 8
> other fields (all text). This table is used to hold
> addresses. The field I'm trying to play with is ZIP code.
>
> Here's the environment.
>
> The database from which I'm trying to delete the field is
> in Access 2000 format. It's used as the back-end database
> of a front-end / back-end setup.
>
> I need to expand the field (text) from 10 to 11 characters
> (the application is used in many countries, and I just got
> my first user in Kenya ... which I discovered has an
> 11-digit postal code that needs to be stored in this
> field). So I sent the user a little mdb written in Access
> 2003 that connects to the back-end and which will delete
> that field and redefine it as 11 characters. The "delete"
> is what's failing. [p.s. if I could just code something
> to expand the length of the ZIP code field in place
> without having to delete it and re-define it, that would
> be far more excellent.]
>
> Anyway, the computer on which it's running (the user's
> computer) has Access 2007 Runtime installed (the latest
> version that MS updated, in April 2009 if I recall).
>
> bob



 
Reply With Quote
 
GeoffG
Guest
Posts: n/a
 
      8th Feb 2010
Sorry - a small code change is needed, which, no doubt, you
will have spotted.

Please add after the variable declarations (Dim statements):

On Error GoTo Error_ChangeZipField

Geoff



"GeoffG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I don't know if you can run code in one database to change
> the size of a text field in another database when the code
> is executed in a runtime version of Access. As you know,
> the Access runtime does not allow database modification at
> the user interface, but I don't know whether that applies
> when the modification is executed in code.
>
> I recommend you try running the code below (or something
> like it) on a typical target machine (with only Access
> runtime installed) before distributing it. I'd be glad to
> hear if it does, or doesn't work.
>
> The database in which the code runs does not require a
> linked table to the backend table.
>
> If the database is not installed in the same folder on
> every
> machine, there is a workaround, such as assuming both
> databases are in the same folder.
>
> You may like to examine Allen Browne's code examples at:
> http://allenbrowne.com/tips.html
> Follow the links for:
> DDL Query
> ModifyFieldDDL()
> That code example applies to changing a field's size in
> the
> current database.
> The code below opens the backend database - in memory, not
> in the user interface - and makes the required change
> using
> DDL (Data Definition Language).
>
>
> Copy and paste the following into a new blank module of a
> new blank database.
>
>
> Option Compare Database
> Option Explicit
>
> ' This module requires a reference to
> ' Microsoft DAO 3.6 Object Library
> ' (In VBA editor, Tools > References).
>
>
> Public Sub ChangeZipFieldSize()
>
> ' Purpose:
> ' To change the size of the Zip field
> ' in the backend database from 10
> ' characters to 11 characters.
>
> Const strcBackendDBFullPath As String = _
> "C:\My Documents\MyDatabaseName.mdb"
> Const strcTableName As String = "MyTableName"
> Const strcFieldName As String = "MyFieldName"
> Const strcNewSize As String = "11"
>
> Call ChangeZipField( _
> strcBackendDBFullPath, _
> strcTableName, _
> strcFieldName, _
> strcNewSize)
>
> End Sub
>
> Private Sub ChangeZipField( _
> strBackendDBFullPath As String, _
> strTableName As String, _
> strFieldName As String, _
> strNewSize As String)
>
>
> Dim objWS As DAO.Workspace
> Dim objDB As DAO.Database
> Dim objTBL As DAO.TableDef
> Dim objFLD As DAO.Field
> Dim strSQL As String
>
> ' Initialize the SQL statement that will
> ' alter the field's size:
> strSQL = "ALTER TABLE " & strTableName _
> & " ALTER COLUMN " & strFieldName _
> & " TEXT(" & strNewSize & ");"
>
> ' Open the database and point to the field
> ' that needs its size changed:
> Set objWS = DBEngine.Workspaces(0)
> Set objDB = objWS.OpenDatabase(strBackendDBFullPath)
> Set objTBL = objDB.TableDefs(strTableName)
> Set objFLD = objTBL.Fields(strFieldName)
>
> ' Ensure the field is a text field and,
> ' if it is, change its size:
> If Not objFLD.Type = dbText Then
> Call Msg1(strBackendDBFullPath, _
> strTableName, strFieldName)
> GoTo Exit_ChangeZipField
> Else
> objDB.Execute strSQL, dbFailOnError
> Call Msg2(strBackendDBFullPath, _
> strTableName, strFieldName, strNewSize)
> GoTo Exit_ChangeZipField
> End If
>
> Exit_ChangeZipField:
>
> On Error GoTo Abort_CleanUp_ChangeZipField
>
> ' Destroy DAO objects:
> Set objFLD = Nothing
> Set objTBL = Nothing
> If Not objDB Is Nothing Then
> objDB.Close
> Set objDB = Nothing
> End If
> Set objWS = Nothing
>
> Exit Sub
>
> Abort_CleanUp_ChangeZipField:
>
> Exit Sub
>
> Error_ChangeZipField:
>
> MsgBox "Error No: " & Err.Number _
> & vbNewLine _
> & "Description:" & vbNewLine _
> & Err.Description, _
> vbExclamation + vbOKOnly, _
> "Error Information"
>
> Resume Exit_ChangeZipField
>
> End Sub
>
> Private Sub Msg1(strDbName As String, _
> strTableName As String, _
> strFieldName As String)
>
> MsgBox "Database:" & vbNewLine _
> & strDbName & vbNewLine & vbNewLine _
> & "Table: " & strTableName & vbNewLine _
> & "Field: " & strFieldName _
> & vbNewLine & vbNewLine _
> & "The above field is not a Text field. " _
> & "Therefore, the field's size " _
> & "was not changed.", _
> vbInformation + vbOKOnly, _
> "Information"
>
> End Sub
>
> Private Sub Msg2(strDbName As String, _
> strTableName As String, _
> strFieldName As String, _
> strNewSize As String)
>
> MsgBox "Database:" & vbNewLine _
> & strDbName & vbNewLine & vbNewLine _
> & "Table:" & vbTab & strTableName _
> & vbNewLine _
> & "Field:" & vbTab & strFieldName _
> & vbNewLine _
> & "New Size:" & vbTab & strNewSize _
> & vbNewLine & vbNewLine _
> & "The size of the above text field " _
> & "has been changed.", _
> vbInformation + vbOKOnly, _
> "Information"
>
> End Sub
>
>
> Geoff.
>
>
>
>
>
>
>> snipped.
>>
>> The error message is as follows: "Error # 3280 ...
>> Cannot
>> delete a field that is part of an index or is needed by
>> the system."
>>
>> I checked, and the only field in that table that has a
>> relationship is the primary key field (which is an
>> autonumber). Besides the primary key, there are 7 or 8
>> other fields (all text). This table is used to hold
>> addresses. The field I'm trying to play with is ZIP code.
>>
>> Here's the environment.
>>
>> The database from which I'm trying to delete the field is
>> in Access 2000 format. It's used as the back-end
>> database
>> of a front-end / back-end setup.
>>
>> I need to expand the field (text) from 10 to 11
>> characters
>> (the application is used in many countries, and I just
>> got
>> my first user in Kenya ... which I discovered has an
>> 11-digit postal code that needs to be stored in this
>> field). So I sent the user a little mdb written in
>> Access
>> 2003 that connects to the back-end and which will delete
>> that field and redefine it as 11 characters. The
>> "delete"
>> is what's failing. [p.s. if I could just code something
>> to expand the length of the ZIP code field in place
>> without having to delete it and re-define it, that would
>> be far more excellent.]
>>
>> Anyway, the computer on which it's running (the user's
>> computer) has Access 2007 Runtime installed (the latest
>> version that MS updated, in April 2009 if I recall).
>>
>> bob

>
>

 
Reply With Quote
 
GeoffG
Guest
Posts: n/a
 
      9th Feb 2010
If anyone reads this thread thinking "Oh dear!" - I agree!

Let's hope Bob Howard reads:
http://tc2.atspace.com/0012-UsingUsenet.htm

Geoff


 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      9th Feb 2010
Tbe Runtime has no restrictions around making changes to tables (or to
macros, for that matter). It's forms, reports and VBA modules that it can't
change.

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
(no e-mails, please!)

"GeoffG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I don't know if you can run code in one database to change
> the size of a text field in another database when the code
> is executed in a runtime version of Access. As you know,
> the Access runtime does not allow database modification at
> the user interface, but I don't know whether that applies
> when the modification is executed in code.



 
Reply With Quote
 
GeoffG
Guest
Posts: n/a
 
      9th Feb 2010
Thanks, Doug.

> Tbe Runtime has no restrictions around
> making changes to tables (or to macros,
> for that matter).


Does that apply at the user-interface, as well as
programmatically?

Geoff



"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_gmail.com> wrote
in message news:um#(E-Mail Removed)...
> Tbe Runtime has no restrictions around making changes to
> tables (or to macros, for that matter). It's forms,
> reports and VBA modules that it can't change.
>
> --
> Doug Steele, Microsoft Access MVP
> http://www.AccessMVP.com/DJSteele
> (no e-mails, please!)
>
> "GeoffG" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I don't know if you can run code in one database to change
>> the size of a text field in another database when the
>> code
>> is executed in a runtime version of Access. As you know,
>> the Access runtime does not allow database modification
>> at
>> the user interface, but I don't know whether that applies
>> when the modification is executed in code.

>
>

 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      10th Feb 2010
As far as I know, yes.

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
(no e-mails, please!)

"GeoffG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks, Doug.
>
>> Tbe Runtime has no restrictions around
>> making changes to tables (or to macros,
>> for that matter).

>
> Does that apply at the user-interface, as well as
> programmatically?
>
> Geoff
>
>
>
> "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_gmail.com> wrote
> in message news:um#(E-Mail Removed)...
>> Tbe Runtime has no restrictions around making changes to
>> tables (or to macros, for that matter). It's forms,
>> reports and VBA modules that it can't change.
>>
>> --
>> Doug Steele, Microsoft Access MVP
>> http://www.AccessMVP.com/DJSteele
>> (no e-mails, please!)
>>
>> "GeoffG" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>I don't know if you can run code in one database to change
>>> the size of a text field in another database when the
>>> code
>>> is executed in a runtime version of Access. As you know,
>>> the Access runtime does not allow database modification
>>> at
>>> the user interface, but I don't know whether that applies
>>> when the modification is executed in code.

>>
>>



 
Reply With Quote
 
Bob Howard
Guest
Posts: n/a
 
      10th Feb 2010

"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_gmail.com> wrote in message
news:(E-Mail Removed)...
> As far as I know, yes.
>
> --
> Doug Steele, Microsoft Access MVP
> http://www.AccessMVP.com/DJSteele
> (no e-mails, please!)
>
> "GeoffG" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Thanks, Doug.
>>
>>> Tbe Runtime has no restrictions around
>>> making changes to tables (or to macros,
>>> for that matter).

>>
>> Does that apply at the user-interface, as well as
>> programmatically?
>>
>> Geoff
>>
>>
>>
>> "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_gmail.com> wrote
>> in message news:um#(E-Mail Removed)...
>>> Tbe Runtime has no restrictions around making changes to
>>> tables (or to macros, for that matter). It's forms,
>>> reports and VBA modules that it can't change.
>>>
>>> --
>>> Doug Steele, Microsoft Access MVP
>>> http://www.AccessMVP.com/DJSteele
>>> (no e-mails, please!)
>>>
>>> "GeoffG" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>>I don't know if you can run code in one database to change
>>>> the size of a text field in another database when the
>>>> code
>>>> is executed in a runtime version of Access. As you know,
>>>> the Access runtime does not allow database modification
>>>> at
>>>> the user interface, but I don't know whether that applies
>>>> when the modification is executed in code.
>>>
>>>

>
>


Turns out that I'll probably be doing something like this in the future.
For now, I'm all set. thanks!!!! bob


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Index Reveal which formula causing true result Windows Vista General Discussion 1 19th Jul 2008 02:07 AM
Delete Index.DAT JD Windows XP General 10 5th Apr 2006 09:20 PM
how to auto delete all index entries entered by an index file mason Microsoft Word Document Management 1 13th Feb 2004 02:42 AM
Index.dat Suite - View & Delete Index.dat Files, Temp Internet Files (TIF), Cookies, History, Temp Files BillR Freeware 39 1st Nov 2003 09:40 PM
How to delete Index.dat MA Windows XP General 6 2nd Oct 2003 04:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:39 PM.