By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Subscript out of range: 'f'

P: 1
This is driving me crazy, I am by no means a pro at this, but I am trying to figure out why this error is popping up, please help, I feel like it is simple..

I am going to post the code and bold and underline the line that is throwing the error

Expand|Select|Wrap|Line Numbers
  1. sub addVariations(pOptionGroupDescrip, pOptionDescrip, pPriceToAdd, pPercentageToAdd, pIdProduct)
  2.  
  3.  dim rstemp, pIndex  
  4.  
  5.  if pOptionGroupDescrip<>"" then
  6.  
  7.   arrayOptions      =split(pOptionDescrip,",")
  8.   arrayPrices       =split(pPriceToAdd,",")         
  9.   arrayPercentage       =split(pPercentageToAdd,",")         
  10.  
  11.   ' verification
  12.   if arrayOptions(0)="" then
  13.     response.redirect "comersus_backoffice_message.asp?message="& Server.Urlencode("If you need variations please fill every variation in order")
  14.   end if
  15.  
  16.    '  add option group 
  17.    mySQL="INSERT INTO optionsGroups (optionGroupDesc, type) VALUES ('" &pOptionGroupDescrip & "','D')"
  18.    call  updateDatabase(mySQL, rstemp, "itemFunctions.asp")  
  19.  
  20.    ' retrieve optionGroupId
  21.  
  22.    mySQL="SELECT MAX(idOptionGroup) AS maxIdOptionGroup FROM optionsGroups WHERE optionGroupDesc='" & pOptionGroupDescrip& "'"
  23.    call  getFromDatabase(mySQL, rstemp, "itemFunctions.asp") 
  24.  
  25.    if rstemp.eof then        
  26.      response.redirect "comersus_backoffice_message.asp?message="& Server.Urlencode("Cannot get option group ID")
  27.    end if  
  28.  
  29.    pIdOptionGroup=rstemp("maxIdOptionGroup")       
  30.  
  31.    ' add options
  32. for f=0 to 3
  33.  
  34.    if arrayOptions(f)<>"" and (arrayPrices(f) <> "" or arrayPercentage(f) <> "") then
  35.        mySQL="INSERT INTO options (optionDescrip, priceToAdd, percentageToAdd) VALUES ('" & arrayOptions(f) & "'," & arrayPrices(f)& "," & arrayPercentage(f) & ")"     
  36.  
  37.         call  updateDatabase(mySQL, rstemp, "itemFunctions.asp")  
  38.  
  39.         ' get idOption
  40.  
  41.         mySQL="SELECT MAX(idOption) AS maxIdOption FROM options WHERE optionDescrip='" & arrayOptions(f) & "'"
  42.  
  43.         call  getFromDatabase(mySQL, rstemp, "itemFunctions.asp") 
  44.  
  45.         if rstemp.eof then        
  46.              response.redirect "comersus_backoffice_message.asp?message="& Server.Urlencode("Cannot get option ID")
  47.         end if     
  48.  
  49.         pIdOption=rstemp("maxIdOption")   
  50.  
  51.         ' assign to group
  52.         mySQL="INSERT INTO options_optionsGroups (idOption, idOptionGroup) VALUES (" & pIdOption & "," & pIdOptionGroup & ")"
  53.  
  54.         call  updateDatabase(mySQL, rstemp, "itemFunctions.asp")  
  55.  
  56.    end if
  57.  
  58.    next
  59.  
  60.    ' assign group to product
  61.     mySQL="INSERT INTO optionsGroups_products (idOptionGroup, idProduct) VALUES (" & pIdOptionGroup & "," & pIdProduct & ")"
  62.  
  63.     call  updateDatabase(mySQL, rstemp, "itemFunctions.asp")  
  64.  
  65.   end if ' filled
  66.  
  67. end sub
  68.  
  69.  
  70. sub loadProductVariations(pIdProduct, arrayOptions1, arrayOptions2, pHiddenIdOptions, pOptionDescrip1, pOptionDescrip2, pIdOptionGroup1, pIdOptionGroup2)
  71.  
  72.   dim rstemp
  73.  
  74.   ' retrieve option groups
  75.  
  76.   mySQL="SELECT optionsGroups.idOptionGroup, optionGroupDesc FROM optionsGroups_products, optionsGroups WHERE optionsGroups.idOptionGroup=optionsGroups_products.idOptionGroup AND idProduct=" &pIdProduct
  77.   call getFromDatabase(mySQL, rstemp, "getOptions") 
  78.  
  79.   pIdOptionGroup1=0
  80.   pIdOptionGroup2=0  
  81.  
  82.   do while not rstemp.eof 
  83.  
  84.     if pIdOptionGroup1=0 then
  85.       pOptionDescrip1=rstemp("optionGroupDesc")
  86.       pIdOptionGroup1=rstemp("idOptionGroup")
  87.     else
  88.       pOptionDescrip2=rstemp("optionGroupDesc")
  89.       pIdOptionGroup2=rstemp("idOptionGroup")
  90.     end if
  91.  
  92.   rstemp.movenext
  93.   loop  
  94.  
  95.   ' fill option arrays with default values
  96.   for f=0 to 3
  97.    arrayOptions1(0,f)=""
  98.    arrayOptions1(1,f)=0
  99.    arrayOptions1(2,f)=0
  100.    arrayOptions1(3,f)=0
  101.    arrayOptions2(0,f)=""
  102.    arrayOptions2(1,f)=0   
  103.    arrayOptions2(2,f)=0
  104.   next
  105.  
  106.  
  107.   ' retrieve options
  108.   pHiddenIdOptions=""
  109.  
  110.   if pIdOptionGroup1<>0 then
  111.  
  112.    mySQL="SELECT optionDescrip, priceToAdd, percentageToAdd, options.idOption FROM options, options_optionsGroups WHERE idOptionGroup=" & pIdOptionGroup1 & " AND options.idOption=options_optionsGroups.idOption"
  113.    call getFromDatabase(mySQL, rstemp, "getOptions") 
  114.    f=0
  115.  
  116.    do while not rstemp.eof
  117.      arrayOptions1(0,f)    = trim(rstemp("optionDescrip"))
  118.      arrayOptions1(1,f)    = rstemp("priceToAdd")
  119.      arrayOptions1(2,f)    = rstemp("percentageToAdd")
  120.      pHiddenIdOptions    = pHiddenIdOptions & rstemp("idOption") &"," 
  121.     f=f+2
  122.    rstemp.movenext
  123.    loop   
  124.  
  125.   end if
  126.  
  127.   if pIdOptionGroup2<>0 then
  128.  
  129.    mySQL="SELECT optionDescrip, priceToAdd, percentageToAdd, options.idOption FROM options, options_optionsGroups WHERE idOptionGroup=" & pIdOptionGroup2 & " AND options.idOption=options_optionsGroups.idOption"
  130.    call getFromDatabase(mySQL, rstemp, "getOptions") 
  131.  
  132.    f=0
  133.    do while not rstemp.eof
  134.      arrayOptions2(0,f)    = trim(rstemp("optionDescrip"))
  135.      arrayOptions2(1,f)    = rstemp("priceToAdd")
  136.      arrayOptions2(2,f)    = rstemp("percentageToAdd")
  137.      pHiddenIdOptions    = pHiddenIdOptions & rstemp("idOption") &"," 
  138.     f=f+2
  139.    rstemp.movenext
  140.    loop   
  141.  
  142.   end if
  143.  
  144. end sub
Aug 29 '08 #1
Share this Question
Share on Google+
2 Replies


Expert Mod 2.5K+
P: 2,545
Hi. Sorry, but 144 lines of code is too much to try to review for potential errors without being able to use the full debugging environment that VBA provides.

The error message is telling you that you are trying to access an array element with an index value that lies outside of the declared range for that array. I would doubt that the error is arising on your highlighted line, the innocuous for loop at line 32, even though you appear to be using an undeclared variable f as the loop index. An error could occur here if at some stage you have changed the value of for counter f from within the loop to a value lying outside of its preset range of 0-3, but this does not jump out from looking at the code.

It also does not help us that the code you post has no declarations for your arrays either.

I would suggest that you are going to have to be systematic about debugging. Set a breakpoint at the start of your routine and step through the code line by line, using the local variable watch facilities of VBA to check the values of all variables as you go. You should check particularly that the value of your array index variable f stays within the declared index range for the arrays concerned.

You may wish to read the series of short articles by our expert site admin NeoPa on Debugging in VBA for further guidance.

-Stewart

ps at lines 58 and 104 you list 'next' instead of 'next f' - better to be explicit and specify what the loop variable being incremented actually is, so use next f there instead.
Aug 29 '08 #2

Expert Mod 2.5K+
P: 2,545
Further to the previous post, it appears that you are either using undeclared variables routinely or you have globals defined elsewhere. You can use the compiler to prevent errors such as the use of undeclared variables by setting

OPTION EXPLICIT

at the top of your code.

Look particularly at where (or if) you are defining loop variable f. It appears in your sub called from your main procedure, and in your main procedure. If it is global in scope you really will have a problem, as its value in the calling routine will be overwritten by the sub when you call it.

-Stewart
Aug 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.