No value given for one or more required parameters

2019-08-07 04:49发布

问题:

I would like to import a text file with this code into an Access 2007 database file:

Dim test As New cAccess
test.Connect()
test.Command.Connection = test.Connection
test.Command.CommandText = 
        "insert into kvks " &
        "select f1 as artnr, f2 as [lfdnr-kal], f3 as saln, f4 as suchbegriff, f5 as wert, f6 as eh " &
        "from [Text;FMT=Delimited;HDR=No;CharacterSet=850;DATABASE=" & txtKvks.Text.Substring(0, txtKvks.Text.LastIndexOf("\")) & "].[" & txtKvks.Text.Substring(txtKvks.Text.LastIndexOf("\") + 1) & "]"
Dim start As DateTime = Now
test.Command.ExecuteNonQuery()

But I get the error "No value given for one or more required parameters".

The text file is delimited with semicolon (;). At first I thought, it is because it recognises one line as a field because of the standard delimiter comma, and then of course the another fields are missing.

I inserted a schema.ini file too in the same directory, where the file is stored, but the same result.

What could be the problem?

回答1:

One potential cause of this error is that when using a schema.ini file with the Jet/ACE "Text" engine the HDR=No clause in the [Text;...] database specification is ignored.

So, for an existing table named [MyExistingTable]

CREATE TABLE MyExistingTable (
    id INT,
    firstname TEXT(255)
)

a text file named "toImport.txt"

1;Larry
2;Hank
3;Artie

and a "schema.ini" file

[toImport.txt]
Format=Delimited(;)

the command

cmd.CommandText =
        "INSERT INTO MyExistingTable " &
        "SELECT F1 AS id, F2 AS firstname " &
        "FROM [Text;FMT=Delimited;HDR=No;CharacterSet=850;DATABASE=C:\Users\Public].[toImport.txt];"

will fail because it will interpret the first line of "toImport.txt" as column names: [1] and [Larry], instead of the default [F1] and [F2] that are assigned when no column names are included.

The easiest fix is to add ColNameHeader=False to the "schema.ini" file

[toImport.txt]
Format=Delimited(;)
ColNameHeader=False

You can also get more sophisticated and add complete column specifications into the "schema.ini" file (see the documentation for details).

Another possible cause:

The error can also be caused by a "schema.ini" file that is saved with a UTF-8 BOM (byte order mark). The Jet "Text" engine pre-dates Unicode and only supports two character sets – "ANSI" or "OEM" – for both the data file and "schema.ini" file.