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

assign a UDT variable to another one...??

Status
Not open for further replies.

JennyPeters

Programmer
Oct 29, 2001
228
US
Hi,

I can't seem to assign the contents of one UDT to another UDT.

i.e.
Type Test
Value as stting
end Type

Dim myTest1 as Test
myTest1.Value = 1

Dim myTest2 as Test
myTest2 = myTest1

myTest2.value ends up being 0

How do I assing the contents of one UDT to another?

Thanks,

Jenny Peters
 
First problem, VALUE is a KeyWord. Avoid using KeyWords in UDTs

Try this:
Type Test
MyValue as string
end Type

Dim myTest1 As Test
Dim myTest2 As Test

myTest1.MyValue = 1

myTest2 = myTest1

Debug.Print myTest1.MyValue
Debug.Print myTest2.MyValue


Both have the value of 1
 
Thanks Robert,

That does work well.

I next question would be this then:
I really want to return a UDT as an array from a function. But, the below code gives me a type mismatch error. Any idea how to deal with this?

Option Explicit

Type Test
MyValue As String
End Type


Sub main()

Debug.Print setMyTest(0).MyValue
Debug.Print setMyTest(1).MyValue

End Sub

Function setMyTest() As Test

ReDim myTest1(1) As Test
myTest1(0).MyValue = "zero"
myTest1(1).MyValue = "one"

setMyTest = myTest1

End Function

 
Not quite sure about this, but I think you must initially define the MyValue string as an array like

Type Test
MyValue(n) As String
End Type

so Access knows it's an array to begin with. Then the Redim might work. Can't say it will work, as I have never tried a Redim on a UDT.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top