How to check if value already exists in listbox (1 Viewer)

Khalid_Afridi

Registered User.
Local time
Tomorrow, 01:31
Joined
Jan 25, 2009
Messages
491
Dear Friends!

How to check if value already exists in listbox?

Thanks
 

wiklendt

i recommend chocolate
Local time
Tomorrow, 08:31
Joined
Mar 10, 2008
Messages
1,746
looking at the list works for me.

(if you want a better answer, provide a better question.)
 

ajetrumpet

Banned
Local time
Today, 17:31
Joined
Jun 22, 2007
Messages
5,638
looking at the list works for me.

(if you want a better answer, provide a better question.)

I can't help but laugh at this wik. Nice answer! :)


and btw, the better answer is:
PHP:
dim i as integer

for i = 0 to me.listbox.listcount - 1

   if me.listbox.column(0, i) = myvalue then
      msgbox "it exists!"
         exit sub
   end if

next i
 

wiklendt

i recommend chocolate
Local time
Tomorrow, 08:31
Joined
Mar 10, 2008
Messages
1,746
I can't help but laugh at this wik. Nice answer! :)


and btw, the better answer is:
PHP:
dim i as integer

for i = 0 to me.listbox.listcount - 1

   if me.listbox.column(0, i) = myvalue then
      msgbox "it exists!"
         exit sub
   end if

next i

ooo! nice solution :)
 

wiklendt

i recommend chocolate
Local time
Tomorrow, 08:31
Joined
Mar 10, 2008
Messages
1,746
actually, my favourite method to find items in a real-time way within a listbox is to use a textbox that is the source for a criterion inside the query the listbox is based on.

i wrote a detailed post about this here some time ago.
 

Khalid_Afridi

Registered User.
Local time
Tomorrow, 01:31
Joined
Jan 25, 2009
Messages
491
Lol :)

Thanks ajetrumpet
I was surprised when I saw wiklendt's answer at first glance. I thought, may be I asked wrong question in wrong place or my question is incomplete...

Actually I have a listbox on my mainform and my recordsource is on subform, I wrote the following function to populate my list box:
Code:
Private Sub Command9_Click()
    If checkVal Then
        MsgBox "Value exists"
    Exit Sub
          Else
           If Me.Field3.Value = "inactive" Then
                MsgBox "you can't add in-active location", vbCritical
           Exit Sub
                Else
                Forms!form1.MyListbox.AddItem Item:=Me.ID.Value & ";" & Me.Field1.Value & ";" & Me.Field2.Value
        End If
    End If
    
End Sub
 
Function checkVal() As Boolean
Dim x As Integer
            For x = 0 To Forms!form1.MyListbox.ListCount - 1
                If Forms!form1.MyListbox.Column(0, x) = Me!ID Then
                   MsgBox "Value exists"
                   checkVal = True
                End If
            Next x
End Function
but...

still it add Me!ID (myvalue) to the list.
where is the bug??

Khalid :)
 
Last edited:

Khalid_Afridi

Registered User.
Local time
Tomorrow, 01:31
Joined
Jan 25, 2009
Messages
491
I can't help but laugh at this wik. Nice answer! :)


and btw, the better answer is:
PHP:
dim i as integer

for i = 0 to me.listbox.listcount - 1

   if me.listbox.column(0, i) = myvalue then
      msgbox "it exists!"
         exit sub
   end if

next i

Thanks adam

i change my code little bit like this:
Code:
Private Sub Command9_Click()
    If checkVal Then
        MsgBox "Value exists"
    Exit Sub
    
    Else
            If Me.Field3.Value = "inactive" Then
                MsgBox "you can't add in-active location", vbCritical
                Exit Sub
            Else
                Forms!form1.MyListbox.AddItem Item:=Me.ID.Value & ";" & Me.Field1.Value & ";" & Me.Field2.Value
            End If
    End If
    
End Sub
 
Function checkVal() As Boolean
Dim x As Integer, [COLOR="Blue"]myValue As Integer[/COLOR]

            For x = 0 To Forms!form1.MyListbox.ListCount - 1
[COLOR="Blue"]                myValue = Forms!form1.MyListbox.Column(0, x)
                If Forms!form1.MyListbox.Column(0, x) = myValue Then
                    checkVal = True[/COLOR]
                    Exit Function
                End If
            Next x
End Function

its works fine now :)

Thanks both of you and all my friends.

Khalid
 

Users who are viewing this thread

Top Bottom