访问sql语句什么都不返回!

时间:2010-10-21 17:46:12

标签: sql sql-server ms-access vba

我遇到访问sql SELECT语句的问题。问题是,当后端是ACCESS-2007时,它可以工作;但是当后端是sql-server-2008时,它不会返回任何内容。这是声明:

SELECT IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Occurrence Code], Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between  Forms!Meeting_Reasons_Frequency!Text4  And  Forms!Meeting_Reasons_Frequency!Text2 )) And [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.*'
GROUP BY IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])
HAVING ((Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]))<>0)
ORDER BY IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])

请注意,不起作用的是:Forms!Meeting_Reasons_Frequency!Text4Forms!Meeting_Reasons_Frequency!Text2 - 当我替换具有常数值的那些时,它们起作用;但问题是它们由于某种原因没有正确读取文本框中的内容。

再次:这个查询正在使用FINE作为后端访问,但是当后端切换到sql server时它无法正常工作!原因再一次是因为forms!text的事情。有谁知道为什么?

更新 感谢bob的建议,我在sql-server上运行了配置文件,发现了一些有趣的东西。它确实正常工作,但它会像这样返回日期:

@P1 datetime,@P2 datetime','2010-04-30 00:00:00','2010-04-01 00:00:00'

3 个答案:

答案 0 :(得分:1)

您不能将控件插入字符串中。

Between  Forms!Meeting_Reasons_Frequency!Text4  
 And  Forms!Meeting_Reasons_Frequency!Text2 

您需要将它们分开,然后在即时窗口中查看您的SQL字符串。

"<beginning query> Between " & Forms!Meeting_Reasons_Frequency!Text4 & _
" And " & Forms!Meeting_Reasons_Frequency!Text2 & " <rest of query>"

如果您打破此字符串的创建,您会注意到,如果将鼠标悬停在控件名称上,将显示文本框值。

答案 1 :(得分:1)

尝试将表单控件引用定义为参数:

  PARAMETERS [Forms]![Meeting_Reasons_Frequency]![Text4] DateTime, 
     [Forms]![Meeting_Reasons_Frequency]![Text2] DateTime;
  SELECT IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Occurrence Code], Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count]
  FROM [Lab Occurrence Form]
  WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between  Forms!Meeting_Reasons_Frequency!Text4  And  Forms!Meeting_Reasons_Frequency!Text2 )) And [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.*'
  GROUP BY IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])
  HAVING ((Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]))<>0)
  ORDER BY IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])

答案 2 :(得分:-2)

这是在黑暗中拍摄的,但我认为这是你的喜欢。 Access使用*作为通配符,而SQL Server使用%。即使语法被翻译成t-sql,我也想知道它是否也将*改为%。尝试一下......