Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2009
    Posts
    21

    Unanswered: Open a Form in another database

    I would like to know if it is possible to open a particular form within another database even though another menu may be selected to open upon startup. I want to be able to do this from another database.

  2. #2
    Join Date
    Mar 2009
    Posts
    5,441
    Provided Answers: 14
    Here is a solution:
    Code:
    Option Compare Database
    Option Explicit
    
    Private Declare Function apiSetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Const SW_MAXIMIZE = 3
    Private Const SW_NORMAL = 1
    
    Function OpenRemoteForm(MDBFile As String, FormName As String, Optional ViewMode As Long = acViewNormal)
    
        Dim appAccess As Access.Application
        
        If Len(Dir(MDBFile)) > 0 Then
            Set appAccess = New Access.Application
            With appAccess
                apiSetForegroundWindow .hWndAccessApp
                apiShowWindow .hWndAccessApp, SW_NORMAL
                .OpenCurrentDatabase MDBFile
                .DoCmd.OpenForm FormName, ViewMode
            End With
        End If
    
    End Function
    Have a nice day!

  3. #3
    Join Date
    Apr 2009
    Posts
    21

    Thanks

    Thanks for the code. Where should I put it for it to get the job done?

  4. #4
    Join Date
    Mar 2009
    Posts
    5,441
    Provided Answers: 14
    Phillip,

    You may place the code wherever you want: an independent module or the class module of a form. Personally I tend to avoid placing any code that's not directly related to a form in the class module of this form. The main advantage is that such a function can be called from anywhere in the application, but it's more of a programming style than anything else.

    Now let's suppose you have a button: Button_OpenRemoteForm on a form: F_AnyForm, the code could look something like this:

    In the class module of F_AnyForm:

    Code:
    Private Sub Button_OpenRemoteForm_Click()
    
        Dim strMDBFile as String
        Dim strFormName as String
    
        strMDBFile = "C:\Documents and Settings\SinnDHo\My documents\Access\dbforum\CascadingSubforms.mdb"
        strFormName = "Main_Form"
        OpenRemoteForm strMDBFile, strFormName
    
    End Sub
    In an independent module:

    Code:
    Function OpenRemoteForm(MDBFile As String, FormName As String, Optional ViewMode As Long = acViewNormal)
    
        Dim appAccess As Access.Application   
     
        If Len(Dir(MDBFile)) > 0 Then
            Set appAccess = New Access.Application
            With appAccess
                apiSetForegroundWindow .hWndAccessApp
                apiShowWindow .hWndAccessApp, SW_NORMAL
                .OpenCurrentDatabase MDBFile
                .DoCmd.OpenForm FormName, ViewMode
            End With
        End If
    
    End Function
    Don't forget to place the declarations and constants for the Windows API in the declaration section of the module.
    Have a nice day!

  5. #5
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    I personally would just use the arguements parameter when opening the mdb file. You can supply a "value" of a variable when opening an mdb using the appropriate parameter.

    Then I'd have code in the mdb which reads that parameter (it's 1 or 2 lines of code in your startup sequence) and open the appropriate form depending on the arguement value.

    I used to do this all the time when I wanted to open another mdb file and have it immediately go to a customerID. I'd pass the CustomerID as an arguement when opening the 2nd mdb file. It's only a couple lines of fairly easy coding (again, just using the arguement parameter.)
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •