Results 1 to 7 of 7
  1. #1
    twgonder is online now Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    652

    See if a Hwnd is active in Access

    I'm using Me.Hwnd in forms. It's how I keep track of certain things.


    One use is to place it in my record locking table, so if there are two FormCustomer running,
    one instance of a form can't start editing the record that another instance is currently editing (and has locked).

    The problem happens when there is an Access crash, and the lock doesn't get cleared.
    As part of the routine, I delete the lock record if it's the same user trying to get back into the record,
    but to work properly, I need to know if the Hwnd is currently active in another instance, in which case I don't want to delete the lock.
    (Not all forms are instanced, but I would need to check the Hwnd anyways, so one routine handles both situations.)

    Is there a way to see if Access (or Windows) has a certain Long Hwnd active?
    (my search in Google wasn't productive for this question)

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    loop through the forms collection

    Code:
    Dim frm As Variant
    
        For Each frm In CurrentProject.AllForms
    
            If CurrentProject.AllForms(frm.Name).IsLoaded Then
                Debug.Print Forms(frm.Name).Hwnd
            End If
        Next
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    twgonder is online now Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    652
    @ Moke123 post #2, I'll start with that, thanks. Should frm be a variant, or is it an object of some kind?

    Question after trying: I don't know what the form name would be (could be one of many) so how can I get around frm.Name?
    or should frm and frm.name be one variable?

    Sorry, still getting used to VBA syntax

  4. #4
    twgonder is online now Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    652

    Lots of runtime errors in this test

    I hacked together this from the suggestion and other code I had.
    It gets different runtime errors, when run from the immediate window with a couple of forms open.
    I'm not sure where the syntax errors are, or what can or can't be done.

    Code:
    Public Function fIsHwndActive(aHwnd As String) As Boolean
      '* Find if a Hwnd is currectly active
      Dim obj As Object
      Dim FrmNm
      Const nProc = "fIsHwndActive"
      
      fIsHwndActive = False
    
    
      For Each FrmNm In CurrentProject.AllForms
        Debug.Print CurrentProject.AllForms(FrmNm).Name
        If CurrentProject.AllForms(FrmNm).IsLoaded Then
          Debug.Print Forms(FrmNm).Hwnd
          If Forms(FrmNm).Hwnd = aHwnd Then fIsHwndActive = True
        End If
      Next
      
      If Not fIsHwndActive Then
        For Each obj In colOpenForm
          If obj.Hwnd = aHwnd Then
            fIsHwndActive = True
            Exit For
          End If
        Next
      End If
      Set obj = Nothing
    
    
    End Function

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    Code:
    Function IsHwndLoaded(aHwnd As Long) As Boolean
    Dim frm As Variant
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    
    
        For Each frm In CurrentProject.AllForms
    
    
            If CurrentProject.AllForms(frm.Name).IsLoaded Then
                'Debug.Print Forms(frm.Name).Hwnd
                dict.Add Forms(frm.Name).Hwnd, Forms(frm.Name).Hwnd
            End If
        Next
    
    
    IsHwndLoaded = dict.exists(aHwnd)
    
    
    End Function
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    twgonder is online now Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    652

    Still not working, problem with instanced forms

    @moke123 post#5 Okay, that seems more complete but I still get the same RTE if an instanced form is opened,
    when I say instanced form I mean the form was started with this from a menu program

    Code:
     Set frm = New Form_frm_4_Entity
    in which case the error looks like this when the instanced form is encountered in the loop

    Click image for larger version. 

Name:	230601Hwnd1.jpg 
Views:	16 
Size:	45.2 KB 
ID:	50297

    Which now brings up a bigger question that's been in other threads of mine.

    I experimented with "instanced" forms (using set as shown above).
    https://www.accessforums.net/showthr...118#post507118

    I fully support the idea of multiple versions of a form running at once, this has been explained extensively in this post:
    https://www.accessforums.net/showthr...148#post508148

    However, because using them (maybe incorrectly) has been causing me lots of problems like this RTE and others, one example is:
    https://www.accessforums.net/showthr...116#post508116

    Now I wonder, is anyone else trying to be a "rockstar" with a dead amp (the analogy is that MS created a faulty and useless way of using instancing).
    https://www.youtube.com/watch?v=GruwS4kHLKk

    The other problem I'm seeing with this thread's example, is that when moke123īs solution goes to RTE and End is clicked, all the instanced forms unceremoniously close down. Another example of how MS instancing is coming up woefully short.

    The yet unanswered question from long before, is anyone using instanced forms with success? If so, how (in a way that addresses the multitude of problems I've documented {yeah, I know, that's a bit much to ask in a forum like this, so I'll settle for a simple yes or no})?

    A summary of some of the problems I've encountered with "instanced" forms:

    • Access gets lost when code executes for one form, and the code result gets erroneously applied to another instanced form (bad workspace management).
    • Access smashes the variables of one instance with variables values from another instance.
    • Impossible to debug code in instanced forms because the debugger is unaware of the instanced situation, often reporting variables incorrectly, or skipping breakpoints in each instance.
    • Any kind of RTE closes all instances of running forms.
    • Code that works for forms being opened in the "regular manner" fails with instanced forms.
    • No way to use variables to initiate an instanced form, so lots of detailed "Set" lines have to be written instead of using one common routine with a variable for the form name.
    • MS seems to have no idea, in Access at least, of how to properly spawn a process with its own workspace, a computer concept that's been around for at least 60 years and which was the whole premise behind Windows itself (at least in a GUI environment).

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    I think your expectations of instanced forms is a little high. Just because you can do something doesn't necessarily mean you should. Take Multi-valued fields or attachment datatypes. You can use them but expect headaches when you do.

    As you know, I use instanced forms all the time in custom classes. Not because I want 2 of the same form open but to tightly control all the workings of the form.

    Edit: Now that I think about it, I'm not sure that an opened instance of a form is added to the forms collection which is why it is not found.

    You may need to use a windows api.
    Last edited by moke123; 06-02-2023 at 05:28 AM.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Access and Active Directory...
    By Voodeux2014 in forum Access
    Replies: 4
    Last Post: 01-19-2015, 02:31 PM
  2. Replies: 21
    Last Post: 01-21-2014, 05:04 PM
  3. Replies: 1
    Last Post: 01-11-2014, 12:39 PM
  4. Access 2010, add-ons and active-x, Possibilities?
    By justphilip2003 in forum Access
    Replies: 1
    Last Post: 04-05-2013, 03:34 PM
  5. access+Active directory
    By cpcp in forum Access
    Replies: 6
    Last Post: 11-15-2010, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums