Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database does not open 1

Status
Not open for further replies.

labanTek

Instructor
Dec 3, 2004
37
Hi the following code does not open my access database can anyone help? Thanks in advance.

Dim myAccess As Access.Application
Set myAccess = CreateObject(, "Access.Application")
myAccess.OpenCurrentDatabase ("C:\test.mdb")
 
Would this help?

Code:
Set myAccess = CreateObject("Access.Application")?
 
labanTek

This

Set myAccess = GetObject(, "Access.Application")

grabs the Access instance if there is one already running. The other creates a new one

You could check first if there is one running and if not create one. The Sub myAccessRunning helps get an instance and know the result

Code:
Public myAccess As Object
Public Const ERR_APP_NOTRUNNING As Long = 429

Sub myAccessRunning() As Boolean
    On Error Resume Next
    myAccessRunning = True
    Set myAccess = GetObject(, "Access.Application")
    If Err = ERR_APP_NOTRUNNING Then
        Set myAccess = CreateObject("Access.Application")
        myAccessRunning= False    
    End If
    On Error GoTo 0
End Sub
 
JerryKlmns, as we are in the Microsoft: Access Modules (VBA Coding) forum it's much likely to have a running access with your code ...
 
labanTek, you may have confused 2 different
methods, in your approach.
As PHV said, there's a redundancy in using CreateObject.
Even though Jerry's suggestion, is very efficient
when such a method is required.

This may be what you want,

Dim DBase As DAO.Database
Dim strPath As String


If Dir("G:\DATA", vbDirectory) <> "" Then
strPath = "G:\DATA\FSXBE.mdb"
Else
strPath = "C:\Documents and Settings\Dan\My Documents\BackUp\DBase\FSX\FSXBE.mdb"
End If

Set DBase = DBEngine.Workspaces(0).OpenDatabase(strPath)

but personally, I'm trying to eliminate DAO in any of my coding.
Anticipating its extinction, in the near future.

Lately I've been working with ADOX,

Dim strConnection As String
Dim strPath As String
Dim cat As New ADOX.Catalog


strPath = "Data Source='" & _
CurrentProject.Connection.Path & "\FSXBE.mdb';"
End If

strConnection = "Provider='Microsoft.JET.OLEDB.4.0';"

cat.ActiveConnection = strConnection & strPath
 
PHV
Indeed it's much likely to have a running access in this forum but it might not, too!

I was to post only the first two lines, but then thought there are times where some extra tips or a nice sub to do a little job is not there and then someone (not neccesserily the OP), asks just that! And that efficient suggestion that Zion7 mentions is knowledge gathered from posts in Tek-Tips. My intention was to share a bit more.

PHV, don't take this reply as I was offended.

labanTek
Sorry for taking space in your post for the above.
 
Thanks all for the suggestions, but the code given above by Zion7 gives errors that says 'User defined typr not defined' this error is prompted on both 'Dim DBase As DAO.Database' and 'Dim cat As New ADOX.Catalog'. Any help on this will be greatly appreciated.
 
You need references to Microsoft DAO 3.# Object Library and Microsoft ADO EXt. 2.# for DDL and Security.

We just know that you wish to open another instance of Access, but we don't know what you wish to do with it. With that information, we could perhaps give better advice.

Roy-Vidar
 
Thank-you Roy,
labantek, outside of the reference issue,
there are a couple of "typos", in my ADOX code.
...I removed the "end if", and changed Dbase name.

Dim strConnection As String
Dim strPath As String
Dim cat As New ADOX.Catalog

strPath = "Data Source='" & _
CurrentProject.Connection.Path & "\Test.mdb';"

strConnection = "Provider='Microsoft.JET.OLEDB.4.0';"

cat.ActiveConnection = strConnection & strPath

And as Roy said, there are a few other ways to open a database, my way may be very well useless,
if you need to work with the GUI directly, for example.

Could you elaborate on your objective,
data manipulation, schema manipulation, recordset retrieval...

Thx!

 
Hi again, what I'm doing is that when the user exits the database app using a command button, it closes it and unknowing to the user, it opens a form on another database. When the user click this 'Exit' form the original app installation is deleted. The Hyperlink function does not fire the Open event of this 'Exit' form, i need this to fire so as to run my code that hides the access database window of the newely opened database. Thanks in advance for all the help you guys have givrn.
 
That's very different.
I don't believe my code will suffice?
At least I don't believe ADO/DAO, will open a
particular form, in any database?

can you put the "Exit" form, in your start-up options,
from the other DBase?

In that case FollowHyperlink or Shell... ?
 
...after a little more thought, I'm assuming it is your
start-up form, you just want to minimize the window.

Got this idea, from Tek-tips

Dim App as String, File As String, x As Integer
'works
App = Chr(34) & "C:\Program Files\Microsoft Office\OFFICE11\Msaccess.exe" & Chr(34)
File = Chr(34) & "C:\Documents and Settings\Dan\My Documents\ListView.mdb" & Chr(34)


x = Shell(App & " " & File, 2)

plaY WITH the shell constants, to get the right window state.(look up in Access help)
 
This is strange - I just tested this, using Shell, ShellExecute API and the hyperfollowthinge, and as long as the form was specified in the Tools | Startup thingie, the on open event of it fired - that is, unless the db was alredy open, in which case the form didn't open, of course.

You are sure the event doesn't fire? And that it's not just the window hiding stuff which is acting unpredictable?

Roy-Vidar
 
Ok I'll try Zion7's code and I will take a look again at my code to hide the access database window. Thanks again to all that contributed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top