run a function in another database to modif that db from "current

M

Mike

i have a database that auto archives data after 30 days. this will delete
tables and redords that are associated and older than 30 days. i found help
here
http://www.microsoft.com/office/com....xml&mid=baa461ea-860b-4f71-968e-63a464d4492d

now, what i want to do is this.

i have the functionallity to search and delete records and tables. i want
to delete all redords/tables older than 90 days in the archive database.
except i'd want this to run when i open the MAIN database, i.e start the
maind db, and have it clean out the archive database. is it possible to
accomplish in a simmilar maner as i did in teh post above?

thanks
 
J

Jeff Boyce

Mike

This may have already been covered ...

Unless your database is immense, you may not need to be "physically"
removing records in order to "archive" them.

An alternate approach is to add a field that indicates "archived", and
modify your queries, forms and reports to work with only those rows that are
not "archived".

If you only care THAT a row has been archived, use a Yes/No field. If you
care WHEN the row was archived, use a date/time field.

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
M

Mike

The requirements are for the archive database to remove records older than 90
days. now, rarely does anyone go into the archive database. if it was used
on a more regular scheduel, say every 30 days, i could do what i did with the
main db, just have it delete the records.

seeing as how this isn't the case, i was wondering if there was a way to
delete records from the archive database while running the main database,
i.e. not open the archive database and still have its contents be
systematically deleted.

Thanks
 
J

Jeff Boyce

I'll defer to other responders, as I don't have experience manipulating
database#2 from within database#1.

And I still wish to point out that there may not be a performance reason for
physically removing the records...

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
M

Mike

ok. and for the record, performance isn't an issue. it's just what was
asked to be done. when the boss says jump, you jump.
 
J

J. Goddard

Why would you want to delete/recreate tables all the time? Wouldn't just
deleting the records do? If you are deleting/adding tables all the
time, you might find the archive database gets large and requires
compacting.

For archiving and deleting records, why not just link to the tables in
the archive database, and the use

Insert into Archivedb Select from Activedb

You can then delete the links when you are done.

John
 
M

Mike

I'm doing this because it was requested. it was requested to keep the "main"
database relativly small, hence the move/deleteing of tables every 30 days.
however, the archive data becomes useless after the 90 days, hence the
deletion of them. I know it is rather silly. if i had it my way, i'd say
keep the main db small and never delete from the archive db. however, this
is not what was requeste. i've said this before. that i would like to do it
another way, however i'm met with resistance. plain and simple. if it's NOT
possible to do the deletion on the archive from the main db, please tell me.
don't respond saying what i'm doing is wrong or stupid or a waste of time.
if it can't be done, we wouldn't be here, and if it is possible, why not let
me know.

that's all i ask.

I appreaciate everyone taking the time to look over my question and respond.
but please if you can't help, just move on.

Thanks
 
J

J. Goddard

Hi Mike -

Don't know where my brain was on my last response; I must have left it
at the office.

Anyway, the answer to your question is yes it most certainly is possible
to do what you are looking to do. I have an application at the office
that creates tables in another database all the time - it just connects
to the database when it needs to. And strangely enough, the database I
connect to is an archive/backup database. Sigh....

I don't delete tables in the connected database, but obviously if I can
create them, I can delete them too.

I gather from your requirement that the actual name of a table in the
archive database gives some indication if what is in it - perhaps an
archive date or something.

I will get back to you with some code examples on Monday.

John
 
J

J. Goddard

Hi Mike -

I have attached an example bit of code which I use to work with a second
database. In my case, I do the same as you are looking to - use the
external database as an archive/backup. ( I also use it as a restore
facility to bail my users out of the Oh S**t! scenarios)

You will note (I hope, if you can decipher my code) that I also use a
table in the main database to index the archive tables, and that I use a
template table in the archive database, and just make copies of it.
(just for convenience, really - saved a bit of code).

I don't delete any archive tables (I should - they accumulate), but
using MS Access help I found that is can be done by something like this:

Dim db As database, dbname As String
dbname = "C:\data\msa_dat\db2.mdb"
Set db = DBEngine.Workspaces(0).OpenDatabase(dbname)
db.TableDefs.Delete "table1"
Set db = Nothing

I tested it - works fine.

Hope this helps

John


J. Goddard said:
Hi Mike -

Don't know where my brain was on my last response; I must have left it
at the office.

Anyway, the answer to your question is yes it most certainly is possible
to do what you are looking to do. I have an application at the office
that creates tables in another database all the time - it just connects
to the database when it needs to. And strangely enough, the database I
connect to is an archive/backup database. Sigh....

I don't delete tables in the connected database, but obviously if I can
create them, I can delete them too.

I gather from your requirement that the actual name of a table in the
archive database gives some indication if what is in it - perhaps an
archive date or something.

I will get back to you with some code examples on Monday.

John

Private Sub Save_Tasks_Click()

On Error GoTo Save_Error
Dim Wksp As Workspace, backupdb As Database
Dim datafiledirectory As String, Destination_Table As String
Dim Table_Name As String, rst As Recordset, db As Database
Dim next_sequence As Integer, Record_Count As Integer
Dim count_sql As String, K As Integer, SQL As String
Dim Records_Saved As Variant

'
' Open the backups database as well
'
backup_database = "C:\msa_dat\oa_backups.mdb"

Set Wksp = DBEngine.Workspaces(0)
Set db = CurrentDb
Set backupdb = Wksp.OpenDatabase(backup_database)

'
' From the backups table, get the next sequence number
'
count_sql = "SELECT COUNT (*) AS RECORD_COUNT FROM [BACKUP LIST] WHERE STUDY = '" & Forms![main menu form].LASTPROJ & "' AND T_S_K = 'T';"
Set rst = db.OpenRecordset(count_sql)
If rst!Record_Count = 0 Then
next_sequence = 1
Else
count_sql = "SELECT MAX([BACKUP NUMBER]) AS CNT FROM [BACKUP LIST] WHERE STUDY = '" & Forms![main menu form].LASTPROJ & _
"' AND T_S_K = 'T';"
Set rst = db.OpenRecordset(count_sql)
rst.MoveFirst
next_sequence = rst!cnt + 1
End If
Forms![please wait].Visible = True
Forms![please wait].SetFocus
Forms![please wait]!label3.Caption = "Backing Up Tasks..."
Forms![please wait].Repaint
DoCmd.Hourglass True
'
' Copy the TASK data and instructions into the local tables template1 and template2
'
K = clear_table("template1", ErrMsg)
K = clear_table("template2", ErrMsg)
SQL = "INSERT INTO template1 SELECT * FROM TASKS"
db.Execute SQL

'
' Make a backup copy of the TASK file in the backups database.
'
Table_Name = Forms![main menu form].LASTPROJ & "_T" & next_sequence
DoCmd.CopyObject backup_database, Table_Name, acTable, "tks_master"

'
' Now attach the table we just created as a linked table, so SQL insert can be used
'
DoCmd.TransferDatabase acLink, "Microsoft Access", backup_database, acTable, Table_Name, "x"
SQL = "INSERT INTO X SELECT * FROM TEMPLATE1"
db.Execute SQL
DoCmd.DeleteObject A_TABLE, "X"

'
' Add a record in the backups table, listing this backup
'
Set rst = db.OpenRecordset("backup list", dbOpenDynaset)
With rst
.AddNew
![backup number] = next_sequence
!study = Forms![main menu form]!LASTPROJ
!t_s_k = "T"
![backup date] = Date
![backup time] = Time$
![backup table name] = Table_Name
.Update
.Close
End With
'===============
'
' Make a backup copy of the Duty file in the backups database.
'
Table_Name = Forms![main menu form].LASTPROJ & "_DT" & next_sequence
DoCmd.CopyObject backup_database, Table_Name, acTable, "dutymaster"
'
' Now attach the table we just created as a linked table, so SQL insert can be used
'
DoCmd.TransferDatabase acLink, "Microsoft Access", backup_database, acTable, Table_Name, "x"
SQL = "INSERT INTO X SELECT * FROM duty"
db.Execute SQL
DoCmd.DeleteObject A_TABLE, "X"
'===============

'
' Save the tasks data back to the network directories
'
Forms![please wait]!label3.Caption = "Saving Tasks to Network..."
Forms![please wait].Repaint
datafiledirectory = Trim(Forms![main menu form].[full path name])
'
' Attach the two files for saving, data and duties
'
DoCmd.TransferDatabase acLink, "Microsoft Access", datafiledirectory, acTable, "TASKS", "SAVE DATA"
DoCmd.TransferDatabase acLink, "Microsoft Access", datafiledirectory, acTable, "DUTY", "SAVE DUTY"
'
' Clear the data from these two tables
'
K = clear_table("SAVE DATA", ErrMsg)
K = clear_table("SAVE DUTY", ErrMsg)

SQL = "INSERT INTO [SAVE DATA] SELECT * FROM TASKS"
db.Execute SQL
Records_Saved = db.RecordsAffected

SQL = "INSERT INTO [SAVE DUTY] SELECT * FROM [DUTY]"
db.Execute SQL
'
' Un-attach the two tables for saving
'
DoCmd.DeleteObject acTable, "SAVE DATA"
DoCmd.DeleteObject acTable, "SAVE DUTY"


'
' Update the control markers on the main menu form
'

Forms![main menu form].SAVED_T = True
Forms![main menu form].SAVEDATE_T = Date
Forms![main menu form].SAVETIME_T = Time$

Me![task saved] = True

'
' update the control table
'
control_table.MoveFirst
control_table.Edit
control_table!COUNT_T = Forms![main menu form].COUNT_T
control_table!SAVEDATE_T = Date
control_table!SAVETIME_T = Time$
If save_user_fields Then control_table!SAVEUSER_T = GetUserID(" ")
control_table.Update
'
' Add a record to the operations log database
'
Add_Log_record "Tasks", "Save", Records_Saved

Forms![please wait].Visible = False
DoCmd.Hourglass False

disable_t = -1
save_done = 1
Exit Sub

Save_Error:
If Err.Number = 3027 Or Err.Number = 3073 Then
FormattedMsgBox "Database is read only!@The network database is marked as read only - no updates can be made.@However, a backup copy of the current data has been made on your local C: drive"
Forms![please wait].Visible = False
DoCmd.Hourglass False
'
' Un-attach the three tables for saving
'
DoCmd.DeleteObject acTable, "SAVE DATA"
DoCmd.DeleteObject acTable, "SAVE DUTY"

Else
FormattedMsgBox "Error Number " & Err.Number & "@" & Err.Description & "@", vbCritical, "Error during attempted save"
Forms![please wait].Visible = False
DoCmd.Hourglass False
End If

End Sub
 
Top