🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.bazy-danych.msaccess
Article #137773

Re: Hyperlink do poszczególnych formularzy w VBA

#137773
From: Zbigniew Bratko
Date: Sun, 21 Mar 2021 15:12
173 lines
7148 bytes
W dniu 21.03.2021 o 13:46, Kali pisze:
> Witam
> Ten kod co mi wysłałeś to trochę za trudne jak dla mnie, jeszcze posiedzę ale to nie moja liga, trochę Zbyszek znasz moje możliwości.
> Myślałem, że coś jest prostszego – ścieżka do pliku i wymieniony konkretny formularz.
> Jak wcześniej mnie naprowadziłeś:
> CreateObject("Shell.Application").Open "C:\Users\User\Documents\2021.mdb"  + konkretny formularz np. Firmy
> Lub
> Application.FollowHyperlink ("C:\Users\User\Documents\2021.mdb") + konkretny formularz

Jakby to było takie proste, jak piszesz, to Dev Ashish by nie 
komplikował sprawy.

> 
> Ten kod trochę ponad moje możliwości, prawdopodobnie należy zamienić ten kod user64, a gdzie ściezka??? To za skomplikowane na moje możliwości ale dzięki jeszcze pomyśle.


Wywołanie funkcji jest następujące:

   fOpenRemoteForm "Dysk:\TwójFolder\TwojaBaza.accdb", "TwojFormularz", 
SW_NORMAL



Mój Access 16 w ver. 32-bit przyjął kod bez "zmrużenia  oka". Co prawda 
po wywołaniu występował błąd nr 91, ale dało się to naprawić.

Przetestuj poniższy kod z poziomu formularza:

Option Compare Database
Option Explicit
'************ Code Start *************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'

#If VBA7 Then
   Private Declare PtrSafe Function apiSetForegroundWindow Lib "user32" _
               Alias "SetForegroundWindow" _
               (ByVal hwnd As LongPtr) _
               As Long

   Private Declare PtrSafe Function apiShowWindow Lib "user32" _
               Alias "ShowWindow" _
               (ByVal hwnd As Long, _
               ByVal nCmdShow As LongPtr) _
               As Long
#Else
   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
#End If

Private Const SW_MAXIMIZE = 3
Private Const SW_NORMAL = 1

'_______________________________________________
'Otwarcie formularza innej bazy w widoku normalnym
Private Sub btnOpenForm_Click()

   fOpenRemoteForm "c:\tmp\aaa.accdb", "TwojFormularz", SW_NORMAL

End Sub

'___________________________________________
Function fOpenRemoteForm(strMDB As String, _
                         strForm As String, _
                         Optional intView As Variant) _
                         As Boolean
Dim objAccess As Access.Application
Dim lngRet As Long

     On Error GoTo fOpenRemoteForm_Err

     If IsMissing(intView) Then intView = acViewNormal

     If Len(Dir(strMDB)) > 0 Then
         Set objAccess = New Access.Application
         With objAccess
             lngRet = apiSetForegroundWindow(.hWndAccessApp)
             lngRet = apiShowWindow(.hWndAccessApp, SW_NORMAL)
             'the first call to ShowWindow doesn't seem to do anything
Rem         lngRet = apiShowWindow(.hWndAccessApp, SW_NORMAL)

             ' lub okno Accessa zmaksymalizowane
             lngRet = apiShowWindow(.hWndAccessApp, SW_MAXIMIZE)

             .OpenCurrentDatabase strMDB
             .DoCmd.OpenForm strForm, intView

             ' niestety, u mnie warunek Len(.CurrentDb.Name)
             ' powoduje błąd po zamknięciu otwartego Access
             '---------------------------
             'Runtime Error
             '---------------------------
             'Error#: 91

             'Object variable or With block variable not set
             '---------------------------
             'OK
             '---------------------------


Rem            Do While Len(.CurrentDb.Name) > 0
Rem                DoEvents
Rem            Loop

             ' Moja poprawka - przechwycenie błędów m.in. nr 91
             On Error Resume Next
             Do
                 IsObject (.CurrentDb.Name)
                 If Err.Number <> 0 Then
                   Err.Clear
                   Exit Do
                 End If
                 DoEvents
               Loop
             On Error GoTo 0
         End With
     End If

fOpenRemoteForm_Exit:
     On Error Resume Next
     objAccess.Quit
     Set objAccess = Nothing
     Exit Function
fOpenRemoteForm_Err:
     fOpenRemoteForm = False
     Select Case Err.Number
         Case 7866:
             'mdb is already exclusively opened
             MsgBox "The database you specified " & _
             vbCrLf & strMDB & vbCrLf & _
             "is currently open in exclusive mode.  " & vbCrLf _
             & vbCrLf & "Please reopen in shared mode and try again", _
             vbExclamation + vbOKOnly, "Could not open database."
         Case 2102:
             'form doesn't exist
             MsgBox "The Form '" & strForm & _
                   "' doesn't exist in the Database " _
                   & vbCrLf & strMDB, _
                   vbExclamation + vbOKOnly, "Form not found"
         Case 7952:
             'user closed mdb
             fOpenRemoteForm = True
         Case Else:
             MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description, _
                     vbCritical + vbOKOnly, "Runtime error"
     End Select
     Resume fOpenRemoteForm_Exit
End Function
'************ Code End *************

W razie kłopotów pisz co jest nie tak.


-- 
Pozdrawiam
Zbigniew Bratko
accdb.pl
gps.accdb.pl
Technical Details
Message-ID: <6057624c$0$537$65785112@news.neostrada.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!goblin2!goblin3!goblin.stu.neva.ru!sewer!alphared!news.uzoreto.com!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!newsfeed.neostrada.pl!unt-exc-01.news.neostrada.pl!unt-spo-b-01.news.neostrada.pl!news.neostrada.pl.POSTED!not-for-mail
References: <8727ce48-0764-4881-a622-ba0a95d67569n@googlegroups.com> <605613a2$0$536$65785112@news.neostrada.pl> <6f15dee5-28d9-4b9c-b53a-ef87db7e4536n@googlegroups.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!goblin2!goblin3!goblin.stu.neva.ru!sewer!alphared!news.uzoreto.com!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!newsfeed.neostrada.pl!unt-exc-01.news.neostrada.pl!unt-spo-b-01.news.neostrada.pl!news.neostrada.pl.POSTED!not-for-mail
Subject: Re: Hyperlink do poszczególnych formularzy w VBA
Newsgroups: pl.comp.bazy-danych.msaccess
References: <8727ce48-0764-4881-a622-ba0a95d67569n@googlegroups.com> <605613a2$0$536$65785112@news.neostrada.pl> <6f15dee5-28d9-4b9c-b53a-ef87db7e4536n@googlegroups.com>
From: Zbigniew Bratko <adminBEZTEGO@accdb.pl>
Date: Sun, 21 Mar 2021 16:12:11 +0100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1
MIME-Version: 1.0
In-Reply-To: <6f15dee5-28d9-4b9c-b53a-ef87db7e4536n@googlegroups.com>
Lines: 199
Message-ID: <6057624c$0$537$65785112@news.neostrada.pl>
Organization: Telekomunikacja Polska
NNTP-Posting-Host: 5.172.235.202
X-Trace: 1616339532 unt-rea-a-02.news.neostrada.pl 537 5.172.235.202:7704
X-Complaints-To: abuse@news.neostrada.pl
X-Received-Bytes: 7586
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit