开发者

Too few parameters. Expected 1 - but I have one

So I've recently been working on a VBA script to transfer an entire database of student medical records from th开发者_如何学Ceir old one-table, 68-field, flat system to a new dynamic system with 24 related tables.

There was no issue for the first few tables, but then I ran into this. The line of code throwing the error is:

Set rstFrom = CurrentDb.OpenRecordset("select " & Flat & ".Student," & Flat & ".School," & Flat & ".Social," & Flat & ".FamilyHist from " & Flat & " WHERE 1=1")`

Flat is a String which stores the name of the flat database (this is because I'm working with a dummy database so they will need a convenient and quick way to modify the code I make to work on the real thing)

rstFrom needs to contain only the columns of the 68-field table which are relevant to the table that I'm copying to at the moment (in this case, the FamilyHistory table which really just needs the studentID and FamilyHistory) - note that the original table did not assign unique studentIDs, so I must use the name, school, and social to determine that I am dealing with the same child and look up their studentID

When this line of code runs I get the following error:

Run-time error '3061':

Too few parameters. Expected 1.

Clearly I have 1 parameter, it's:

"select " & Flat & ".Student," & Flat & ".School," & Flat & ".Social," & Flat & ".FamilyHist from " & Flat & " WHERE 1=1"

(which after parsing is):

"select Demos.Student,Demos.School,Demos.Social,Demos.FamilyHist from Demos WHERE 1=1"

The where 1=1 is required when working with Access VBA or else it only returns the first record which matches, not all matching records.

Has anyone else had this same problem as resolved it? I did notice one thing. When I change the parameter to:

"select Demos.Student from Demos WHERE 1=1"

It is able to get past this line no problem, although this causes issues later on when I need to read other data that I did not retrieve. I thought it was interesting, though, that the error seems to be coming from the SQL and not the OpenRecordset function.


Check the field names in the SQL vs what you have in table.

I think, either the field name in above SQL is misspell or you don't have one or more field (of SQL statement) in the table.


The text parameters in the insert query need to have a single quote around them. I ran into the same problem with a query using Visual C++.

Here's the code I ended up using...

void FileInterface::TblWrite(CDatabase* db, rec* r)
{
   string sqlQuery = "insert into THREATS(ID,CODE,ID,LAT,LON,SHOW_A,SHOW_B) Values(" +
      to_string((_Longlong)r->num) + "," +
      to_string((_Longlong)r->code) + "," +
      "'" + r->id + "'" + "," + 
      to_string((long double)r->lat) + "," +
      to_string((long double)r->lon) + "," +
      "1" + "," +
      "1" +
      ")";
   db->ExecuteSQL(sqlQuery.c_str());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