PDA

View Full Version : Custom Data Type - Dim Variable As CustomDataTypeName



DzemoDzem
08-06-2014, 08:39 AM
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

mikerickson
08-06-2014, 12:04 PM
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.

mikerickson
08-06-2014, 12:07 PM
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

DzemoDzem
08-06-2014, 02:25 PM
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

mikerickson
08-06-2014, 03:31 PM
The properties of an object, native, custom data type or custom class, cannot be treated as variables in VBA.