PC Review


Reply
Thread Tools Rate Thread

Acces won't allow me to delete related tables with VBA

 
 
Sirritys
Guest
Posts: n/a
 
      12th Oct 2006
Hi,

I have a problem (again).

I'm trying to delete table from database with VBA using
"DoCmd.DeleteObject" before I import the table with same name with
"DoCmd.TransferText".

Everything went fine until I came up with database with relations. Now
acces won't let me delete the table before importing new one. And if I
just import the new data, acces appends data to previous table instead
of replacing it.

So what should I do?

Yous,
Sirritys

 
Reply With Quote
 
 
 
 
Patrice
Guest
Posts: n/a
 
      12th Oct 2006
Not sure what is the exact goal of this update. My personal preference woudl
be likely to import the new table under a temporary name and then perform 3
SQL satements to update current data (updating those who are there,
inserting new ones, deleting old ones)...

Another option could be to delete/recreate the relations but what if the new
table is not consistent ?

--
Patruce

"Sirritys" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> Hi,
>
> I have a problem (again).
>
> I'm trying to delete table from database with VBA using
> "DoCmd.DeleteObject" before I import the table with same name with
> "DoCmd.TransferText".
>
> Everything went fine until I came up with database with relations. Now
> acces won't let me delete the table before importing new one. And if I
> just import the new data, acces appends data to previous table instead
> of replacing it.
>
> So what should I do?
>
> Yous,
> Sirritys
>



 
Reply With Quote
 
Van T. Dinh
Guest
Posts: n/a
 
      12th Oct 2006
Use VBA to delete the relations before deleting the Table.

For sample code, see http://www.tinyurl/kmlww

--
HTH
Van T. Dinh
MVP (Access)



"Sirritys" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I have a problem (again).
>
> I'm trying to delete table from database with VBA using
> "DoCmd.DeleteObject" before I import the table with same name with
> "DoCmd.TransferText".
>
> Everything went fine until I came up with database with relations. Now
> acces won't let me delete the table before importing new one. And if I
> just import the new data, acces appends data to previous table instead
> of replacing it.
>
> So what should I do?
>
> Yous,
> Sirritys
>



 
Reply With Quote
 
Sirritys
Guest
Posts: n/a
 
      12th Oct 2006
Hi,

The table is always consistent. But it seems like too much work to
delete/recreate relations every time.

About the other option: How could I execute such statements you
referred from my VBA code.

Lets assume I import data to table called "tmp" in my loop which goes
trough all tables (in separate .txt files) in 1 folder. So when program
enters this loop it should 1st create that "tmp" table and after that
update the existing table and finally delete this "tmp" table.

So where I'm lost is what would be code for that update (updating those
who are there,
inserting new ones, deleting old ones) part. I'm really a newbie with
SQL =)

Yours,
Sirritys

Patrice wrote:
> Not sure what is the exact goal of this update. My personal preference woudl
> be likely to import the new table under a temporary name and then perform 3
> SQL satements to update current data (updating those who are there,
> inserting new ones, deleting old ones)...
>
> Another option could be to delete/recreate the relations but what if the new
> table is not consistent ?


 
Reply With Quote
 
Sirritys
Guest
Posts: n/a
 
      12th Oct 2006
Can't open that url, and don't want to delete relations =)


Van T. Dinh wrote:
> Use VBA to delete the relations before deleting the Table.
>
> For sample code, see http://www.tinyurl/kmlww


 
Reply With Quote
 
Van T. Dinh
Guest
Posts: n/a
 
      12th Oct 2006
Sorry, the correct link is:

http://www.tinyurl.com/kmlww

If you son't want to delete the relations then you won't be able to delete
the Table. In this case, you probably want to delete ALL existing records
from the Table and then append the new records to the now empty Table.

--
HTH
Van T. Dinh
MVP (Access)




"Sirritys" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can't open that url, and don't want to delete relations =)
>
>



 
Reply With Quote
 
Sirritys
Guest
Posts: n/a
 
      12th Oct 2006
Hi,

This would be just what I should do.
The thing is I don't know how to do it.

I got this example from someone on the group:

