VBA ADODB Error -2147217904 - Values were not specified for some of the required parameters

StackOverflow https://stackoverflow.com/questions/21753430

  •  11-10-2022
  •  | 
  •  

Pergunta

In an Excel VBA project I'm using an .accdb Access DB, along with ADODB.Connection for performing queries, etc. My system has MS Office 2007 (English), but the client's system is Office 2010 (Spanish).

I've tested on my machine, and the queries, inserts, etc. all work fine. But when I tested on the client's machine, I got the following error:

Error -2147217904, No se han especificado valores para algunos de los parámetros requeridos.

Which can be translated, roughly, to "Values were not specified for some of the required parameters."

The statement giving me problems is the following, executed using conn.Execute sql, numRowsAffected:

INSERT INTO SomeTable
    (COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10, COL11, COL12, COL13, COL14)
    VALUES (1, 'TEXT', 5163, 8482, 103, Verdadero, 3, -1, 'Blanco', 3, 33, 40, 29, 1);

I've changed column names to shorten the code. Also, Verdadero is True in Spanish, in case anyone's wondering.

I've searched for this error on the web, but I can't find much useful information. One forum (in Spanish) mentioned that the error usually means that a field name is wrong, like a table column name changed from "Persona" to "Person", but the query is still referencing "Persona". I double checked, and I don't think this is the case here. Furthermore, the query executes perfectly on my system, which wouldn't be the case if it were a problem like this.

It's also worth noting that other inserts execute successfully (on the client's machine), and the error is only experienced when it executes this particular statement. Also, SomeTable has an AUTOINCREMENT PK ID value that is not included in the SQL statement above.

I assumed that 2007 VBA would be compatible with 2010, and that the clients' machines having the Spanish version of Office wouldn't matter, but now I'm having doubts. I'm using references to "Microsoft Office 12.0 Object Library", while the client's is 14.0, and Microsoft ActiveX Data Objects 6.0 Library, while the client's is 6.1 (I think). Same for the Access database engine Object Library, mine is 12.0 and client's is 14.0.

Anyone know of any potential causes?

Foi útil?

Solução

As suggested in the comments to the question, this appears to be yet another example of where a parameterized query will make code easier to manage (as well as safer). In this case the code would look something like this:

Option Compare Database
Option Explicit

Sub InsertTest()
    Dim con As ADODB.Connection, cmd As ADODB.Command
    Const Veradero = True  ' to simulate Spanish VBA environment
    Set con = CurrentProject.Connection
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = con
    cmd.CommandText = _
            "INSERT INTO SomeTable (TextField, YesNoField) VALUES (?,?)"
    cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "TEXT")
    cmd.Parameters.Append cmd.CreateParameter("?", adBoolean, adParamInput, , Veradero)
    cmd.Execute
    Set cmd = Nothing
    con.Close
    Set con = Nothing
End Sub
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top