Page 1 of 1

Subscript out Of Range??

Posted: Tue Oct 02, 2012 10:32 pm
by JasonQuinn1992
What does it mean if you get an error stating "Subscript out Of Range"?

How would I fix it on this code:



' Initialize variables
Pieces = -1
Pay# = 0
TotPieces = 0
TotPay = 0
T1$ = "PIECEWORK WEEKLY REPORT"
H1$ = "NAME" + SPC$(16) + "Pieces" + SPC$(6) + "Pay"
D1$ = ""
TL$ = ""
Name$ = ""

'Get first worker name
INPUT "PLEASE ENTER YOUR NAME OR TYPE 'END' TO QUIT"; Name$

DO
'Get worker pieces
INPUT "PLEASE ENTER THE NUMBER OF PIECES OR TYPE 'END' TO QUIT"; Pieces

'Calculate rate
IF Pieces >= 600 THEN
Rate = 65
ELSEIF Pieces >= 400 AND Pieces <599>= 200 AND Pieces <399>= 1 AND Pieces <= 199 THEN
Rate = 5
ELSE Rate = 0
END IF

'Calculate pay
IF Rate > 0 THEN Pay = Pieces * Rate ELSE Pay = 0

'Running Totals
TotPieces = TotPieces + Pieces
TotPay = TotPay + Pay
PayF$ = " "
RSET PayF$ = STR$(Pay)
D1$ = D1$ + Name$ + SPC$(20 - LEN(Name$)) + STR$(Pieces) + SPC$(10 - LEN(STR$(Pieces))) + PayF$ + CHR$(13)

'Get next worker or END to quit
INPUT "PLEASE ENTER YOUR NAME"; Name$

LOOP UNTIL UCASE$(Name$) = "END"

'Clear screen
CLS

' Write headings
PRINT TAB(10); T1$
PRINT H1$

'Print report detail
PRINT D1$
ToTPayF$ = " "
RSET ToTPayF$ = STR$(TotPay)
PRINT " TOTALS" + SPC$(12) + STR$(TotPieces) + SPC$(10 - LEN(STR$(TotPieces))) + ToTPayF$
END
'**********************************************************************


its happening at the "H1$ = "NAME" + SPC$(16) + "Pieces" + SPC$(6) + "Pay" line

Posted: Wed Oct 03, 2012 10:22 am
by OPRESION
'SUBSCRIPTION OUT OF RANGE' IT'S AN ERROR WHEN YOU OPEN A MATRIX AND
YOU TRY TO USE THAT MATRIX BEYOND THE LIMITS. FOR EXAMPLE IF YOU OPENED...

DIM AAA(100) AS INTEGER

AND AFTER THAT YOU TRY TO DO THIS...

AAA(101) = 22222

THEN YOU WILL HAVE THE ERROR 'SUBSCRIPTION OUT OF RANGE'

THE FUNCTION SPC() IT DOESN'T NEED THE STRINGS SIGN: '$' BECAUSE
THE COMPILER WILL THINK THAT IT IS A MATRIX. YOU LINE MUST BE:

Code: Select all

 H1$ = "NAME" + SPC(16) + "Pieces" + SPC(6) + "Pay" 

Posted: Wed Oct 03, 2012 11:07 am
by JasonQuinn1992
Thank you for the reply, I tried getting rid of the "$" sign but then I get an error on SPC(16) stating Expected expression, so my program wont run.. how should I fix this?

Posted: Wed Oct 03, 2012 1:46 pm
by burger2227
SPC$ denotes a STRING ARRAY. SPC prints spaces!

Code: Select all

' Initialize variables
Pieces = -1
Pay# = 0
TotPieces = 0
TotPay = 0
T1$ = "PIECEWORK WEEKLY REPORT"
H1$ = "NAME" + SPC(16) + "Pieces" + SPC$(6) + "Pay"
D1$ = ""
TL$ = ""
Name$ = ""

'Get first worker name
INPUT "PLEASE ENTER YOUR NAME OR TYPE 'END' TO QUIT"; Name$

