Help with Split...

Posted on 2003-03-09
Medium Priority
Last Modified: 2010-05-01
Working on an RPG.
For the items, I have a text file with the format
ID# <> NameofItem <> TypeofItem <> ValueofItem
To test the call, I made a sample form with a text box and a command button.

Private Sub cmdNewItem_Click()
Item = txtItem.Text
If Item = "" Then Item = 0
Call Module1.CreateItem(Item)

End Sub

Sub CreateItem(ByVal ItemNo As Integer)
fileItems = App.Path + "\Data\Items.dcl"
If ItemNo = 0 Then ItemNo = 100
If ItemNo < 100 Or ItemNo > 300 Then
MsgBox "Invalid Item ID Detected(" & ItemNo & ")"
End If
'Note About File Set Up
'fileItems format for items is as follows:
'ID% <> Name <> Type <> Value
'Icon = ID%
Open fileItems For Input As #1
Do Until EOF(1)
    Line Input #1, Temp$
    ID% = Split(Temp$, "<>")(0)
    If ItemNo = ID% Then
        Items(ItemNo).ID = Split(Temp$, "<>")(0)
        Items(ItemNo).Name = Split(Temp$, "<>")(1)
        Items(ItemNo).Type = Split(Temp$, "<>")(2)
        Items(ItemNo).Value = Split(Temp$, "<>")(3)
        Items(ItemNo).Icon = ID%
        If Items(ItemNo).Type = "Armor" Then
            MsgBox "You found a suit of " & Items(ItemNo).Name & "!"
            MsgBox "You found a " & Items(ItemNo).Name & "!"
        End If
        Close #1
        Universe.NoOfItems = Universe.NoOfItems + 1
        Exit Sub
    End If
Close #1
End Sub

Every time I try to run this little tidbit...if I set the ID equal to 100 by any means, it's give me the appropriate "You found a Dagger!" MsgBox. It also recognizes the invalid IDs (below 100, above 300). Any number above one hundred and below three hundred, the accepted range, however, gives me a "Subscript out of range" error. Debug points at the line "Items(ItemNo).ID = Split(Temp$, "<>")(0)" in CreateItem. This is the second time I've had split errors in a program; the last tiem it happened I just had to sit and toy with it until i foudn some thick, bulky code to cover it up. Is there anyone who knwos how to fix this, or who can at least pass me a fair reason why the subcript is out of range? I'd be greatly appreciative for any help.
Question by:Aioreyn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 13

Expert Comment

ID: 8097950
Where do you Dimension Items? WHat is its size?

If I understand what yo are doing, Items should be an array which is at least 300 long as:
if ItemNo = 0 you force it to 100 and write in items(100).ID the found value from your file and itemNo can at most be equal to 300 and you will write in items(300)

Apparently, if Dagger is 100, Items must have been declared as dim items(100) (101 slots)

Am I right?



Author Comment

ID: 8098000
The Dims arei n the same module, and are as follows:

Public Type ItemArray
    ID As Integer
    Name As String
    Type As String
    Icon As Integer
    Value As Integer
    Special As Integer
    Magic As Integer
    Equipped As Boolean
End Type
Global Items(100) As ItemArray
LVL 13

Accepted Solution

stefri earned 200 total points
ID: 8098137
If the read value is 120, you will write it in slot 120 of Items which is limited to slot 100, so it will bomb with Subscript out of range" error.

Increase the size of Items or write at slot ItemID - ubound(Items)

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question