INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Object variable or with block not set

Object variable or with block not set

(OP)
Hi, I have the following code in a module

CODE --> VB6

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 --> vb6

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

RE: Object variable or with block not set

(OP)
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

RE: Object variable or with block not set

> '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

RE: Object variable or with block not set

(OP)
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

RE: Object variable or with block not set

(OP)
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

RE: Object variable or with block not set

> 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?

RE: Object variable or with block not set

(OP)
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

RE: Object variable or with block not set

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close