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

Access: Object Variable or With Block variable

Status
Not open for further replies.

BrownsIT

IS-IT--Management
Sep 7, 2012
1
GB
Hi,

I am developing an application in Access 2003 and am re-using a Class module (LoFund) that has been written by somebody else.

In the following piece of code I get an 'Object variable or With block variable not set' error on the lofund.AddValTransaction line. It doesnt even step into that function.

I have tried replacing all the argument variables with static values and I get the same problem. The "set lofund =" line earlier completed fine so I assume the class is initialised ok.

Any ideas?

Code:
    Do While Not mrsShdwFunds.EOF
        Set loFund = GetShdwFund(mrsShdwFunds!sFundCd, mrsShdwFunds!sAltFundCd, rsTran!TRANS_DT, rsTran!UNIT_PRICE_DT, rsRates, moOrigFund)

        dblPrem = Rounding(lfVal * mrsShdwFunds!fPerc, 2)
        
        loFund.AddValTransaction rsTran!LAST_UP_MOVE_DT, dblPrem, _
            rsTran!TRANS_CD, rsTran!TRANS_TYPE, rsTran!UNIT_PRICE_DT
        mrsShdwFunds.MoveNext
    Loop

Thanks

Daniel
 
Did you break your code and put

?GetShdwFund(mrsShdwFunds!sFundCd, mrsShdwFunds!sAltFundCd, rsTran!TRANS_DT, rsTran!UNIT_PRICE_DT, rsRates, moOrigFund)

into the immediate window to see what it is returning?
 
The problem might not be at the code line where you are looking. "with" and no "end with", "if" with no "end if", other loops without "loop" or "next" can all cause the error you are getting. Without the proper closing statement for all such instances, VBA will give the error you are getting, sometimes at locations that don't seem to make sense why the error would occur just there.
 
> Without the proper closing statement for all such instances, VBA will give the error you are getting

Actually, it should give a compile error.

 
So that the OP and others are not confused the following statement is not correct
"with" and no "end with", "if" with no "end if", other loops without "loop" or "next" can all cause the error you are getting. Without the proper closing statement for all such instances,

The error has nothing to do with not closing out a block statement. As pointed out by Strongman, this would cause compile errors not runtime errors and would have a much different error message like
"Loop without Do, for without next, If without end if...
The message could be clearer if MS made it read "object variable not set, or with block variable not set"

But as Strongman and vbajock point out the called function is simply declaring an object variable but for some reason not returning a pointer to an instantiated object. Without seeing the function and likely the class no one can tell why. Here is a simple demo

Code:
Public Function ReturnObject(frmName As String) As Access.Form
  'A function that returns does nothing
  'It declares the variable but it is not set to anything
  
  'if I added code here like: set ReturnObject = forms(frmName)
  'Then it would work 
End Function

Public Sub testReturn()
  Dim frm As Access.Form
  Set frm = ReturnObject("frmOne")
  MsgBox (frm Is Nothing)
  MsgBox frm.Name
End Sub

Since the function does nothing but declares a "ReturnObject", ReturnObject returns nothing. The first Msgbox returns true showing that the function returns a variable set to nothing, the second msgbox gives the "not set" error as expected.


But you can cause this error whenever using a not set object

Code:
dim someObject as object
msgbox someObject.name
 
The error may actually be within the GetShdwFund itself, need to check and see if it has an error handler or is actually returning errors to the calling procedure.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top