Runtime Error '9': Subscript out of range-VBForums
Results 1 to 11 of 11

Thread: Runtime Error '9': Subscript out of range

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Question Runtime Error '9': Subscript out of range

    I have run into this persistent problem with a project. I can compile just fine, I can even run the project without a problem. However, when I make the executable and then run the executable i get the error, "Runtime Error '9': Subscript out of range".

    I have literally removed all the code, components, references, modules, etc. and still get the error on a bare bones nothing proejct. Very strange to say the least.

    I started a new project and started to import everything from scratch again. I am now at a point in which I have 2 modules, 1 class, and 1 form along with a data environment in the project.

    Once again, I can compile and run the program without a problem at all. Making the executable and then running the executable fails with the aformentioned error code.

    Taking out the line of code the creates the instance of the class gets rid of the error.

    "Set gEmailSettings = New Email_Settings"

    I am really stumped. I don't understand the error at all and all the code is pretty basic at the moment.

    Any ideas would be greatly appreciated.

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Runtime Error '9': Subscript out of range

    Welcome to the forums.

    Try to do a "Run With Full Compile" from the IDE....it should blow up on the line causing the error.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Re: Runtime Error '9': Subscript out of range

    I had been running it from the ide with full compile. I get no error at all.

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    Re: Runtime Error '9': Subscript out of range

    As far as I know you can only get this error from a few different things.
    Code:
    Dim x()
    x(0) = 1
    Code:
    Dim y(2)
    y(3) = 1
    Code:
    Dim C As Collection, V
    Set C = New Collection
    C.Add 1
    V = C.Item(2)
    Collections are not that common so I guess it's a array subscript problem.

    Your first post suggests that the class is at fault....

    How about posting your project

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Re: Runtime Error '9': Subscript out of range

    All of this code works just fine during debugging and will work just fine with a full compile as well. It is only the executable that fails.


    I do have a couple of arrays in the class module.

    Code:
    Private Function ParseCLA()
      Dim tArray() As String ' Temporary String array to hold all parsed arguments
      ' Use the split function to parse out all arguments into an array
      tArray() = Split(Command, ";")
      '  Takes the first 3 arguments and assigns them to the global variables
      gSpinLoc = tArray(0)
      gCurrentUser = tArray(1)
      gCurrentBrokerID = tArray(2)
    End Function
    I have a private type for the module as well that is used in an array:

    Code:
    Private Type AccountSettings
      sHostName As String           ' Holds the full hostname URL
      sEADD As String               ' Email address of the account
      sRPort As Integer             ' Port to retrieve email on.  Standard is 143
      sUseSSL As Boolean            ' SSL is required or not
      sUName As String              ' Username for the account
      sPass As String               ' Password for the account
      sAccountName As String        ' Friendly name of the account
      sFullName As String           ' Full Name of the user
      sPlainTextPass As Boolean     ' If a plain text password should be used
    End Type
    This is the code that uses the type in an array:

    Code:
    Private Function GetPrimaryEmailSettings()
      Dim tRST As ADODB.Recordset   ' Temporary recordset to obtain email settings
      Dim tErr As Integer           ' Holds custom error code
      Dim tNum As Integer           ' Temporary variable to move through the recordset
      
      ' Set the error code to 0
      tErr = 0
      ' Open the connection to SArtData.mdb
      DCON.SARTDATA.Open
      ' Determine how many email accounts exist
      Set tRST = DCON.SARTDATA.Execute("SELECT Count(*) As Amount FROM [EMAIL-ACCOUNTS]")
      ' If no email accounts exist then trigger an error and terminate the program,
      ' otherwise initiate the gESA() array to contain the number of accounts found
      If tRST!Amount <= 0 Then
        tErr = 1
        GoTo PrimaryAccountError
      Else
        ReDim gESA(1 To tRST!Amount) As AccountSettings
      End If
      
      ' Grab just the Primary Account
      Set tRST = DCON.SARTDATA.Execute("SELECT * FROM [EMAIL-ACCOUNTS] WHERE PRIMARYACCOUNT = -1")
      ' If no Primary Account exists then trigger an error and terminate the program
      If tRST.EOF = True Then
        tErr = 2
        GoTo PrimaryAccountError
      End If
      
      ' Set the first element in gESA() to the primary account settings
      gESA(1).sEADD = tRST!Email_Address
      gESA(1).sHostName = tRST!Host_Incoming
      gESA(1).sPass = tRST!Email_Password
      gESA(1).sRPort = tRST!Port_Incoming
      gESA(1).sUName = tRST!User_Name
      gESA(1).sUseSSL = tRST!SSL_IN
      gESA(1).sAccountName = tRST!Account_Name
      gESA(1).sPlainTextPass = tRST!PlainTextPass
      gESA(1).sFullName = tRST!Full_Name
      
      ' Grab all the secondary accounts
      Set tRST = DCON.SARTDATA.Execute("SELECT * FROM [EMAIL-ACCOUNTS] WHERE PRIMARYACCOUNT = 0")
      
      ' Loop through the accounts and set the gESA()
      If tRST.EOF = False Then tRST.MoveFirst
      tNum = 2  ' This is set to 2 since it is the beginning of the secondary accounts in gESA()
      Do While Not tRST.EOF
        gESA(tNum).sEADD = tRST!Email_Address
        gESA(tNum).sHostName = tRST!Host_Incoming
        gESA(tNum).sPass = tRST!Email_Password
        gESA(tNum).sRPort = tRST!Port_Incoming
        gESA(tNum).sUName = tRST!User_Name
        gESA(tNum).sUseSSL = tRST!SSL_IN
        gESA(tNum).sAccountName = tRST!Account_Name
        gESA(tNum).sPlainTextPass = tRST!PlainTextPass
        gESA(tNum).sFullName = tRST!Full_Name
        tNum = tNum + 1
        tRST.MoveNext
      Loop
      
      ' Close the data connection and set the recordset object to nothing
      DCON.SARTDATA.Close
      Set tRST = Nothing
      
      Exit Function
      
    ' Custom error. Will inform the user if no accounts exist or if a primary account does not exist.
    PrimaryAccountError:
      If tErr = 1 Then
        RaiseEvent AccountError(1)
      End If
      If tErr = 2 Then
        RaiseEvent AccountError(2)
      End If
    End Function

  6. #6
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,088

    Re: Runtime Error '9': Subscript out of range

    Do you have any command line arguments in your app that are pre-set in the IDE? When you run the compiled exe you also need to pass those arguments or it will error.

    Create a shortcut to your compiled exe and set the arguments in the properties of the shortcut.

    "C:\SomePath\MyEXE.exe" ;Arg1 ;Arg2

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    Re: Runtime Error '9': Subscript out of range

    That would seem logical, the code assumes there will always be three command line arguments passed. It should really check first.

  8. #8
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,088

    Re: Runtime Error '9': Subscript out of range

    Yup, a check would have prevented the errors and some kind of notification to the user that arguments were not being passed would have saved allot of time on this issue.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  9. #9
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Runtime Error '9': Subscript out of range

    Quote Originally Posted by ed207
    All of this code works just fine during debugging and will work just fine with a full compile as well. It is only the executable that fails.


    I do have a couple of arrays in the class module.

    Code:
    Private Function ParseCLA()
      Dim tArray() As String ' Temporary String array to hold all parsed arguments
      ' Use the split function to parse out all arguments into an array
      tArray() = Split(Command, ";")
      '  Takes the first 3 arguments and assigns them to the global variables
      gSpinLoc = tArray(0)
      gCurrentUser = tArray(1)
      gCurrentBrokerID = tArray(2)
    End Function
    As milk pointed out:


    Dim x()
    x(0) = 1
    and with your directive such that tArray() = Split(Command, ";")
    and then immediately after you say :

    gSpinLoc = tArray(0)
    gCurrentUser = tArray(1)
    gCurrentBrokerID = tArray(2)

    You never check the ubound of tArray().


    I would suggest that wherever you assign a variable to a value of an array that you've created by the split function, that you check first if its ubound is at least that of the maximum index that you intend to be available.

    -Lou

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Re: Runtime Error '9': Subscript out of range

    Sorry for the long reply. I appreciate your help. I included a check to see if Command was null, then checked the ubound of the array. Any problems is "supposed" to raise an event. That however, is not working and the subject of another post......

  11. #11
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,088

    Re: Runtime Error '9': Subscript out of range

    Well Command$ will never be Null as it would be vbNullstring.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.