Consulting

Results 1 to 5 of 5

Thread: Custom Data Type - Dim Variable As CustomDataTypeName

  1. #1

    Custom Data Type - Dim Variable As CustomDataTypeName

    Hello everyone,

    I am new at VBA and I am working on a Project were I have lot of Custom Data Types.

    My module named Modul1 is:

          Type CNODE                   ' 20/00  Nodes
            m_NR As Long               '        node-number
            m_INR As Long              '        internal node-number
            m_KFIX As Long             '        degree of freedoms
            m_NCOD As Long             '        additional bit code
            m_XYZ(1 To 3) As Single    ' [1001] X-Y-Z-ordinates
          End Type
    
    
          Type CNODE_GRC               ' 20/11:0  number of Nodegroups
            m_ID As Long
            m_MAXGRP As Long           '        maximum number of groups per node
          End Type
    ...
    and in my second Modul2 i have:

    Option Explicit
    'CDB Code For Getting the Database DATA
    
    
    'Read THE CDB
    Public Sub ReadCDB(datei As String)
    
    
    Dim row As Long
    Dim pos As Integer
    Dim data As VARIABLEVALUE 'HOW CAN I INPUT THE STRING VARIABLE VALUE, SOMETIMES IT IS CNODE, SOMETIMES CNODE_GRC...
    Dim DataLen As Long
    Dim Index As Long
    Dim InitIndex As Long
    Dim Column As Long
    Dim Sum As Long
    How can I declare Dim data As ValueFromAStringVariable or insert a code here from StringVariableValue

    Second problem that I have is:
    Is it possible to work with CUSTOM TYPE as if it is array.

    For Example:
    FOR i=1 to 3
         Worksheets(1).Cells(row, Column).Value = data.m_INR '(instead of name, to add element number)
    NEXT
    END
    Is it possible to get the number of elements in Custom TYPE and then say data.element(i) where the i the element number.
    FOR CNODE that I can say element data.element(1) is data.m_NR, data.element(2) is data.m_INR...
    If you have a good tutorial or resolved this in past. Please share it.

    Kind regards,
    ST

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You can't.
    You can declare a variable as only one data type. Luckily Variant is one of those types.
    Unfortunatly, when it comes to assign a custom data type property to that variable.
    ' For Example,
    
    A.m_NR = 8
    You will get an Object Require error.

    You should declare two variables, one for each data type, and once you know what data type you are working on, branch to use one variable or the other.

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    As for the arrays, you could use something like
    Type myDataType
        Elements As Variant
    End Type
    
    
    Sub test()
        Dim A As myDataType
            
            
        ReDim A.Elements(1 To 100)
        A.Elements(1) = 45
        
        With A
            MsgBox "The first of " & UBound(.Elements) & "elements is " & .Elements(1)
        End With
    End Sub

  4. #4
    Thank you mikerickson for the reply and trying to help.


    I have lot of:


    Type NAME1
    Element1(1 to 2) as Long
    Element2 As Long
    Element3 As Single
    Element4 As Single
    Element5 As Long
    EndType
    
    
    Type NAME2
    Element1(1 to 2) as Long
    Element2 As Long
    Element3 As Single
    EndType
    
    
    Type NAME3
    Element1(1 to 2) as Long
    Element2 As Long
    Element3 As Single
    Element4 As Single
    EndType
    More than 600.
    Is it possible if I have a textbox and if I put into textbox1.text="NAME3" to do something like


    Dim Data As textbox1.text (This is the idea, but I can't find a way to implement it)
    Or the other solution, is it possible to add and change code from output e.g. a textbox when macro is started

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The properties of an object, native, custom data type or custom class, cannot be treated as variables in VBA.

Posting Permissions

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