Results 1 to 6 of 6

Thread: [RESOLVED] Subscript Out Of Range error while saving from MSFlexGrid to Access Database.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    25

    Resolved [RESOLVED] Subscript Out Of Range error while saving from MSFlexGrid to Access Database.

    Hello,

    I am working on a application where i am using a MSFlexGrid to allow users to populate data.

    When the populates the data in the FlexGrid and says "Save", the data in the grid is written to the access database.

    In the for loop(which writes data to access), I am getting an error "Subscript out Of Range".

    Below is the code that is causing the issues.

    ==================================================================
    For i = 0 To FlxGd.Rows - 1
    For j = 0 To FlxGd.Cols - 1
    rs.AddNew
    rs.Fields(0) = FlxGd.TextMatrix(0, j + 2) ------> This line throws the error.
    rs.Fields(1) = FlxGd.TextMatrix(i + 1, 0)
    rs.Fields(2) = FlxGd.TextMatrix(i + 1, 1)
    rs.Fields(3) = FlxGd.TextMatrix(i + 1, j + 2)
    rs.Fields(4) = cmbMonths.Text
    Next
    Next
    ==================================================================

    Please let me know if there is any issue in the code that might be causing this.

    I have also attached a screenshot of my form.

    Thanks in advance.
    Attached Images Attached Images  

  2. #2
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,375

    Re: Subscript Out Of Range error while saving from MSFlexGrid to Access Database.

    I don't recall whether the rows and cols (of the flexgrid) start at 0 or 1 ?
    But in the meantime, when the program breaks (pauses due to the error), you can hover the mouse over j etc to see what they contain, and also the IDE will tell you which 'thing' it cannot handle (out of range)

    Rob

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    25

    Re: Subscript Out Of Range error while saving from MSFlexGrid to Access Database.

    I see that the program is breaking when i = 40 and also when j = 31.

    I believe it is because we do not have 41st row and 32nd column.

    Could anyone please help me with a code update which might help me get code right.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,346

    Re: Subscript Out Of Range error while saving from MSFlexGrid to Access Database.

    Well since you are adding 2 to J then you need to stop your loop sooner

    rather than
    Code:
    For j = 0 To FlxGd.Cols - 1
    It would be
    Code:
    For j = 0 To FlxGd.Cols - 3
    Or better still forget adding anything to J and start the loop at 2

    Code:
    For j = 2 To FlxGd.Cols - 1
    Of course you need to do something like this for the other loop as well either remove the addition and start the loop higher or increase the subtraction
    Last edited by DataMiser; Aug 12th, 2013 at 06:37 AM.

  5. #5
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,375

    Re: Subscript Out Of Range error while saving from MSFlexGrid to Access Database.

    The hovering should also have told you exactly where the error is, by telling you exactly where in the code it is out of range.
    Did you hover enough ?

    Can you make a copy of your project and delete everything but that form, and attach it.
    An empty copy of your data base would be handy as well

    Rob

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    25

    Re: [RESOLVED] Subscript Out Of Range error while saving from MSFlexGrid to Access Da

    Thanks for the help DataMiser...Your code tweaks helped me fix the issue...

Posting Permissions

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



Click Here to Expand Forum to Full Width