+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    03-31-2009
    Location
    Auburn Alabama
    MS-Off Ver
    Microsoft Office Excel 2007 (Windows)
    Posts
    99

    User-Defined Data Types

    Hello,
    My question is about creating User-Defined Data Types. Is there ANY way possible i can create a User-Defined Data Type that declares a variable of another User-Defined Data Type instead of the Pre-Defined User Types like String, Integer, etc?

    The following explains my problem in more detail.

    I know to create a User-Defined Data Type at the top of the module before any procedures. Like this:

    Code:
    Type CustomerInfo
        Company As String
        RegionCode As Integer
    End Type
    In a procedure I would then declare a variable as that type using a Dim statement. Like this:

    Code:
    Sub TestDataType
        Dim Customer(1 To 100) As CustomerInfo
    End Sub
    So, in the first code example above I created a User-Defined Data Type called CustomerInfo. And in that User-Defined Data Type I declared the variable Company and RegionCode as a String and Integer, respectively. String and Integer being, of course, Pre-Defined Data Types.

    Then in the next code example I declared the variable in a procedure.

    All that works just great.

    BUT... Is there any possible way to create a User-Defined Data Type with a variable that is declared not as a Pre-Defined Data Type, like String and Integer, but instead is declared as another User-Defined Data Type?

    It's my understanding an example would look like this:

    Code:
    Type CustomerInfo
        Company As String
        RegionCode As Integer
    End Type
    
    Type EveryCustomer
          EveryCompany As CustomerInfo
    End Type
    The above would come at the top of the module, before the procedures. The only problem with this is that I didn't declare the variable EveryCompany using a Dim statement. That's something you have to do when using a User-Defined Data Type but not when using a Pre-Defined Data Type. However, the VBE won't allow me to use a Dim statement before the first procedure.

    So, in light of this, is there ANY way possible i can create a User-Defined Data Type that declares a variable of User-Defined Data Type instead of the Pre-Defined User Types like String, Integer, etc.

    Is there any workaround? I really want to create a hierarchy of Data Types...

    Thanks for your help in advance!!! I really appreciate the guidance!
    Last edited by CrazyFileMaker; 04-13-2009 at 03:48 AM. Reason: Forgot the Code Tags!

  2. #2
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,133

    Re: User-Defined Data Types

    Yes, you can.

    Please edit your post to add code tags.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-31-2009
    Location
    Auburn Alabama
    MS-Off Ver
    Microsoft Office Excel 2007 (Windows)
    Posts
    99

    Re: User-Defined Data Types

    Great!!

    Could you or someone elaborate as to how?

  4. #4
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,212

    Re: User-Defined Data Types

    Crazy, as my colleague requested earlier, please modify your original thread to enclose any code within CODE tags. In the post editor, you can press the # button on screen to generate code tags, and then place your code between those tags.

    You will not get responses from others until you've complied with the request of a moderator (that's part of the forum rules as well).

  5. #5
    Registered User
    Join Date
    03-31-2009
    Location
    Auburn Alabama
    MS-Off Ver
    Microsoft Office Excel 2007 (Windows)
    Posts
    99

    Talking Re: User-Defined Data Types

    Oops... Sorry shg! And thanks for pointing that out pjoaquin. At first I misunderstood and actually tagged the post with the word "code"!

  6. #6
    Forum Guru Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: User-Defined Data Types

    Hi

    I am afraid I don't really understand what problem you are having - your code should compile without a problem (so please explain exactly what steps you are taking which is causing you problems). The following which is similar also works without issue:

    Code:
    Type Customer
        ID As Long
        Nm As String
    End Type
    
    Type SpecialCustomer
        Spec As Customer
    End Type
    
    Sub test()
    Dim c As SpecialCustomer
    c.Spec.ID = 1
    c.Spec.Nm = "Richard"
    
    Debug.Print c.Spec.ID & " " & c.Spec.Nm
    
    End Sub
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  7. #7
    Registered User
    Join Date
    03-31-2009
    Location
    Auburn Alabama
    MS-Off Ver
    Microsoft Office Excel 2007 (Windows)
    Posts
    99

    Re: User-Defined Data Types

    Richard,

    I see now I was actually making a mistake in the procedure. Your example cleared things up for me. Thanks to you, I'm making some great strides now. I appreciate the help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0