On Click Event

Code:
Private Sub Command80_Click()

DoCmd.TransferDatabase acExport, "Microsoft Access", "D:\temp\my_data.mdb",  acQuery, "Q_Books", "q_test", True

End Sub

This code almost does what I need it to, but here is a problem I run into with this,,,,a would need the code to take the customer# from the textbox on the form(Called cust#) and export any query, table or form that begins with the customer #. For example lets say the cust# is JJ1234.

I would need the code to ONLY export tables/forms/quereis that begin with JJ1234. Can VB use a wildcard something like:
Code:
DoCmd.TransferDatabase acExport, "Microsoft Access", "D:\temp\my_data.mdb",  acQuery, "cust#", "cust#", True
 
I think this is what you want:

Code:
    'WARNING: Code deletes tables, queries and forms!
    'only test on a copy of the database
    Dim sDB as String, sCustNo, sMSA As String
    sMSA = "Microsoft Access
    sDB = "D:\temp\my_data.mdb"
    sCustNo = "xxxxxxxxxxxx" 'Code to assign customer number needed
    Dim obj As AccessObject
    For Each obj In Application.CurrentData.AllTables
        If InStr(obj.Name, sCustNo) Then
            DoCmd.TransferDatabase acExport, sMSA, sDB, acTable, obj.Name, obj.Name, True
            'DoCmd.DeleteObject acTable, obj.Name
        End If
    Next obj
    For Each obj In Application.CurrentData.AllForms
        If InStr(obj.Name, sCustNo) Then
            DoCmd.TransferDatabase acExport, sMSA, sDB, acForm, obj.Name, obj.Name, True
            'DoCmd.DeleteObject acForm, obj.Name
        End If
    Next obj
    For Each obj In Application.CurrentData.AllQueries
        If InStr(obj.Name, sCustNo) Then
            DoCmd.TransferDatabase acExport, sMSA, sDB, acQuery, obj.Name, obj.Name, True
            'DoCmd.DeleteObject acQuery, obj.Name
        End If
    Next obj

I'm hoping deleting the object won't screw up the For loop.
I would only un-comment the deleteobject lines when you're sure it's exporting the correct objects.
Some error handling would be wise too

I've not got a database I care to test it on :rolleyes:
 
Last edited:
Welcome to the Twilight Zone, boys and girls.

It does not follow that just because someone can do something, that they should necessarily do it.
 
I added this line of code to my db, it debugs fine, but when I go to execute the code I get this message....

"Object doesn't support this property or method"

Is this because there is a VB reference that needs to be installed that I haven't or did I leave a line of code out????
 
Welcome to the Twilight Zone, boys and girls.

It does not follow that just because someone can do something, that they should necessarily do it.

Yeah, I feel like I'm handing a sledge hammer to someone who insists on knocking down an interior load-bearing wall by hand.

I should at least give Jo a hard hat:



This is the code I think you want, with error handling and requires user to confirm deletion:
Code:
Option Compare Database
Option Explicit
 
Private Const MS_ACCESS As String = "Microsoft Access"
Private Const XPORT_SUCCESS As String = " exported successfully"
Private Const FINAL_CHANCE As String = "Final Chance to Say No"
 
