On Click Event

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.)

Encountering unexpected quote marks is a very common cause. They really screw up lines of code.

Code problems with unhandled errors will put the VBA into Break mode when encountered in the full version of Access. They will terminate the application if it is hosted in the Runtime.

Consequently it would appear that the code does not include error handling where it matters most and the database is being used in Runtime. If it is in Runtime then the bandaid isn't going to work.

do not believe that faulty data structure will cause a crash only 30 times a day.
I should not have suggested the problems were due to "database structure" in my earlier post but "database construction". I am usually more careful with phrases that already have a well defined meaning.:o
 
Galaxiom - thank you for the insight. I changed all instances of
Code:
Application.CurrentData.AllForms
to this:
Code:
CurrentProject.AllForms

But still when I attempt to call this function using this code:
Code:
Call Call ExportAndDeleteCustomerObjects

I get an error that says Compile Error Argument Not Optional
Is this the incorrect way to call this type of Function?
 
'Data structure' was a general term I used and was not directed at post #35.

Chris.
 
Last edited:
Code:
Public Function ExportAndDeleteCustomerObjects(ByVal sCustNo As String, Optional ByVal sExternalDB As String = "D:\temp\my_data.mdb") As Boolean

The procedure is expecting the customer number as a string.

Use something like:

Code:
boolSuccess = ExportAndDeleteCustomerObjects strCustomerNumber
 
Use something like:

Code:
boolSuccess = ExportAndDeleteCustomerObjects strCustomerNumber
[/QUOTE]

I am not following what this code says at all?? Can you elaborate please?
 
boolSuccess is a variable that accepts the Booean return code from the funtion. I didn't look closely but I expect it indicates the function succeeded as expected. You can use this to report back to the user that the job was done.

The function needs to know the customer number you want to process and expects it as an argument. strCustomerNumber is a string variable that is used as the argument. It can come from anywhere such as a dialog box, a control on a form or a field in a query.

As ChrisO says though, you would be better off putting your effort into findng the real problem. It may be something quite trivial.

I can see disasters ensuing when you put this function in the hands of users. Users will misuse it in ways you cannot even yet imagine.
 
This code will be executed by one person, as the onclick event. They are the only user that can see this button. I think I now understand what you are meaning. If I add
Code:
boolSuccess = ExportAndDeleteCustomerObjects strCustomerNumber
To the beginning of the function, then once I call the function by using Call ExportAndDeleteCustomerObjects the function should execute? Is that correct?

Update: If I add this line of code, it tells me there is a syntax error?
 
That line should be in the button precedure. Of course you will have to load the variable with a string before it..

That line calls the function itself and puts the return code into the boolReturnCode variable.
 
It feel quite simple with regard to codes (may be a little complex for understanding logic behind it)!


anyhow solution is

DoCmd.TransferDatabase
DoCmd.DeleteObject


following are the code example, just fill the variables and add more codes as required

1. Assuming you dont want to transfer the FORM on what your pressing Done button (currently focused).

2. Assuming all objects [Form, Report, Query and etc] currently are closed as it will give an error on line where DoCmd.DeleteObject will execute, I think you will manage that

Code:
[B]Sub DoneButton_OnClick()[/B]
[B]DoCmd.TransferDatabase acExport, _[/B]
[B]"Microsoft Access", _[/B]
[B]strDestinationDatabasePath, acReport, strLocalReportName, _[/B]
[B]strTargetReportName[/B]
 
[B]DoCmd.TransferDatabase acExport, _[/B]
[B]"Microsoft Access", _[/B]
[B]strDestinationDatabasePath, acForm, strLocalFormName, _[/B]
[B]strTargetFormName[/B]
 
[B]DoCmd.TransferDatabase acExport, _[/B]
[B]"Microsoft Access", _[/B]
[B]strDestinationDatabasePath, acQuery, strLocalQueryName, _[/B]
[B]strTargetQueryName[/B]
 
[B]DoCmd.TransferDatabase acExport, _[/B]
[B]"Microsoft Access", _[/B]
[B]strDestinationDatabasePath, acTable, strLocalTableName, _[/B]
[B]strTargetTableName[/B]
 
[B]' so on....... add code for other objects[/B]
[B]'[/B]
[B]'[/B]
 
 
[B]'Once successfully transfer add code for deleting transfer objects[/B]
 
[B]DoCmd.DeleteObject acForm, strLocalFormName[/B]
 
[B]DoCmd.DeleteObject acReport, strLocalReportName[/B]
 
[B]DoCmd.DeleteObject acQuery, strLocalQuery[/B]
 
[B]DoCmd.DeleteObject acTable, strLocalTable[/B]
 
[B]' so on...... add code for other objects[/B]
[B]'[/B]
[B]'[/B]
[B]'[/B]
[B]End Sub[/B]

Note: If you want to transfer currently open form too then you have to close form and transfer control to modules and from modules execute codes.
 
@Jo:
You would use the function by:
If Not ExportAndDeleteCustomerObjects(strCustNo) Then
'Perhaps some code in response to the export failing
End If

strCustNo is the prefix to the object names you mentioned (the value of the textbox you mentioned I presume).
(Passing arguments to a function is very basic stuff so I'm concerned about you using this code not knowing about that)

@Chris:

The reason is it crashes 30 times a day is because the database creates a table, query and form every time a record is created. It gets bloated and corrupted. And, because of that 'system', compiling it is not an option.

@Jo again:
If I were you I would (if you don't already), at the start of each day, create a new database (empty) and import everything you need out of the main one, archive the main one and replace it with this new one. It should crash a little less often.
 
The reason is it crashes 30 times a day is because the database creates a table, query and form every time a record is created.

:eek: Now that really is utterly bizarre. Whoever constructed that knew too much about VBA and absolutely nothing about databases. No wonder the bandaid request was so strange.

Euthanasia is the only solution.
 
VilaRestal - that is a great idea, I hadn't even thought of starting fresh with a new db each day! As far as calling the function I will test that this afternoon (when I have a free moment to try it) by it looks like you are saying use an if/then statement beginning with this:
Code:
If Not ExportAndDeleteCustomerObjects(strCustNo) Then
Is that correct?
 
Yes, because the function returns a boolean it can be used as the comparison in an if statement.

I do think the manual approach is better.

Adding more code, and especially quite complex code that involves complex interactions with an external database, to fix a database crashing (because it's too complicated and prone to corruption) might just be making things worse: a case of cranial amputation to cure a migraine
 
Once many years ago. A young genius was given a task by a mad man. The simple task “” was to create a system that would manage every aspect of the home building process. $300,000 later they had created a .net program running on a single table on a MySQL database.

I went to work there as the head of the design team. In less than two months we had created the first working model in Access. It had about 60 tables including lookup tables and 1000 time less code.

And can you guess? It actually worked.

The moral, forget code until you have something that works using table and queries alone.

Code is mostly used to create the user interface, not to manage data. SQL is much better at that.


I will leave you to your misery now, too many others are trying to help this along.
My advise; learn how to trouble shoot first, and that means eliminate multiple variables and track down the cause to the root.
 
Once many years ago. A young genius was given a task by a mad man. The simple task “” was to create a system that would manage every aspect of the home building process. $300,000 later they had created a .net program running on a single table on a MySQL database.

That reminds me af a far worse tale of woe that aught to be read by every developer. It is quite entertaining.

http://www.simple-talk.com/opinion/opinion-pieces/bad-carma/
 
#56 Great story.

Inspired by it, and this thread, I get an idea for a:

database killer application (position the missing hyphen where suitable) :D
 
spikepl - Location: San Serriffe

Love it.:D:D:D
 
Just for craps and giggles I was trying to run the code that was posted, but it won't work for me. Strangely enough, now my curiosity is sparked as to how to get this code to work, as destructive as it is! Potentially this could be a sort of a "virus" to simply extract data from one persons database and transfer a copy into yours. Scary!

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
 

Users who are viewing this thread

Back
Top Bottom