Consulting

Results 1 to 19 of 19

Thread: How to find an object's type?

  1. #1

    How to find an object's type?

    I need to create function in such a way that it has to work for both excel ranges and recordsets.
    The problem is how to find what kind of object is getting passed inside the function. Help needed

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Public Function ObjectType(obj As Object)
        If TypeName(obj) = "Range" Then
        ObjectType = "Range"
        Else
        ObjectType = "RS"
        End If
    End Function
    Last edited by Aussiebear; 04-14-2023 at 02:04 AM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thank you xld

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    More generic would be to do

    Public Function ObjectType(obj As Object)
        ObjectType = TypeName(obj)
    End Function
    Last edited by Aussiebear; 04-14-2023 at 02:05 AM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Now i'm asking the reverse of it. I know the object-type using the typename( ) function. Now Inside the function I need to create a reportobject (either worksheet/recordset) according to input type. How to set the type of the vairable dynamically?
    Function reconcile(statement1 as object, statement2 as object)
    Dim reportobj as object
    Set reportobj = typename(Statement) 'How to do this?
    End function
    Last edited by Aussiebear; 04-14-2023 at 02:05 AM. Reason: Added the code tags

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Surely, it is nothing to do with its type per se, you just set the object variable to the object.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    But I want to create an entirely new object respectively to user input's object type. For example if the user is reconciling two excel sheets. I need to give the report in an excel sheet. Otherwise in a recordset. But entirely new Excelsheet or a recordset. How to create an object where its type is dynamic? Possible?

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Your code is creating a generic object, so I cannot see the issue.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If you are setting an object, set it to an object, not a string value as returned by TypeName().

    XLD already provided an example on how to check the typename using IF. Select is another route one could use if you have more than 2 or 3 cases.

    e.g.
    Sub t()
      Dim w As Object, wb As Object, ws As Object
      Set w = ThisWorkbook
      MsgBox TypeName(w)
    Set w = ActiveSheet
      MsgBox TypeName(w)
    Select Case TypeName(w)
        Case "Workbook":
          Set wb = w
        Case "Worksheet":
          Set ws = w
      End Select
    MsgBox TypeName(wb), , "Workbook"
      MsgBox TypeName(ws), , "Worksheet"
    End Sub
    Last edited by Aussiebear; 04-14-2023 at 02:06 AM. Reason: Adjusted the code tags

  10. #10
    But you are not getting my point. Suppose user is passing an filled worksheet/recordset. But I need a fresh worksheet/recordset to display my result. So I need to create an object(fresh) as the type of object the user inputs. How to do that?
    I can do that set myresultobj = argobj
    But how to make it fresh (like re-declared)? I believe you are getting it.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I think Kenneth is saying that you need to create a bunch of variables to cover all circumstances, and then assign the appropriate one once you have determined the object type.

    But I ask again, what is the point? Why not just use an object data type and just use that with whatever object is assigned to it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    What I want?
    Is there a way to get that application.classname of an object?

    Why I want?
    I need an empty object of the type of object user deals with. If the user reconciles two recordsets I need a fresh recordset to display my result. If the user reconciles two worksheets then I need a fresh worksheet object.
    I believe now the question understandable.

    What's the advantage of the question I ask?
    If I check the object type using typename and do a case else to create the respective object. I have to give the n number of cases for n number of object type available. Supppose if there is a way to get the application and its class name of an object. We can direct use it in the Createobject( ) function itself.

    Where the hell I need to create an object for?
    To display my results, I need a fresh recordset or a fresh worksheet. So I need a new object.

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Nothing you have said makes me understand why you cannot use a generice object type. I think we are going round in circles here.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You are being too generic for us to help. Zip and attach simple XLS and MDB files if needed.

    As XLD said, using the Object can be generic. CreateObject() gives you a late bound object. If you are wanting an early bound object so that intellisense works while programming, I would just do that at the programming stage and then comment it out and uncomment the late bound object lines for the production version.

    In ADO programming, when you set the recordset object, if your SQL results in no records then your object would be empty. Of course any of your objects can be set to Nothing. e.g.
    Sub t()
    Dim w As Object
    Set w = ThisWorkbook
    MsgBox TypeName(w), , "ThisWorkbook"
    Set w = Nothing
    MsgBox TypeName(w), , "Was ThisWorkbook, now nothing."
    Set w = ActiveSheet
    MsgBox TypeName(w)
    End Sub
    Most programmers will set objects to Nothing at the end of their code to free memory. It not much of any issue these days.
    Last edited by Aussiebear; 04-14-2023 at 02:08 AM. Reason: Adjusted the code tags

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by prabhafriend
    What I want?
    Where the hell I need to create an object for?
    To display my results, I need a fresh recordset or a fresh worksheet. So I need a new object.
    If you want to display results on a fresh worksheet, then you need to .Add a new worksheet to the workbook.

    Then you can Set the new worksheet to an object variable

    Option Explicit
     
    Sub drv()
    Dim obj1 As Object, obj2 As Object, obj3 As Object
    Set obj1 = ActiveWorkbook.Worksheets("sheet1")
    Set obj2 = ActiveWorkbook.Worksheets("sheet2")
    If TypeOf obj1 Is Worksheet Then
        ActiveWorkbook.Worksheets.Add
        ActiveSheet.Name = "FreshSheet"
        Set obj3 = ActiveWorkbook.Worksheets("FreshSheet")
        MsgBox TypeName(obj3)
        Else
        'do recordset stuff
    End If
    End Sub


    Paul
    Last edited by Aussiebear; 04-14-2023 at 02:09 AM. Reason: Adjusted the code tags

  16. #16
    'Assume the scenario
    inputobj as object            'User Sends a recordset
    resultobj as object
    set resultobj = inputobj     'Copying the object
    set resultobj = Nothing      'Wont give a fresh recordset
    I want a fresh recordset(Whatever the user inputs may be a recordset, may be a worksheet) What to do?

    How to create a copy of an object(user's selection)? and making it as it has just been declared?
    Like Set resultobj = Worksheet; Set resultobj = recordset;
    Last edited by Aussiebear; 04-14-2023 at 02:10 AM. Reason: Added the code tags

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    As has been said many times.

    Dim myObBj As Object
    Set myObj = New ADODB.Recordset
        ' do something with myObj as a RecordSet
    Set myObj = Activesheet
        ' dp something with myObj as a Worksheet
    Last edited by Aussiebear; 04-14-2023 at 02:11 AM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  18. #18
    Set myObj = New ADODB.Recordset
    How to make the above statement dynamic? (I want to create a fresh object in the type of object the user inputs (dynamically))
    Last edited by Aussiebear; 04-14-2023 at 02:11 AM. Reason: Added the code tags

  19. #19
    'I want like this
    Dim userobj as object
    Dim resultobj as object
    set userobj = user'sobj
    set resultobj = new (userobj.objecttype)
    How to?
    Last edited by Aussiebear; 04-14-2023 at 02:13 AM. Reason: Added the code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •