Search code examples
vb6libreoffice-basic

Symbol already defined differently VB


I'm trying to compile the following code, and I keep getting an error. I got this erro before multiple times so I was forced to use workaround functions. This time I'm really tired of this issue and I need to know what's wrong here.

sub SQL_AddTestResults (byval sData as string, byval testID as integer)

   dim i as integer
   dim dataChain as string
   dim aData (Split(sData, ";").length) as string

   aData = Split(sData, ";")

   for i = 0 to aData.Length

      if(i = 4) then
          goto skip
      elseif (i = 68) then
          goto skip
      elseif (i = 72) then
          goto skip
      end if

      if(i = aData.length) then
          dataChain = dataChain & aData(i)
      else
          dataChain = dataChain & aData(i) & ", "
      end if

      skip:
   next

   MsgBox (dataChain)

   SQL_statement = "INSERT INTO ""TestData"" VALUES (" & dataChain & ");"   
   Stmt = connection.createStatement()
   Stmt.executeUpdate(SQL_statement)
end sub 

Compiling this code gives me the following error on "for i = 0 to aData.Length" line:

Basic syntax error.

Symbol aData already defined differently.

Have no idea why. Apologies if that's a trivial problem, but I'm completely new to VB. C++ didn't prepare me for this.


Solution

  • Arrays in classic VB don't have a "length" property. I'm not sure where you got that from.

    The way to get the bounds of an array in classic VB is with the LBound and UBound functions.

    for i = LBound(aData) to UBound(aData)
    

    This way you can even handle arrays that don't have 0 as the starting index, as yes, one of VB's wonderful quirks is that it lets you use any range of numbers for your indexes.

    VB6 isn't a language I'd recommend for new development. If you're trying to learn something new, there are plenty of other options. As you've no doubt noticed, it's harder and harder to find documentation on how classic VB does things, and how it differs from VBScript and VB.NET. If you need to be maintaining an older VB6 code base, I'd recommend finding a used book somewhere that goes over VB6 syntax and usage.