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

problem with public variable 2

Status
Not open for further replies.

WebStar

Programmer
May 1, 2002
69
DE
hello,
Here is my problem:
I have a module in wich I have a public variable, a public function and some private functions. When I call the public function from a userform all that private functions are called and the public variable takes a value. Why can't I see that public variable from the form after I call the public function. I need to see what value the variable has taken. It gives me run-error '9': Subscript out of range.
I cant make the public funtion to return the value 'cause the public value is of a type defined by me and is a structure.
The error is recived when I try do rach the structure elements with for.

Here is the code:

'*** Module SWMod ****
Code:
Public Type ARLType
    ARLGen As Long
    ARLCus As Long
    ARLSpe As Long
    ARLName As String
    ARLLib As String
    ARLVer As String
    ARLTVer As String
    ArlSubSys As String
    ArlFile() As FileType
End Type

Public ARL() As ARLType

Public Sub OpenProject()
...
End sub
'*** user form ***
SWMod.OpenProject

For i = 0 To UBound(SWMod.ARL) <--- HERE COMES THE ERROR
cbARL.AddItem SW.ARL(i).ARLName
Next i
End Sub
 
Have you used Redim ARL(something) before using UBound(SWMod.ARL) in the loop?

combo
 
Is it safe to assume that you have Option Explicit set? I ask because a superficial glance indicates that the following might be the problem

For i = 0 To UBound(SWMod.ARL) <--- HERE COMES THE ERROR
cbARL.AddItem SWMod.ARL(i).ARLName
Next i

and this would have been picked up if Option Explicit was set
 
yes. The ARL structure is ok until i leave the module. I can see the entire structure in Watch in the module but in the form it sez that it's out of range
 
I tried with Option Explicit set and not set but I get the same error
 
You call the SWMod.OpenProject from userform. What happens when you do this from another standard module? Do you have any form initialize/activate procedures?
I would trace the array after every redim statement.

combo
 
here is my entire userform:
Code:
Private Sub UserForm_Initialize()
Dim i As Integer
SWMod.OpenProject

For i = 0 To UBound(SWMod.ARL)
    cbARL.AddItem SWMod.ARL(i).ARLName
Next i

End Sub

the error is recived in for, the call of the OpenProject procedure works ok.
if I try:

msgbox SWMod.ARL(1).ARLName

insted of the for I get the same error.
In the module I traced the structure and it has 133 items but when it returns to the userform and I try to access the structure I get the error
 
What we actually need to see is the code in OpenForm, as I now suspect you may have a scoping issue
 
ok u saw the UserForm now here is the entire module:

Public Type FileType
FileName As String
FileType As String
FileLib As String
FileVer As String
FileDest As String
CFlags As String
End Type
Public Type ARLType
ARLGen As Long
ARLCus As Long
ARLSpe As Long
ARLName As String
ARLLib As String
ARLVer As String
ARLTVer As String
ArlSubSys As String
ArlFile() As FileType
End Type

Public ARL() As ARLType
Public Function GoToSection(ByRef File As TextStream, ParamArray SecName() As Variant) As Boolean
Dim i As Integer
Dim data As String

GoToSection = False
Do While Not File.AtEndOfStream
data = File.ReadLine
For i = 0 To UBound(SecName)
If InStr(1, data, &quot;[&quot; & UCase(SecName(i)) & &quot;]&quot;, vbTextCompare) = 1 Then
GoToSection = True
Exit Do
End If
Next i
Loop

End Function
Public Sub ReadARLFile(ByRef File As TextStream, ByVal arlIndex As Long, ByRef ARL() As ARLType)
Dim index As Long, pos As Long
Dim data As String
Dim AFileName As String, AFileLib As String, AFileVer As String, AFileDest As String, AFileCFlag As String
Dim flag As Boolean

index = 0
data = File.ReadLine
flag = False
Do While Not File.AtEndOfStream And Not (Left(data, 1) = &quot;[&quot; And flag = True)
'replace tab by space
pos = InStr(1, data, Chr(9), vbTextCompare)
Do While pos > 0
data = Left(data, pos - 1) & &quot; &quot; & Right(data, Len(data) - pos)
pos = InStr(1, data, Chr(9), vbTextCompare)
Loop
data = Trim(data)
Debug.Print data
If Left(data, 1) = &quot;[&quot; Then
flag = True
End If
If Left(data, 3) <> &quot;/*~&quot; And Left(data, 1) <> &quot;#&quot; And data <> &quot;&quot; Then
pos = InStr(1, data, &quot;=&quot;, vbTextCompare)
If pos > 0 Then
AFileName = Trim(Left(data, pos - 1))
data = Trim(Right(data, Len(data) - pos))
If data <> &quot;&quot; Then
SubStr = Split(data, &quot;|&quot;)
If UBound(SubStr) > 2 Then
AFileLib = Trim(SubStr(0))
AFileVer = Trim(SubStr(1))
AFileDest = Trim(SubStr(2))
AFileCFlag = Trim(SubStr(3))
If AFileLib <> &quot;-&quot; Then
ReDim Preserve ARL(arlIndex).ArlFile(index)
With ARL(arlIndex).ArlFile(index)
.FileName = AFileName
.FileLib = AFileLib
.FileVer = AFileVer
.FileDest = AFileDest
If AFileCFlag <> &quot;-&quot; And AFileCFlag <> &quot;.&quot; Then
.CFlags = AFileCFlag
Else
.CFlags = &quot;&quot;
End If
End With
End If
index = index + 1
End If
End If
End If
End If
data = File.ReadLine
Loop

End Sub
Public Sub ReadARLSection(ByRef File As TextStream, ByRef Subsys As String, ByRef TheDest() As ARLType)
Dim pos As Long, index As Long
Dim data As String

If TheDest(0).ARLName <> &quot;&quot; Then
index = UBound(TheDest) + 1
Else
index = 0
End If
data = File.ReadLine
Do While Not File.AtEndOfStream And Left(data, 1) <> &quot;[&quot;
data = Trim(data)
If Left(data, 3) <> &quot;/*~&quot; And Left(data, 1) <> &quot;#&quot; And Left(data, 2) <> &quot;/*&quot; And Left(data, 2) <> &quot;//&quot; Then
pos = InStr(1, data, &quot;=&quot;, vbTextCompare)
If pos > 0 Then
ReDim Preserve TheDest(index)
TheDest(index).ArlSubSys = TheDest(index).ArlSubSys & &quot;-&quot; & Subs
TheDest(index).ARLName = Trim(Left(data, pos - 1))
data = Trim(Right(data, Len(data) - pos))
pos = InStr(1, data, &quot;|&quot;, vbTextCompare)
TheDest(index).ARLLib = Trim(Left(data, pos - 1))
data = Trim(Right(data, Len(data) - pos))
pos = InStr(1, data, &quot;#&quot;, vbTextCompare)
If pos > 0 Then
TheDest(index).ARLVer = Trim(Left(data, pos - 1))
Else
TheDest(index).ARLVer = Trim(data)
End If
If Left(TheDest(index).ARLVer, 1) = &quot;v&quot; Or Left(TheDest(index).ARLVer, 1) = &quot;r&quot; Then
TheDest(index).ARLVer = Right(TheDest(index).ARLVer, Len(TheDest(index).ARLVer) - 1)
End If
index = index + 1
End If
End If
data = File.ReadLine
Loop

End Sub
Public Sub ReadConf(ByVal File As TextStream, ByRef SubTab() As String, ByVal Path As String)
'find all subsystems from mc1_conf.def
Dim data As String
Dim i As Integer
Dim SubsysPath As String

i = -1
Do While Not File.AtEndOfStream
data = File.ReadLine
If LCase(Left(data, 7)) = &quot;subsys_&quot; Then
i = i + 1
ReDim Preserve SubTab(i)
SubsysPath = Left(Path, InStr(1, Path, &quot;\mc1_u\work\conf&quot;)) & &quot;mc1_&quot; & Mid(data, 8, 1) & &quot;\work\config&quot;
SubTab(i) = SubsysPath & &quot;\&quot; & Left(data, 8) & &quot;.def&quot;
End If
Loop

End Sub

Public Function OpenProject()
Dim objFSO As New FileSystemObject
Dim objText As TextStream, objTextFile As TextStream
Dim Path As String, PathFile As String
Dim Subsys() As String, mySubsys As String, ConfFile As String
Dim ARL() As ARLType
Dim bFound As Boolean

'path has to be defined
Path = &quot;h:\p_rm1\502\mc1_u\work\config&quot;
'take respective subsystem
ConfFile = Path & &quot;\mc1_conf.def&quot;
'ReDim Subsys(0)
'Subsys(0) = Path & &quot;subsys_&quot; & Mid(Path, InStr(1, Path, &quot;\work\&quot;, vbTextCompare) - 1, 1) & &quot;.def&quot;
mySubsys = Path & &quot;\&quot; & &quot;subsys_u.def&quot;
If objFSO.FileExists(ConfFile) Then
Set objText = objFSO.OpenTextFile(ConfFile, ForReading)
If GoToSection(objText, &quot;PRJ_SUBSYS&quot;) = True Then
Call ReadConf(objText, Subsys(), Path)
ReDim ARL(0)
For i = 0 To UBound(Subsys)
If LCase(Subsys(i)) = LCase(mySubsys) And objFSO.FileExists(Subsys(i)) Then
Set objTextFile = objFSO.OpenTextFile(Subsys(i), ForReading)
If GoToSection(objTextFile, &quot;PRJ_AGR&quot;, &quot;PRJ_AGGR&quot;) Then
bFound = True
ReDim TheDest(0)
Call ReadARLSection(objTextFile, Subsys(i), ARL())
End If
objTextFile.Close
End If
Next i
Else
MsgBox &quot;Cannot find 'PRJ_SUBSYS' section in configuration file.&quot;, vbOKOnly + vbExclamation, &quot;Missing data !&quot;
End If
objText.Close
End If
If bFound Then
For i = 0 To UBound(ARL)
PathFile = Path & &quot;\&quot; & ARL(i).ARLName & &quot;.arl&quot;
If objFSO.FileExists(PathFile) Then
Set objTextFile = objFSO.OpenTextFile(PathFile, ForReading)
ReadARLFile objTextFile, i, ARL
End If
Next i
End If

End Function
 
Try to remove the declaration:
Dim ARL() As ARLType
from Public Function OpenProject().

combo

 
Bingo! As suspected, it is a scoping issue; you have a declaration for ARL() in the OpenProject function. Within the scope of the OpenProject function this will override your global declaration. Remove it, and things should work as expected.
 
damn... that was it...Thank you very much! This was really helpfull. And I'm working to find the error for about 4 hours:) Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top