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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object variable or with block not set 1

Status
Not open for further replies.

dpaulson

Programmer
May 7, 2000
347
CA
Hi, I have the following code in a module
Code:
Public Function CallingForm(ByVal hWnd As Long) As Form    
    'Return the Form that launched the given Window
    Dim lHwnd As Long
    Dim oForm As Form
    'get the Handle of the Window that Loaded this Window
    lHwnd = GetNextWindow(hWnd, GW_HWNDNEXT)
    'Find a match in the "Forms" collection
    'and return a reference to it
    For Each oForm In Forms
        If oForm.hWnd = lHwnd Then
            Set CallingForm = oForm
            Exit For
        End If
    Next
End Function
And in my project I use following snippet in form activate event
Code:
       If CallingForm(hWnd).Name = "frmMonthEndRoutine" Then    ' <==== error here
            If Month(CompanyYearEnd) + frmMonthEndRoutine.CurrentMonthEnd <= 12 Then
                For SEARCH = 0 To cboYear.ListCount - 1
                    If CLng(cboYear.List(SEARCH)) = CLng(frmMonthEndRoutine.cboYear.Text) - 1 Then
                        cboYear.Text = cboYear.List(SEARCH)
                        Exit For
                    End If
                Next SEARCH
            Else
Occasionally (while in the ide) I get a 'Object variable or with block not set' error. Doing some testing I find that Callingform(hWnd) is nothing, but if I press run again, it continues and produces no error. I just don't know what to do to fix this.
TIA

David Paulson

 
A bit of an update. I have verified that the 'frmMonthEndRoutine' is indeed loaded and listed in the forms collection, but I find that the 'lHwnd = GetNextWindow(hWnd, GW_HWNDNEXT)' returns the wrong value so it cannot be found in the forms collection.

David Paulson

 
> 'lHwnd = GetNextWindow(hWnd, GW_HWNDNEXT)' returns the wrong value

Erm ... it can't return the wrong value. It returns exactly what it is supposed to. Now, that what it returns may not be what you expect is a different matter.

Your own comment says "get the Handle of the Window that Loaded this Window" - and that is NOT what GetNextWindow does. GetNextWindow is simply an alias for GetWindow, which is documented here
 
I would tend to agree with you, but this is the 'dump' that I am getting.

frmmonthendroutine 'this is called in the frmmonthendroutine to show what forms are in the forms collection, as well as their handles, last number is forms.count
frmMainMenu 29819658 2
frmMonthEndRoutine 13371094 2

Callingform 'this is called in the module1 where this function resides. Function is called from the frmARPosting from the activate event
frmMainMenu 29819658 3
frmMonthEndRoutine 13371094 3
frmARPostings 13371116 3

hWnd - 13371116 'handle value that was passed from the frnARPosting form 'seems to be correct
lHwnd - 24183738 'this is within the callingform function
The title bar of the window: M 'this is retrieved using the GetWindowText api that was added to possibly help in debugging



David Paulson

 
This is the dump if I show a simple msgbox from activate event prior to calling the callingform function

frmmonthendroutine
frmMainMenu 29950730 2
frmMonthEndRoutine 11536150 2

Callingform
frmMainMenu 29950730 3
frmMonthEndRoutine 11536150 3
frmARPostings 15468524 3

hWnd - 15468524
lHwnd - 11536150
The title bar of the window: Month End Routine

Everything is just as it should be. Why is it different?


David Paulson

 
> if I show a simple msgbox from activate event

This ensures that the form with the activate event is the top of the Z order.

Tell me, what is your project called?
 
The project is named 'Wadena Steel' after my company. It's just a accounting program I've been working on and off for 15 yrs

David Paulson

 
Thanks strongm for pointing out that article. I now get the handle for the owner form, which really was the one I wanted. It all good now.

David Paulson

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top