我正在使用Access 2003(由于具有兼容性,不得不这样做)来修改一个10年前的项目,而不是我自己做的。 我在执行此查询时遇到错误:
INSERT INTO ClientiContratto ( ID, CLIENTE, DATA, PERIODO, IMPORTO, FATTURATO )
SELECT [Forms]![InserisciContratto]![Cliente] AS Espr1, (SELECT Nome from TAnagrafica WHERE TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]) AS Espr2, [Forms]![InserisciContratto]![Data] AS Espr3, [Forms]![InserisciContratto]![Periodo] AS Espr4, [Forms]![InserisciContratto]![Importo] AS Espr5, False AS Espr6;
由于
返回错误 (SELECT Nome from TAnagrafica WHERE TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]) AS Espr2
如果我独立执行此查询,它的作用就像一个魅力,但是当涉及将查询插入INSERT INTO ... SELECT语句时,它返回(从意大利语翻译而来):
运行时错误'3000':保留错误(-3025):没有消息 对于此错误。
目的是基于活动窗体中找到的值在表中插入一些新值,并且不起作用的部分代码应在表中搜索链接到[InserisciContratto]的值![Cliente]实际值。
我在做什么错?也许是因为我无法在先前的SELECT查询中执行SELECT子查询?
任何帮助将不胜感激。
答案 0 :(得分:1)
您可以使用DLookUp
而不是子查询来解决此问题:
DLookUp("Nome", "TAnagrafica", "TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]")
请注意,您可以在表单控件上或查询中使用DLookUp
。两者均有效。在查询中,它看起来像这样:
INSERT INTO ClientiContratto ( ID, CLIENTE, DATA, PERIODO, IMPORTO, FATTURATO )
SELECT [Forms]![InserisciContratto]![Cliente] AS Espr1, DLookUp("Nome", "TAnagrafica", "TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]") AS Espr2, [Forms]![InserisciContratto]![Data] AS Espr3, [Forms]![InserisciContratto]![Periodo] AS Espr4, [Forms]![InserisciContratto]![Importo] AS Espr5, False AS Espr6;
此类错误的另一种常见来源是Access在使用子查询而不是从真实表中查询时表现出挑剔。通过使用子查询作为主查询,您可以轻松解决此问题。请注意,这确实需要子查询始终返回结果,否则将不会插入任何行:
INSERT INTO ClientiContratto ( ID, CLIENTE, DATA, PERIODO, IMPORTO, FATTURATO )
SELECT [Forms]![InserisciContratto]![Cliente] AS Espr1, Nome AS Espr2, [Forms]![InserisciContratto]![Data] AS Espr3, [Forms]![InserisciContratto]![Periodo] AS Espr4, [Forms]![InserisciContratto]![Importo] AS Espr5, False AS Espr6
FROM TAnagrafica
WHERE TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]