fast find in array

User avatar
sal21
PlatinumLounger
Posts: 4349
Joined: 26 Apr 2010, 17:36

fast find in array

Post by sal21 »

Code: Select all

Dim MIO_ARRAY_FESTE () as Variant 

 Sub ARRAY_FESTIVI()

    SQL = "SELECT DATE FROM FESTIVI"
    Set RS = New ADODB.Recordset
    RS.CursorLocation = adUseClient
    RS.Open SQL, CNN, adOpenForwardOnly, adLockReadOnly
    RS.Sort = ("DATE")

    RS.MoveFirst
    Erase MIO_ARRAY_FESTE()
    MIO_ARRAY_FESTE = RS.GetRows()
    RS.Close
    Set RS = Nothing

End Sub
how to find in the array mydate (as date)

User avatar
HansV
Administrator
Posts: 78410
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fast find in array

Post by HansV »

Why don't you change the SQL to select only the date that you want?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4349
Joined: 26 Apr 2010, 17:36

Re: fast find in array

Post by sal21 »

HansV wrote:
15 Jul 2020, 12:10
Why don't you change the SQL to select only the date that you want?
but in effect, based the last post, to fill a listview with date in month, is simply to chech DOMENICA, but my prob is to check, during the loop in a list, also a national day and Christmas, Easter...ecc

User avatar
HansV
Administrator
Posts: 78410
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fast find in array

Post by HansV »

You could create the following function in a module:

Code: Select all

Function InArray(d, arr) As Boolean
    Dim v
    For Each v In arr
        If v = d Then
            InArray = True
            Exit For
        End If
    Next v
End Function
And then use code like

Code: Select all

    If InArray(mydate, MIO_ARRAY_FESTE) Then
        ' It is a holiday
        ...
    End If
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4349
Joined: 26 Apr 2010, 17:36

Re: fast find in array

Post by sal21 »

HansV wrote:
15 Jul 2020, 12:48
You could create the following function in a module:

Code: Select all

Function InArray(d, arr) As Boolean
    Dim v
    For Each v In arr
        If v = d Then
            InArray = True
            Exit For
        End If
    Next v
End Function
And then use code like

Code: Select all

    If InArray(mydate, MIO_ARRAY_FESTE) Then
        ' It is a holiday
        ...
    End If
OK
wath you think about:

If InArray(LADATA, MIO_ARRAY_FESTE) Or Weekday(LADATA) = 1 Then
ITM.ForeColor = vbRed
End If

User avatar
HansV
Administrator
Posts: 78410
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fast find in array

Post by HansV »

That will work, but I'd do it slightly differently for efficiency:

Code: Select all

        If Weekday(LADATA) = 1 Then
            ITM.ForeColor = vbRed
        ElseIf InArray(LADATA, MIO_ARRAY_FESTE) Then
            ITM.ForeColor = vbRed
        End If
That way, the code doesn't have to check the array for a Sunday.
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: fast find in array

Post by LisaGreen »

Hi,

Coming to this late. I think you could save a teeny bit of time by putting the SORT into the SQL statement.

HTH
Lisa