Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Opening an Access from from VBA

Posted on 2013-11-20
7
Medium Priority
?
611 Views
Last Modified: 2013-11-22
I have some code that is run in Excel that opens an Access database.

It has been working well but I now want it to open a different database.

It works fine if the database is not open to start with, but if the database is open I get the error that

"the open form command is not available now".

Here is the code, I am running it in test mode so boTest is TRUE:

Sub OpenDatabaseForm()

Dim OrderDB As Database
Dim stForm As String
Dim intDbVersion As Integer
Dim accObj As Object

stErrorTitle = "Open Database Form"


    If boTest Then
        Set accObj = CreateObject("access.application")
        Set FSO = CreateObject("Scripting.FileSystemObject")
        If FSO.FileExists(Environ("Userprofile") & "\Desktop\TestOrderBookSQLFE.laccdb") Then
            AppActivate ("Microsoft Access - TestOrderBookSQLFE")
            If Not accObj Is Nothing Then
                With accObj
                    .docmd.openform "frm_orders", , , "[f1cSCOrder]= '" & stSCOrd & "'"
                    .docmd.openform "frmProcessOrders", , , "[f1cSCOrder]= '" & stSCOrd & "'"

                    .UserControl = True
                End With
            End If
        Else
            accObj.OpenCurrentDatabase Environ("UserProfile") & "\Desktop\TestOrderBookSQLFE.accdb"
        End If
    Else
        Set accObj = CreateObject("access.application")
        Set FSO = CreateObject("Scripting.FileSystemObject")
        If FSO.FileExists(Environ("Userprofile") & "\Desktop\TestOrderBookSQLFE.laccdb") Then
            AppActivate ("Microsoft Access - TestOrderBookSQLFE")
            If Not accObj Is Nothing Then
                With accObj
                    .docmd.openform "frm_orders", , , "[f1cSCOrder]= '" & stSCOrd & "'"
                    .docmd.openform "frmProcessOrders", , , "[f1cSCOrder]= '" & stSCOrd & "'"
                    .UserControl = True
                End With
            End If
        Else
            Set accObj = CreateObject("access.application")
            accObj.OpenCurrentDatabase Environ("UserProfile") & "\Desktop\TestOrderBookSQLFE.accdb"        
End If
    End If
         With accObj
            .docmd.openform "frm_Orders", , , "[f1cSCOrder]= '" & stSCOrd & "'"
            .docmd.openform "frmProcessOrders", , , "[f1cSCOrder]= '" & stSCOrd & "'"
            .UserControl = True
         End With


End Sub

Why won't the form open.  Could there be something in frm_orders prevent automatic opening like this? it does have procedures in the On Current and On Load events.

Thanks

LJM
0
Comment
Question by:LJKMartin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 39662628
I'd think when Access has it opened it may lock the file and you can't VBA to a locked file.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 39662679
in your code, if the Application is open, you are not setting the object accObj to point to the open application..

if the application is open, use the GetObject() function to set the accObj

set accObj=getObject(, "access.application")


see this for reference
http://msdn.microsoft.com/en-us/library/office/aa164798%28v=office.10%29.aspx
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 39663127
What exactly are you trying to do?  Opening a form via automation doesn't make any sense.  And opening a database that other people are using should never be done.  Remember, every user should have his own personal copy of the FE.  And if it turns out that you actually do need to automate the application, you should use a copy that is not being used by others.  They should never be shared.  Forms are intended to be interactive.  You don't use them in a batch process.  Are you trying to run some code from the form's class module or perhaps a query?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:LJKMartin
ID: 39664060
Hi Pat,

Yes, every user does have their own copy of the front-end and that's what I'm trying to open (hence the path to the file being the user's desktop).

The code imports data from Excel into the SQL back-end and then I want to open the form in order to show the imported data.

There is no process required in the form, I just want to open it and show the imported record.
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 39664125
Wouldn't it be simpler to automate this from Access?  Then you don't need to open Excel at all.  Just have Access link to the spreadsheet and run an append query to append the data to the linked SQL server table.  This would probably be more convenient for the user also.
0
 

Author Comment

by:LJKMartin
ID: 39664212
Nope,

The data is sent in via email and the user needs to open it to check before importing it to the database.  Which they do without having to save it first.
0
 

Author Closing Comment

by:LJKMartin
ID: 39668452
Thanks Capricorn
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question