Public Function ExportAndDeleteCustomerObjects(ByVal sCustNo As String, Optional ByVal sExternalDB As String = "D:\temp\my_data.mdb") As Boolean
    'WARNING: Code deletes tables, queries and forms!
    'only test on a copy of the database
    'returns true if no errors encountered
    On Error GoTo Err_ExportAndDeleteCustomerObjects
    If Len(sCustNo) > 0 Then
        Dim obj As AccessObject
        Dim sTable As String, sForm As String, sQuery As String
        sTable = ""
        sForm = ""
        sQuery = ""
        For Each obj In Application.CurrentData.AllTables
            If Left(obj.Name, Len(sCustNo)) = sCustNo Then
                sTable = obj.Name
                DoCmd.TransferDatabase acExport, MS_ACCESS, sExternalDB, acTable, sTable, sTable, True
                MsgBox sTable & XPORT_SUCCESS, vbInformation
                Exit For
            End If
        Next obj
        If sTable = "" Then MsgBox "No appropriate table found!", vbInformation
        For Each obj In Application.CurrentData.AllForms
            If Left(obj.Name, Len(sCustNo)) = sCustNo Then
                sForm = obj.Name
                DoCmd.TransferDatabase acExport, MS_ACCESS, sExternalDB, acForm, sForm, sForm, True
                MsgBox sForm & XPORT_SUCCESS, vbInformation
                Exit For
            End If
        Next obj
        If sForm = "" Then MsgBox "No appropriate form found!", vbInformation
        For Each obj In Application.CurrentData.AllQueries
            If Left(obj.Name, Len(sCustNo)) = sCustNo Then
                sQuery = obj.Name
                DoCmd.TransferDatabase acExport, MS_ACCESS, sExternalDB, acQuery, sQuery, sQuery, True
                MsgBox sQuery & XPORT_SUCCESS, vbInformation
                Exit For
            End If
        Next obj
        If sQuery = "" Then MsgBox "No appropriate query found!", vbInformation
        If MsgBox("Access is ready to delete those objects exported." & vbCrLf & "Are you sure you wish to proceed?" & "THIS STEP IS IRREVERSIBLE!", vbYesNo Or vbExclamation, "Permanently Delete Objects") = vbYes Then
            If sTable <> "" Then
                If MsgBox("Delete " & sTable & "?", vbYesNo, FINAL_CHANCE) = vbYes Then
                    DoCmd.DeleteObject acTable, sTable
                    MsgBox sTable & " deleted!", vbInformation
                End If
            End If
            If sForm <> "" Then
                If MsgBox("Delete " & sForm & "?", vbYesNo, FINAL_CHANCE) = vbYes Then
                    DoCmd.DeleteObject acForm, sForm
                    MsgBox sForm & " deleted!", vbInformation
                End If
            End If
            If sQuery <> "" Then
                If MsgBox("Delete " & sQuery & "?", vbYesNo, FINAL_CHANCE) = vbYes Then
                    DoCmd.DeleteObject acQuery, sQuery
                    MsgBox sQuery & " deleted!", vbInformation
                End If
            End If
        End If
    End If
    ExportAndDeleteCustomerObjects = True
    Exit Function
Err_ExportAndDeleteCustomerObjects:
    MsgBox "Error encountered trying to export" & vbCrLf & "Description given was:" & vbCrLf & Err.Description, vbCritical
    ExportAndDeleteCustomerObjects = False
    Exit Function
End Function

The one thing it's lacking is testing the export worked correctly.
I think you might have to put in lines that import what was exported as temporary objects and check they exist before deleting them and the original objects. Sheeesh I don't like this one bit.

As before, I've not tested that function.

You should test it thoroughly on a database that doesn't matter if it loses every table, query and form.
 
Last edited:
PS: There are hidden system tables in Access. Have a look at their names and make sure sCustNo is never like them. Deleting them would be bad (although I doubt it would let you). sCustNo should be a very unique string that only the table, query and form you want have names starting with it.
 
I input the Function that you wrote, but when I try to call the function as my onclick event, it gives me the Compile Error: Argument Not Optional.

Code:
Private Sub Command8_Click()
Call ExportAndDeleteCustomerObjects
End Sub

There may be other parameters needed in the Call Request, but this is also the most advanced function I Have ever dealt with, and am just trying to call it like I would a normal Function, hence why the thought of more parameters being needed causing the error. (Or I could be completely out in left field!)
 
Last edited:
@VilaRestal

Well then, now that you've built the handgranade, tell the voluntary victim how to pull the pin out :D
 
Yes, let me know how to "pull the pin." That is a funny analogy to use, spikepl!
 
The first argument is the string that the table, query and form's name will start with. The contents of the textbox you mentioned I guess. The second argument is optional and is for specifying a different database to export to.

It's very simple to use. It's basically going to try to export the first table, form and query (each) whos name begins with the string argument you pass it. Then it will ask if you want to delete the ones it finds.

As I keep saying, test it in a copy.

Also, read it and understand it. You shouldn't use until you do.
 
Can I ask one last question? (Then I promise I won't try to persuade you not to do this anymore. It is only a database after all. So what if it gets bloated and corrupted and loses data?)

How often would this process take place? How many of these export/delete operations will be done every week?

As I said, there's an absolute maximum of 1000 forms the external database can receive (assuming the forms have modules) and it will probably break down at less than half that.

You might end up having to manually archive the archives out of the archive database (:s) just to keep it running and that would kind of defeat the objective of having an automated archive system (as I'll call it to make it sound sensible, which, as we've repeatedly said, it isn't). Something to bear in mind if you're going to be doing 10 or more exports a week. (Better to just do them manually in the first instance.)
 
And thankyou for a new experience for me: It's the first time I've written code that I hope never gets used :D
I'm the Oppenheimer of Access: I have become the destroyer of databases
 
I understand what the function is doing, it is IMO actually quite a simple yet intricate code that performs all of the requested things I need done (at least temporarily). My only questions is how to call it? I have only worked with calling functions where inputting Call *Input the name of the function* works, and in this instance it didn't, so I am not sure how to get this function to execute in the code.

There will probably be about 5 exports done weekly, so 20 a month, and ideally come tomorrow afternoon, this db will have a "band-aid" on it long enough for me to have the "new" db designed and operational (tentatively Monday of next week).

And I am glad I could be your 1st! haha!!

Again, my last remaining ? is how to actually call the function. I understand the coding, what it does, and how it is doing it, I am not seeing understanding how to call it?!??
 
Just looked at this tread for the first time. Without a doubt the OP's request is the most bizarre and unlikely goal I have ever seen on this forum.

If this is a quick fix then the database structure is undoubtedly desperately ill.
 
Galaxiom -- the current db structure is deathly ill. It is crashing some days, 15 - 20 times a day, thus making backups be performed every 30 minutes (SUCH a waste!) and when the db crashes, having to revert to a backup, which again wastes time! As I stated a few posts earlier, I am solely looking for a temporary "band-aid!"
 
Galaxiom -- the current db structure is deathly ill. It is crashing some days, 15 - 20 times a day, thus making backups be performed every 30 minutes (SUCH a waste!) and when the db crashes, having to revert to a backup, which again wastes time! As I stated a few posts earlier, I am solely looking for a temporary "band-aid!"

That must be an absolute pain for everyone.:eek:

I bet you are looking forward to the day when you can give it a lethal injection.
 
Yes, it has been on my "death row" list since I took this position!!! If I can figure out how to call the function posted by VilaRestal I will be able to implement this "band-aid" tomorrow and have a new, fresh, clean, designed correctly db working next week!!!!
 
This would be incorrect:
Code:
Application.CurrentData.AllForms

AllForms is not in CurrentData but CurrentProject
Code:
CurrentProject.AllForms

I'm glad the first one didn't work.
Using InStr would have removes any account that is a substring of the target.:eek:
 
1 crash every 12 months is bad.
30 crashes a day are good; it’s easier to reproduce.

1.
I do not believe that faulty data structure will cause a crash only 30 times a day.
I do believe that faulty data structure, that can cause a crash, will cause a crash each time.
It should follow that, if the data structure is the fault, then that data structure is only being used 30 times a day.

2.
I do not believe that faulty code will cause a crash only 30 times a day.
I do believe that faulty code, that can cause a crash, will cause a crash each time.
It should follow that, if the code is the fault, then that code is only being used 30 times a day.

3.
The fault may be data driven.
The data may change in such a way that it causes 30 crashes a day.
(Transient things like unexpected Nulls or ZLS etcetera can be a problem.)

4.
Talk to the users; ask them to reproduce the fault.
Once you know how to create the fault you have a start.
Make a backup of the faulty database, do not change it before the backup.
Make a backup of the backup and work on that.

5.
Do a compile.
Do a compact and repair.
Do a decompile/compile/compact and repair.

6.
Try to reproduce the fault the user stated.
Does it crash?
What error message?
Check references, etcetera.

7.
If a Query is involved, try running the Query from the database window.
If code is involved, try running the code directly.
If a Form is involved, try opening the Form directly.
If a Report is involved, try opening the Report directly.
If a Macro is involved, good luck.

8.
Collate time spent on fixes verses band-aids that may never be finished.
To paraphrase the joke about Marie Antoinette at the guillotine:
Marie!, Marie! we have this fix for you;
ah!…just drop it in the basket and I’ll read it later.

9.
Divide and conquer.
Fix the problem; do not settle for a band-aid.
If one band-aid fixes the problem… it’s still a band-aid.


Chris.
 

Users who are viewing this thread

Back
Top Bottom