DO
  'Get worker pieces
  INPUT "PLEASE ENTER THE NUMBER OF PIECES OR TYPE 'END' TO QUIT"; Pieces

  'Calculate rate
  IF Pieces >= 600 THEN
    Rate = 65
  ELSEIF Pieces >= 400 AND Pieces <599>= 200 AND Pieces <399>= 1 AND Pieces <= 199 THEN
    Rate = 5
  ELSE Rate = 0
  END IF

  'Calculate pay
  IF Rate > 0 THEN Pay = Pieces * Rate ELSE Pay = 0

  'Running Totals
  TotPieces = TotPieces + Pieces
  TotPay = TotPay + Pay
  PayF$ = " "
  RSET PayF$ = STR$(Pay)
  D1$ = D1$ + Name$ + SPC(20 - LEN(Name$)) + STR$(Pieces) + SPC$(10 - LEN(STR$(Pieces))) + PayF$ + CHR$(13)

  'Get next worker or END to quit
  INPUT "PLEASE ENTER YOUR NAME"; Name$

LOOP UNTIL UCASE$(Name$) = "END"

'Clear screen
CLS

' Write headings
PRINT TAB(10); T1$
PRINT H1$

'Print report detail
PRINT D1$
ToTPayF$ = " "
RSET ToTPayF$ = STR$(TotPay)
PRINT " TOTALS" + SPC(12) + STR$(TotPieces) + SPC$(10 - LEN(STR$(TotPieces))) + ToTPayF$
END
'**********************************************************************

Posted: Wed Oct 03, 2012 2:21 pm
by JasonQuinn1992
Ok, yeah.. I need spaces.... but when i try to run the program i still get an expected expression error and when i change it to SPC$ i get a subscript out of range error

Posted: Wed Oct 03, 2012 5:33 pm
by burger2227
Yeah, QB requires SPACE$ in string definitions. SPC is suppposed to be used to move the invisible PRINT cursor on the screen. I also took off the END exit option because the INPUT requires a number, not a string! If you type END instead of a number it will REDO FROM START.

You could use a string variable for the number INPUT and use VAL to convert it to a number if it is not "END". VAL("END") = 0 so you could also exit if the number value is 0...

Code: Select all

' Initialize variables
Pieces = -1
Pay# = 0
TotPieces = 0
TotPay = 0
T1$ = "PIECEWORK WEEKLY REPORT"
H1$ = "NAME" + SPACE$(16) + "Pieces" + SPC$(6) + "Pay"
D1$ = ""
TL$ = ""
Name$ = ""

'Get first worker name
INPUT "PLEASE ENTER YOUR NAME OR TYPE 'END' TO QUIT: "; Name$

DO
  'Get worker pieces
  INPUT "PLEASE ENTER THE NUMBER OF PIECES: "; Pieces

  'Calculate rate
  IF Pieces >= 600 THEN
    Rate = 65
  ELSEIF Pieces >= 400 AND Pieces <599>= 200 AND Pieces <399>= 1 AND Pieces <= 199 THEN
    Rate = 5
  ELSE Rate = 0
  END IF

  'Calculate pay
  IF Rate > 0 THEN Pay = Pieces * Rate ELSE Pay = 0

  'Running Totals
  TotPieces = TotPieces + Pieces
  TotPay = TotPay + Pay
  PayF$ = " "
  RSET PayF$ = STR$(Pay)
  D1$ = D1$ + Name$ + SPACE$(20 - LEN(Name$)) + STR$(Pieces) + SPC$(10 - LEN(STR$(Pieces))) + PayF$ + CHR$(13)

  'Get next worker or END to quit
  INPUT "PLEASE ENTER YOUR NAME"; Name$

LOOP UNTIL UCASE$(Name$) = "END"

'Clear screen
CLS

' Write headings
PRINT TAB(10); T1$
PRINT H1$

'Print report detail
PRINT D1$
ToTPayF$ = " "
RSET ToTPayF$ = STR$(TotPay)
PRINT " TOTALS" + SPACE$(12) + STR$(TotPieces) + SPC$(10 - LEN(STR$(TotPieces))) + ToTPayF$
END

Posted: Wed Oct 03, 2012 9:11 pm
by JasonQuinn1992
Thank you so much that was exactly what I needed.. the program works perfectly :)