CUrrentDb.Execute "DELETE * FROM MyTable", dbFailOnError

The thing is that the command is a string, as I would need to use a
variable containing a table name. (ie. "MyTable" stored in string
variable).

Can I do that somehow ?




> In this case, you probably want to delete ALL existing records
> from the Table and then append the new records to the now empty Table.


 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      12th Oct 2006
This would be basically something like (air code) :

1)

UPDATE MyTable SET MyTable.a=tmp.A,MyTable.B=Tmp.b etc... FROM MayTable
INNER JOIN tmp ON MyTable.Key=tmp.Key

Records are matched on the key code and fields are updated.

2) INSERT INTO MyTable(a,b etc...) SELECT a,b.. FROM tmp WHERE NOT Key IN
(SELECT Key FROM MyTable)

Records are inserted if the key is not already found in the table

3)

DELETE FROM MyTable WHERE NOT Key IN (SELECT Key FROM tmp)

It will delete records in MyTable that are not in the tmp table.

Of course all updates are still subject to existing relations (i.e. if you
have related records and not delete cascade, you'll have to delete them, if
you insert and needs related records, you'll get an error if the relation is
not satisfied).

The exact purpose may help (this is to gather data in a readonly db ?)

--
Patrice

"Sirritys" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> Hi,
>
> The table is always consistent. But it seems like too much work to
> delete/recreate relations every time.
>
> About the other option: How could I execute such statements you
> referred from my VBA code.
>
> Lets assume I import data to table called "tmp" in my loop which goes
> trough all tables (in separate .txt files) in 1 folder. So when program
> enters this loop it should 1st create that "tmp" table and after that
> update the existing table and finally delete this "tmp" table.
>
> So where I'm lost is what would be code for that update (updating those
> who are there,
> inserting new ones, deleting old ones) part. I'm really a newbie with
> SQL =)
>
> Yours,
> Sirritys
>
> Patrice wrote:
>> Not sure what is the exact goal of this update. My personal preference
>> woudl
>> be likely to import the new table under a temporary name and then perform
>> 3
>> SQL satements to update current data (updating those who are there,
>> inserting new ones, deleting old ones)...
>>
>> Another option could be to delete/recreate the relations but what if the
>> new
>> table is not consistent ?

>



 
Reply With Quote
 
Van T. Dinh
Guest
Posts: n/a
 
      12th Oct 2006
CurrentDb.Execute "DELETE * FROM [" & StringVar & "]", dbFailOnError


--
HTH
Van T. Dinh
MVP (Access)



"Sirritys" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> This would be just what I should do.
> The thing is I don't know how to do it.
>
> I got this example from someone on the group:
>
> CUrrentDb.Execute "DELETE * FROM MyTable", dbFailOnError
>
> The thing is that the command is a string, as I would need to use a
> variable containing a table name. (ie. "MyTable" stored in string
> variable).
>
> Can I do that somehow ?
>
>
>
>
>> In this case, you probably want to delete ALL existing records
>> from the Table and then append the new records to the now empty Table.

>



 
Reply With Quote
 
Sirritys
Guest
Posts: n/a
 
      12th Oct 2006
THANKS!

I guess it works now.

Van T. Dinh wrote:
> CurrentDb.Execute "DELETE * FROM [" & StringVar & "]", dbFailOnError


 
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
How do delete queries work with tables that are related? =?Utf-8?B?S3Jpc3RpbmUgTQ==?= Microsoft Access Queries 1 8th Nov 2006 12:30 AM
Delete a current record from a form and related tables. =?Utf-8?B?Um9uIFdlYXZlcg==?= Microsoft Access Forms 2 27th Oct 2006 11:40 PM
how do i link dbf tables to acces? =?Utf-8?B?TGljYSBMVCBUdWZhcnU=?= Microsoft Access External Data 1 23rd Oct 2005 11:24 PM
foxpro 2.6 tables in acces Bakje herrie Microsoft Access External Data 0 21st Jul 2004 02:56 PM
HOWTO: Table, related tables, and only related data in a dataset? McGurk Microsoft ADO .NET 1 29th Apr 2004 06:17 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:09 PM.