vba 参数太少。预期 1 - 但我有一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6125382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Too few parameters. Expected 1 - but I have one
提问by stevendesu
So I've recently been working on a VBA script to transfer an entire database of student medical records from their old one-table, 68-field, flat system to a new dynamic system with 24 related tables.
所以我最近一直在研究一个 VBA 脚本,以将整个学生医疗记录数据库从他们旧的单表、68 字段、平面系统转移到一个具有 24 个相关表的新动态系统。
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)
Flat
是一个存储平面数据库名称的字符串(这是因为我正在使用一个虚拟数据库,所以他们需要一种方便快捷的方法来修改我为处理真实事物所做的代码)
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 studentID
s, so I must use the name, school, and social to determine that I am dealing with the same child and look up their studentID
rstFrom
只需要包含与我目前正在复制到的表相关的 68 字段表的列(在这种情况下,FamilyHistory 表实际上只需要studentID
和FamilyHistory
) - 请注意原始表没有分配唯一的studentID
s,所以我必须使用姓名、学校和社会来确定我正在与同一个孩子打交道并查找他们的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:
显然我有 1 个参数,它是:
"select " & Flat & ".Student," & Flat & ".School," & Flat & ".Social," & Flat & ".FamilyHist from " & Flat & " WHERE 1=1"
"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"
"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.
使用 Access VBA 时需要 where 1=1 否则它只返回匹配的第一条记录,而不是所有匹配的记录。
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"
"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.
它能够通过这条线没问题,尽管这会在稍后我需要读取我没有检索到的其他数据时导致问题。不过,我认为有趣的是,错误似乎来自 SQL 而不是 OpenRecordset 函数。
回答by shahkalpesh
Check the field names in the SQL vs what you have in table.
检查 SQL 中的字段名称与表中的名称。
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.
我认为,要么是上述 SQL 中的字段名称拼写错误,要么表中没有一个或多个(SQL 语句的)字段。
回答by Jeremy Whitcher
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++.
插入查询中的文本参数需要用单引号括起来。我在使用 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());
}