开发者

access sql statement is returning nothing!

i am having a problem with access sql SELECT statement. the problem is that when the backend is ACCESS-2007 it works; however when the backend is sql-server-2008, it is not returning anything. here is the statement:

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)])

please note that what is not working is this: Forms!Meeting_Reasons_Frequency!Text4 and this Forms!Meeting_Reasons_Frequency!Text2 - when i replace th开发者_C百科ose with constant values they work; however the trouble is that it they are not reading correctly what is in the textbox for some reason.

again: this query is working FINE with access as a backend, however when the backend switches to sql server it is not working! the reason again is because of the forms!text thing. does anyone know why?

update thanks to bob's suggestion i ran profile on sql-server and found something funny. it is indeed working correctly, however it is returning the date like this:

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


Try defining your form control references as parameters:

  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)])


You can't just jam the controls into your string.

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

You need to separate them out and then look at your SQL string in the immediate window.

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

If you break on the creation of this string, you'll notice that the text box values will be displayed if you hover your mouse over the control name.


This is a shot in the dark but I think it is your LIKE's. Access uses *'s as the wildcards while SQL Server uses %'s. Even if the syntax is translated to t-sql, I wonder if it also changes *'s to %'s. Try it out...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