1

This is a bit weird to me.

I have the following code:

      public static object[,] dbMultipleSingleQueries(string strDBPath, object[] oSQL)
      {
       //This is needed for attaching a DB.
       object[,] objOut;
       objOut = new Object[1, 1];
       try
       {

        //Prepare DB connection string      
        strDBPath = "Data Source=" +
              @strDBPath +
              ";Version=3";

        //SQL Objs
        SQLiteConnection dbConnection = new SQLiteConnection(strDBPath);
        SQLiteCommand dbCommand = null;
        SQLiteDataAdapter dbAdapter = null;
        DataTable table = new DataTable();
        //For looping
        int j = 0;
        //Conn str
        dbConnection.Open();
        dbCommand = new SQLiteCommand(dbConnection);
        while (j < oSQL.Length)
        {
         try
         {
          dbCommand.CommandText = oSQL[j].ToString();
          dbCommand.CommandType = System.Data.CommandType.Text;
          if (j == (oSQL.Length - 1))
          {
           dbAdapter = new SQLiteDataAdapter(dbCommand);
           dbAdapter.Fill(table);
           objOut = table2Array(table);
          }
          else
          {

           dbCommand.ExecuteNonQuery();
           objOut[0, 0] = "Success!";
          }
         }
         catch (Exception e2)
         {
          System.Console.WriteLine(e2.StackTrace.ToString());
          objOut[0, 0] = e2.StackTrace.ToString();
         }
         j++;
        };
        dbConnection.Close();
        dbConnection = null;

       }
       catch (Exception e)
       {
        objOut[0, 0] = e.StackTrace.ToString();
       }
       return objOut;

      }

which works perfectly if i feed my arguments from a spreadsheet like this:

    o = Application.Run("dbMultipleSingleQueries", "20130201.db", [test])

where [test] is a range in my workbook with queries that work and look something like this:

attach database 'a.db' as d1

select * from d1.main

now if i do the following in vba

    Option Explicit
    Sub Main()

    Dim sql(1 To 2, 1 To 1)

    sql(1, 1) = "attach database 'a.db' as d1"
    sql(2, 1) = "select * from d1.main"

    o = Application.Run("dbMultipleSingleQueries", "20130201.db", sql)

    End Sub

this does not work. i get a "Type Mismatch" error. Any input why would be a huge help this is driving me nuts!

1 Answer 1

1

Your function prototype specifies that the second argument needs to be of type "Object[]" - but you are passing a variant that probably thinks it is a string array, based on the assignment.

3
  • ok yes i had assumed this as well, but what is the solution? how do i pass it properly?
    – jason m
    Feb 5, 2013 at 3:42
  • Dim sql(1 to 2, 1 to 1) as object[], I assume. Doing this on ipad, no VBA handy right now... Let me know if this works...
    – Floris
    Feb 5, 2013 at 3:44
  • Thanks. Will try for sure in the morning.
    – jason m
    Feb 5, 2013 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.