Solved Filtering range of records

quest

Member
Local time
Today, 19:08
Joined
Nov 26, 2024
Messages
82
Hi,
I am trying to make query that will filter range of records. how this is not with maximum and minimum i tried with between same like with date from to but that way i get only first and last not the records between

record with docnumber 347 like in this example can be smaller than records with number 348 and 349 but also can be bigger. so with expresion between i got reocrd with docnumber 348 and 349 but not the record with docnumber 347 i want 347 too.
not successful with copy so here is picture instead
 

Attachments

  • Untitled.png
    Untitled.png
    9.5 KB · Views: 32
Last edited:
Filter on range of what? What field are you applying criteria to - DateOfReceiving? Your dates are formatted for non-U.S. structure. Review http://allenbrowne.com/ser-36.html
Post your attempted query SQL statement.
 
Hi,
I am trying to make query that will filter range of records. how this is not with maximum and minimum i tried with between same like with date from to but that way i get only first and last not the records between

record with docnumber 347 like in this example can be smaller than records with number 348 and 349 but also can be bigger. so with expresion between i got reocrd with docnumber 348 and 349 but not the record with docnumber 347 i want 347 too.
not successful with copy so here is picture instead
Please describe this situation as a narrative. What is the end goal? Explain things like maximum and minimum in terms of the specific values you want to compare. Are you talking about a date range, a number range, or something else? Which fields should be filtered for max and min?
 
I agree with the others---what are you talking about exactly? Give us a few examples so we understand the issue. Between in Access includes the end points.
 
sorry for delay i have been absent. maybe is best to show you sample database. this database with conditional formatting shows in red field {transfer] which is with mistake. however all conditions work except the last one. like in the query range of records make problem. you can see record with docnumber 347 subnumber 3 says that between docnumber 348 and 349 there is next record 347 with subnumer 4 and that record exist so it should not be in red. i cant fix that. so i send you the sample database to see what i am talking about.
 

Attachments

Can we assume you are referring to frmDocuments? You need to answer questions from those who are attempting to help you. No one here can read your mind.

Your function IsTransferValid() should have a header with the design specifications.
 
"you can see record with docnumber 347 subnumber 3 says that between docnumber 348 and 349 there is next record 347 with subnumer 4 and that record exist"??
How would a reader know what subnumber 3 means?
As DHookom said 'No one here can read your mind.' I suggest you leads us through a scenario in simple terms of what we're looking at, and what it is doing/not doing that has to change.
 
Can we assume you are referring to frmDocuments? You need to answer questions from those who are attempting to help you. No one here can read your mind.

Your function IsTransferValid() should have a header with the design specifications.
yes frmDocuments display form on opening shows in red with conditional formatting mistakes made when entering data. it is to know that is not valid and should be fixed. when mistake is fixed red disappear. conditional formatting is based on the code in module1. however for this record and condition code don't work. for transfer field contain "n/a", works for cases with year like 768/2019 works. when value is like 7/8 both parts are numbers lenght to 3 digits first rule that second part is equal to first part plus one 7+1=8 works. connditition that not works is like record docnummber 347 subnumber 3 has transfer field 348/349 should not be in red because record with 347 exist between 348/349 if there is no record with 347 between 348 and 349 then should be red because pointing to records that does not exist.
 
about subnumber. each docnumber means document about something like file for something. if that same file continues next document will have same docnumber but subnumer will rise it means first is with subnumber 1 which means first document next is second etc so in the example with docnumber 347 and subnumber 3 transfer field means this document continues to docnumber 347 again and subnumber 4 now and it is placed between docnumbers 348 and 349
 
Here is a quick review of your IsTransferValid function (via Chatgpt)

Issue / OddityComment
DocNumber parameter is never usedIf the caller supplies it, you might have planned extra validation that hasn’t been coded yet.
Third check in the range logic seems counter‑intuitiveSQL ... DocNumber NOT IN (lower, upper) AND DocNumber > lower AND DocNumber < upper will be empty when no document is in‑between. The function currently marks the transfer invalid if the recordset is empty, meaning it demands at least one “in‑between” document. Most business rules want the opposite (ensure there are no missing numbers). If you really want to reject gaps, you probably meant If Not rs.EOF Then IsTransferValid = False.
SELECT * when you only test existenceReplace with SELECT 1 or COUNT(*) to reduce network traffic.
Multiple open/close calls inside a loopConsider parameterised queries or a single read if performance ever becomes an issue.
No explicit Option Explicit and undeclared variables inside blocks (trgDoc, trgYear, lowerDoc, upperDoc)Declare all at the top, or use Option Explicit.
thisYear is derived from DateOfReceiving, not the system clockGood—keeps validation anchored to the document’s own year, not “today”.


IsTransferRow(Transfer As String) As Boolean

A convenience wrapper; simply tells you whether the Transfer field might contain something worth validating.



Code:
IsTransferRow = Not (Trim(Transfer) = "N/A" Or Trim(Transfer) = "")
If it’s blank or “N/A”, it’s not a transfer row (returns False). Otherwise it is (returns True).

How the two functions might be used together​

  1. IsTransferRow runs first inside a form, query, or import routine to skip non‑transfer lines quickly.
  2. For every row that passes, IsTransferValid is called to ensure the reference(s) point to real, allowable documents.
 
Why do you have IsTransferValid function in two places? One is behind form and one is in general module. The module procedure does not get used.

Still not clear why 347 with 348/349 should not be highlighted. As is, it meets the criteria for Conditional Formatting.
 
Why do you have IsTransferValid function in two places? One is behind form and one is in general module. The module procedure does not get used.

Still not clear why 347 with 348/349 should not be highlighted. As is, it meets the criteria for Conditional Formatting.
i don't know how it meets criteria but should not. first record in red 450/460 is red and is true because is not 450/451. 348/349 is true because there is a record with docnumber 347 between docnumber 348 and docnumber 349 so should not be red. if record with docnumber 347 not exist between docnumber 348 and docnumber 349 then should be red
 
Record 347 with 348/349 should not be red because there is ANOTHER 347 record (subnumber 4) between 348 and 349 by date? Your code logic does not test for that date criteria. You have a test for: DocNumber > " & lowerDoc & " AND DocNumber < " & upperDoc
This criteria has nothing to do with record order - it is numerical and 347 does not meet the condition of: >348 AND <349.
 
Last edited:
I would open the debug window (press Ctrl+G) to test the functions with entered values. You can set a break point and step through the code line by line.
 
Record 347 with 348/349 should not be red because there is ANOTHER 347 record (subnumber 4) between 348 and 349 by date? Your code logic does not test for that date criteria. You have a test for: DocNumber > " & lowerDoc & " AND DocNumber < " & upperDoc
This criteria has nothing to do with record order - it is numerical and 347 does not meet the condition of: >348 AND <349.
i don't know how many versions i have tried i have new code but still code not work. queries i tested work perfectly but code no. here are two queries first:
SELECT d.*
FROM tblDocuments AS d
WHERE d.DocNumber = 347
AND Year(d.DateOfReceiving) = 2016
AND d.DateOfReceiving > (
SELECT Min(DateOfReceiving)
FROM tblDocuments
WHERE DocNumber IN (348, 349)
AND Year(DateOfReceiving) = 2016
)
AND d.DateOfReceiving < (
SELECT Max(DateOfReceiving)
FROM tblDocuments
WHERE DocNumber IN (348, 349)
AND Year(DateOfReceiving) = 2016
);
and second
PARAMETERS prmDocNumber Long, prmTransfer Text ( 255 );
SELECT d.*
FROM tblDocuments AS d
WHERE d.DocNumber = prmDocNumber
AND Year(d.DateOfReceiving) =
(SELECT Year(DateOfReceiving)
FROM tblDocuments
WHERE DocNumber = prmDocNumber AND Transfer = prmTransfer)

AND d.DateOfReceiving > (
SELECT Min(DateOfReceiving)
FROM tblDocuments
WHERE DocNumber IN (
Val(Left(prmTransfer, InStr(prmTransfer, "/") - 1)),
Val(Mid(prmTransfer, InStr(prmTransfer, "/") + 1))
)
AND Year(DateOfReceiving) =
(SELECT Year(DateOfReceiving)
FROM tblDocuments
WHERE DocNumber = prmDocNumber AND Transfer = prmTransfer)
)

AND d.DateOfReceiving < (
SELECT Max(DateOfReceiving)
FROM tblDocuments
WHERE DocNumber IN (
Val(Left(prmTransfer, InStr(prmTransfer, "/") - 1)),
Val(Mid(prmTransfer, InStr(prmTransfer, "/") + 1))
)
AND Year(DateOfReceiving) =
(SELECT Year(DateOfReceiving)
FROM tblDocuments
WHERE DocNumber = prmDocNumber AND Transfer = prmTransfer)
);
 
Don't see what those queries have to do with functions and the issue I identified. Do you understand what I described?
What change did you make in code? Did you add criteria for date range?
 
Don't see what those queries have to do with functions and the issue I identified. Do you understand what I described?
What change did you make in code? Did you add criteria for date range?
here is last code
Public Function IsTransferValid(DocNumber As Long, Transfer As String, DateOfReceiving As Date) As Boolean
On Error GoTo HandleError

Dim parts() As String
Dim startDoc As Long, endDoc As Long
Dim thisYear As Long
Dim rs As DAO.Recordset
Dim dateStart As Date, dateEnd As Date
Dim sql As String

Debug.Print "---- Start Check ----"
Debug.Print "DocNumber: " & DocNumber
Debug.Print "Transfer: " & Transfer
Debug.Print "DateOfReceiving: " & DateOfReceiving

If Trim(Transfer) = "" Or UCase(Transfer) = "N/A" Then
Debug.Print "Transfer is empty or N/A"
IsTransferValid = True
Exit Function
End If

If InStr(Transfer, "/") = 0 Then
Debug.Print "No slash found in Transfer"
IsTransferValid = False
Exit Function
End If

parts = Split(Transfer, "/")
If UBound(parts) <> 1 Then
Debug.Print "Transfer not in expected format (X/Y)"
IsTransferValid = False
Exit Function
End If

If Not IsNumeric(parts(0)) Or Not IsNumeric(parts(1)) Then
Debug.Print "One of the parts is not numeric"
IsTransferValid = False
Exit Function
End If

startDoc = CLng(parts(0))
endDoc = CLng(parts(1))
Debug.Print "StartDoc: " & startDoc & ", EndDoc: " & endDoc

If endDoc <> startDoc + 1 Then
Debug.Print "EndDoc is not StartDoc + 1"
IsTransferValid = False
Exit Function
End If

thisYear = Year(DateOfReceiving)
Debug.Print "Year of receiving: " & thisYear

sql = "SELECT COUNT(*) AS C FROM tblDocuments WHERE DocNumber IN (" & startDoc & "," & endDoc & ") AND Year(DateOfReceiving)=" & thisYear
Set rs = CurrentDb.OpenRecordset(sql)
Debug.Print "Boundary records found: " & rs!C
If rs!C < 2 Then
Debug.Print "One or both boundary documents missing"
IsTransferValid = False
rs.Close
Exit Function
End If
rs.Close

sql = "SELECT DateOfReceiving FROM tblDocuments WHERE DocNumber=" & startDoc & " AND Year(DateOfReceiving)=" & thisYear
Set rs = CurrentDb.OpenRecordset(sql)
If rs.EOF Then
Debug.Print "StartDoc date not found"
IsTransferValid = False
rs.Close
Exit Function
End If
dateStart = rs!DateOfReceiving
Debug.Print "Start Date: " & dateStart
rs.Close

sql = "SELECT DateOfReceiving FROM tblDocuments WHERE DocNumber=" & endDoc & " AND Year(DateOfReceiving)=" & thisYear
Set rs = CurrentDb.OpenRecordset(sql)
If rs.EOF Then
Debug.Print "EndDoc date not found"
IsTransferValid = False
rs.Close
Exit Function
End If
dateEnd = rs!DateOfReceiving
Debug.Print "End Date: " & dateEnd
rs.Close

sql = "SELECT 1 FROM tblDocuments " & _
"WHERE DocNumber = " & DocNumber & _
" AND Year(DateOfReceiving) = " & thisYear & _
" AND DateOfReceiving > #" & Format(dateStart, "mm/dd/yyyy") & "# " & _
" AND DateOfReceiving < #" & Format(dateEnd, "mm/dd/yyyy") & "#"

Debug.Print "Check range SQL: " & sql
Set rs = CurrentDb.OpenRecordset(sql)
Debug.Print "Record found between dates: " & Not rs.EOF
IsTransferValid = Not rs.EOF
rs.Close

Debug.Print "Result: " & IsTransferValid
Debug.Print "---- End Check ----"
Exit Function

HandleError:
Debug.Print "ERROR: " & Err.Description
IsTransferValid = False
End Function
 
As per my previous post to troubleshoot, you should set a breakpoint to step through your code with your values like:
1751311755305.png

Use the F8 key to step through the code line by line to determine where your logic is off.
 
As per my previous post to troubleshoot, you should set a breakpoint to step through your code with your values like:
View attachment 120410
Use the F8 key to step through the code line by line to determine where your logic is off.
June7 was right i have code twice in the module and in the form i don't know how it get in the form maybe too many version one by mistake end there. i deleted code in the form i now checking one by one 348/349 like is fixed only left transfer fields with double values like 349/2016 79/2019
 

Users who are viewing this thread

Back
Top Bottom