Advertisement
Dotterbart

Search nearest number

Jan 2nd, 2024
1,782
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 1.03 KB | Source Code | 0 0
  1. Function SearchNearest(intSearch As Integer) As Integer
  2. Dim DB As DAO.Database
  3. Dim RS As DAO.Recordset
  4. Dim strSQL, strMax, strMin As String
  5. Dim intMax, intMin As Integer
  6. Dim intDifMax, intDifMin As Integer
  7.  
  8. Set DB = CurrentDb
  9.  
  10. strSQL = "SELECT TOP 1 BuchArtikel_ID FROM Buchung WHERE BuchArtikel_ID <= " & CStr(intSearch) & " ORDER BY Buchung.BuchArtikel_ID DESC;"
  11. Set RS = DB.OpenRecordset(strSQL)
  12. If RS.RecordCount > 0 Then
  13.     intMin = RS!BuchArtikel_ID
  14. Else
  15.     intMin = 0
  16. End If
  17.  
  18. strSQL = "SELECT TOP 1 BuchArtikel_ID FROM Buchung WHERE BuchArtikel_ID >= " & CStr(intSearch) & " ORDER BY Buchung.BuchArtikel_ID ASC;"
  19. Set RS = DB.OpenRecordset(strSQL)
  20.  
  21. If RS.RecordCount > 0 Then
  22.     intMax = RS!BuchArtikel_ID
  23. Else
  24.     intMax = 0
  25. End If
  26.  
  27. intDifMax = (intSearch - intMax) * -1
  28. intDifMin = intSearch - intMin
  29. Debug.Print intDifMax
  30. Debug.Print intDifMin
  31.  
  32. If intDifMin > intDifMax Then
  33.     SearchNearest = intMax
  34. Else
  35.     SearchNearest = intMin
  36. End If
  37.  
  38. RS.Close
  39. DB.Close
  40. Set DB = Nothing
  41. Set RS = Nothing
  42. End Function
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement