Link to home
Start Free TrialLog in
Avatar of rogerdjr
rogerdjrFlag for United States of America

asked on

Access 2013 - Opening the last form or table that was accessed on start-up

I have an access 2013 database that is opening the last form or table that was accessed when it starts-up

I have a start-up form that runs a vba app to create a table on start-up then it closes the start-up form and opens a master form.

The start-up form is listed under the Display form in Current Database Options

code in the start-up form is


Private Sub Form_Close()
    DoCmd.OpenForm "0_MasterDataFrm", acNormal
End Sub

Private Sub Form_Open(Cancel As Integer)
    CreateTableFunction
    DoCmd.Close acForm, "0_STARTUP"

End Sub

Thanks
Avatar of PatHartman
PatHartman
Flag of United States of America image

I don't see a question.

If the question is "how do I open on startup the last FORM accessed before the app was closed last time?" then --

You will need to add a new table (preferably in the BE) to log each Form as it opens.  You will also need to capture the username as he logs in.  You can't do anything with tables since tables do not offer any events and users should NEVER, EVER access tables or queries directly anyway.  As each form opens, it should update the record of a particular user in the log table to record that this user opened this form.  The code needs to be able to add a log record if one doesn't already exist for the user.  You can call this logging code from the Open event of each form.  You would pass in the name of the form.

Call MyLogger(Me.Name)

The reason I prefer the table to be in the BE rather the FE is because when you replace the FE to distribute updates, the table with the last Form for this user will be wiped out and replaced with the copy from your test FE.

Then instead of
    DoCmd.OpenForm "0_MasterDataFrm", acNormal
you would use
    DoCmd.OpenForm  Nz(DLookup("FormName", "tblLogUsage", "UserName = '" & loggedinusername & "'"), "0_MasterDataFrm"), acNormal


Creating a table every time the app opens sounds like you think you are working with a spreadsheet rather than with a relational database.

Also, rather than hardcoding the form name in expressions such as
DoCmd.Close acForm, "0_STARTUP"
Use a reference to the name of the current form
DoCmd.Close acForm, Me.Name

This is safer because
a) if you copy and paste the code, you won't have to remember to change the form name.
b) if you rename the form, the code won't break.  It will simply pick up the new form name.
c) if you create dummy forms that you copy whenever you create a new form so you don't have to keep redefining the wheel, the code references don't have to change each time.  I have several standard forms with headers/footers, font, color, standard buttons, standard procedures (open, close, BeforeUpdate) that include standard code snippets.
Avatar of rogerdjr

ASKER

The issue actually is that the last table or form that was open when I closed the database re-opens when I start-up.

I don't want this to happen.

Seems to be unique to this particular application - other databases I'm working don't do this?
There is an option to open the last used database when Access starts (Access Options/Client Settings/Advanced) but none to open the last used object that I have ever seen.

On the Access Options/Current Database sheet, you have the option to specify a startup form.
It seems to be something strange about this database

Please don't judge the content too harshly - I'm an amateur and I only use this as a working tool to learn and deliver results (reports) for clients

Thanks
Is there a macro running?  Look for something named AutoExec.  Did you ever create a macro or attempt to write code to perform this automatic opening?

Try opening the database while holding down the shift key.  This stops the startup processes from running.
The only thing I can think is if the spell check process I use on some objects might be an issue a typical example would be

Private Sub IDandDescripofInspStructures_Exit(Cancel As Integer)
    'Private Sub GeneralLocation_Exit(Cancel As Integer)
        On Error Resume Next

        If Nz(IDandDescripofInspStructures.Text, "") = "" Then
            Exit Sub
        Else
            IDandDescripofInspStructures.SetFocus
            IDandDescripofInspStructures.SelStart = 0
            IDandDescripofInspStructures.SelLength = Len(IDandDescripofInspStructures.Text)
            DoCmd.SetWarnings False
            DoCmd.RunCommand acCmdSpelling
            DoCmd.SetWarnings True
        End If
End Sub
You may have to post the database.  Please compact it and zip it first.
See attached zipped database

I had some computer hardware issues that delayed my response
J--Projects--Accessibility---CASp-Librar
You seem to have disabled the shift-bypass key so I can't open the database and whatever code you have running is in a loop and so the database doesn't ever open.
If I disabled the shift-bypass key it was not intentional - how would I re-enable it?
Can you open the database directly into design view by holding down the shift key?  If you can then there may be something wrong with the file you uploaded.  Please try compacting and zipping and upload again.
it Opened fine in the design mode with shift key pressed

I compacted and re-zipped - see attached file

The last time i opened it the CASp_ImplementationPlan_SummaryDataTbl opened and access tried to spell check it. That was the last table I had opened when I was working on the database and closed it.

Very strange - I have several databases that I use as working tools (regularly adding tables, forms, reports, etc.) and none of them have this going on????
J--Projects--Accessibility---CASp-Librar
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks