Type decleration not accessible on another form?

Pharnax
04-15-2008, 04:29 AM
Ok so here's my problem:

I've made a type to store some values, and it works fine on my first form, but on my second form I can't access the variables I've stored as my type.

First form:

Option Explicit
Dim playername(0 To 4) As String
Private Type Person
pName As String

End Type

Dim p(0 To 4) As Person

Private Sub Command1_Click()

Open App.Path & "\Players.txt" For Append As #iFileNo
Print #iFileNo, Text1.Text
Close #iFileNo
End Sub

Private Sub Command2_Click()
Open App.Path & "\Players.txt" For Input As #iFileNo
Line Input #iFileNo, playername(0)
Close #iFileNo
p(0).pName = playername(0)
End Sub

Second Form:

Private Sub Form_Load()
List1.AddItem p(0).pName
End Sub

And I get the error "Sub or Function not defined". How can I make it recognize my UDT on the second form?

NEOLLE
04-15-2008, 04:47 AM
Hello,

You can declare your type as public.
:)

Pharnax
04-15-2008, 05:09 AM
Aready tried that...when I run the program I get a "Cannot define a Public user-defined type within a privat object module" error. Eh...

Flyguy
04-15-2008, 05:18 AM
Then move the declarations to a module.
A Form is like a class, types can only be private.

Pharnax
04-15-2008, 05:51 AM
Then move the declarations to a module.
A Form is like a class, types can only be private.
If I do that, then I'm back to where I started. "Sub or Function not defined" will appear when Form2 loads.

:(

morbidangel
04-15-2008, 05:54 AM
You should declare it as Public in the module

Pharnax
04-15-2008, 06:05 AM
You should declare it as Public in the module

Like this:

Option Explicit

Public Type Person
pName As String

End Type
That's what I did...I still get the same compile error.

Just as if I move any variables to the module, it'll go "Variable not defined".... :(

Flyguy
04-15-2008, 07:25 AM
If you want to access the local Form variables you have to declare the variables as Public:

Public p(0 To 4) As Person


Then you can access it like this from Form2:

MsgBox Form1.p(0).pName

Pharnax
04-15-2008, 09:40 AM
Ah that worked! Thank you Flyguy. And thanks to everyone for the effort :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum