add new item in combobox if...

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

add new item in combobox if...

Post by sal21 »

In a combobox have dates

01/08/2021
15/06/2021
...
ecc...

i have myvardate=02/08/2021. i need to add in combobox only if not exists.
i need to loop the item in combobox, with a if condition, before to add myvardate, or exists a othre fast method?

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

Re: add new item in combobox if...

Post by HansV »

Yes, you need to loop through the items of the combo box and compare them to the value of myvardate. If none matches, then use AddItem to add the new value.
Regards,
Hans

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

Re: add new item in combobox if...

Post by sal21 »

HansV wrote:
13 Oct 2021, 19:34
Yes, you need to loop through the items of the combo box and compare them to the value of myvardate. If none matches, then use AddItem to add the new value.
Wath you think about:
Private Sub AGG_GIORNO_CASSA(GIORNO)

With Me.CGIORNO1

For K = 1 To .ListCount - 1
If GIORNO = Split(.List(K), "-")(1) Then Exit For
Next K

If K = .ListCount - 1 Then
.AddItem UCase(Format(GIORNO, "DDD")) & "-" & GIORNO
End If

.Refresh

End With
'
End Sub

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

Re: add new item in combobox if...

Post by HansV »

The line

If K = .ListCount - 1 Then

should be

If K = .ListCount Then

If the loop runs to completion (Exit For is not executed), the loop index will be 1 more than the upper bound.
Regards,
Hans

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

Re: add new item in combobox if...

Post by sal21 »

HansV wrote:
14 Oct 2021, 06:29
The line

If K = .ListCount - 1 Then

should be

If K = .ListCount Then

If the loop runs to completion (Exit For is not executed), the loop index will be 1 more than the upper bound.
:cheers: