473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Trouble with SQL Statement in Function

I would appreciate help with the following Function.

The SQL statement below returns a Recordcount = 0, yet,
when it runs in the query designer it returns the correct count of 40

Private Function ApplyFilter()
Dim intCount As Single
Dim dtStartDate As Date
Dim dtEndDate As Date
Dim strRange As String
Dim strCondition As String
Dim strRange2 As String
Dim strCondition2 As String
Dim strSQL As String
Dim cn As ADODB.Connection
Dim rst As ADODB.Recordset

On Error GoTo EH
Set cn = CurrentProject.Connection
dtStartDate = Me!StartDate
dtEndDate = Me!EndDate
strRange = "([Admission Data].[Admitting Date] >= #" & dtStartDate
& "# AND [Admission Data].[Admitting Date] <= #" & dtEndDate & "#)"
strCondition = "([Admission Data].[Account Number] NOT LIKE 'G%')
AND "

DoCmd.SetWarnings (True)
With cn
Set rst = New ADODB.Recordset

'get Theraputic PTT Heparin
strSQL = "SELECT [Admission Data].AdmissionID, [Admission
Data].[Admitting Date], [Admission Data].[Account Number],
Min(Labs.LabValue) AS NumLabValues"
strSQL = strSQL & " FROM ([Admission Data] LEFT JOIN Labs ON
[Admission Data].AdmissionID = Labs.AdmissionID) LEFT JOIN Medications
ON [Admission Data].AdmissionID = Medications.AdmissionID"
strSQL = strSQL & " WHERE (((Medications.[Medication
Anticoag]) Like 'Heparin')"
strSQL = strSQL & " And ((Medications.[Medication Route]) Like
'25000*') And ((Labs.[Lab Type]) = 'PTT')"
strSQL = strSQL & " And ((IsNumeric(Labs.LabValue)) = True) And
((CSng(Labs.LabValue)) >= 50.5"
strSQL = strSQL & " And (CSng(Labs.LabValue)) <= 72.4))"
strSQL = strSQL & " GROUP BY [Admission Data].AdmissionID,
[Admission Data].[Admitting Date], [Admission Data].[Account Number]"
strSQL = strSQL & " HAVING " & strCondition & strRange & ";"

rst.Open strSQL, cn, adOpenStatic, adLockReadOnly

intCount = rst.RecordCount
Me!txtTherPTT = intCount
rst.Close
End With

' clean up
cn.Close
Set rst = Nothing
Set cn = Nothing
Exit Function

EH:
' clean up
If Not rst Is Nothing Then
If rst.State = adStateOpen Then rst.Close
End If
Set rst = Nothing

If Not cn Is Nothing Then
If cn.State = adStateOpen Then cn.Close
End If
Set cn = Nothing

If Err <0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If

End Function

This is the query in the Query Designer

SELECT [Admission Data].AdmissionID, [Admission Data].[Admitting
Date],
[Admission Data].[Account Number], Min(Labs.LabValue) AS NumLabValues
FROM ([Admission Data] LEFT JOIN Labs ON [Admission Data].AdmissionID
= Labs.AdmissionID)
LEFT JOIN Medications ON [Admission Data].AdmissionID =
Medications.AdmissionID
WHERE ((Medications.[Medication Anticoag]) Like 'Heparin')
And (Medications.[Medication Route] Like '25000*')
And (Labs.[Lab Type] = 'PTT')
And (IsNumeric(Labs.LabValue) = True)
And (CSng(Labs.LabValue) >= 50.5 And CSng(Labs.LabValue) <= 72.4)
GROUP BY [Admission Data].AdmissionID, [Admission Data].[Admitting
Date], [Admission Data].[Account Number]
HAVING ([Admission Data].[Admitting Date] >= #4/1/2007# AND [Admission
Data].[Admitting Date] <= #4/30/2007#);

Sep 19 '07 #1
2 1747
When you form sql in code, you *always* need to force the date format into
usa format.
I would use:

format([Admitting Date],"mm/dd/yyyy")

to make life more easy, build a function like:
Public Function qudate(myDate As Date) As String

' This routine requires a DATE date type. Etiher a var, or
' a field on a form is ok.
' returns a formatted string of date, surrounded with # signs
' format is mm/dd/yyyy as required for sql, regardless of date setting

qudate = "#" & Format(myDate, "mm/dd/yyyy") & "#"
End Function
Then use:
dtStartDate = Me!StartDate
dtEndDate = Me!EndDate
strRange = "([Admission Data].[Admitting Date] >= " qudate(dtStartDate)
& _
" AND [Admission Data].[Admitting Date] <= " &
qudate(dtEndDate)

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com


Sep 19 '07 #2
"Albert D. Kallal" <Pl*******************@msn.comwrote in
news:qTdIi.9104$A55.1718@pd7urf2no:
I would use:

format([Admitting Date],"mm/dd/yyyy")

to make life more easy, build a function like:

Public Function qudate(myDate As Date) As String
Er, the canonical solution for this is the DateSerial() function.

However, if you're going to use format, use one of the unambiguous
ones, like "long".

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Sep 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I have a parameter defined in a module, called PREVIEW. Many functions use it's value to modify their behavior. A function called dispatch checks the user arguments in sys.argv and calls...
3
by: Jason | last post by:
I am having trouble using the CONTAINS function in sql server(enterprise manager). I am typing the following: Select * FROM mytable WHERE CONTAINS(myfield,'mystring') This returns the...
7
by: | last post by:
I fail to understand why that the memory allocated in the void create(int **matrix) does not remain. I passed the address of matrix so shouldn't it still have the allocated memory when it returns...
9
by: Donius | last post by:
Hey everyone, i am doing some stuff where i'd like to pop up a little confirmation before a user clicks on a 'delete' link. Just trying to keep the markup clean, i added an attribute ...
1
by: akg250978 | last post by:
ok here is my problem i created the JS to insert rows in an html doc, this works perfectly but if i was to refresh the page or leave it and return to it L8 the rows have diappeared here is the...
3
by: Michael | last post by:
Hi all, I'm having trouble PInvoking a TCHAR within a struct. I'll paste the specific struct's API definition below. I've tried so many numerous variations. The main Win32 error I get is...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
11
by: copx | last post by:
For some reason I cannot add a const qualifier to a typedefed pointer type if said type is used for a return value. It does work if the type is used for a parameter. I do not see the logic behind...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.