help in FOR-NEXT and IF-Then-Else (1 Viewer)

mahenkj2

Registered User.
Local time
Today, 19:18
Joined
Apr 20, 2012
Messages
459
I am using following code in a text box exit event to find matched text in a list box.
Private Sub txtLoaded_Exit(Cancel As Integer)
Dim I As Integer

If Not IsNull(txtLoaded) Then

For I = 0 To Me.lstLoaded.ListCount - 1
If Left$(Me.lstLoaded.Column(0, I), Len(Me.txtLoaded)) = Me.txtLoaded Then
Me.lstLoaded.Selected(I) = True

Me.lstLoaded.SetFocus
Me.lstLoaded.ListIndex = I
Me.cmdLoad.Enabled = True

Exit For
'Else
' MsgBox "Not available.", vbInformation
' Me.cmdLoad.Enabled = False
End If
Next I

Else

Exit Sub

What I need is when exact text is not found, a message box to display and some other actions but I am not finding correct place to keep those else commands. You can see else commands in ' appostophe.

May somebody suggest how to tackle this.

Thanks.
 

Isskint

Slowly Developing
Local time
Today, 13:48
Joined
Apr 25, 2012
Messages
1,302
It would depend how many other conditions you wanted to test for. The following will test for you current condition as the only acceptable condition.
Code:
Private Sub txtLoaded_Exit(Cancel As Integer)
Dim I As Integer
If Not IsNull(txtLoaded) Then
For I = 0 To Me.lstLoaded.ListCount - 1
    If Left$(Me.lstLoaded.Column(0, I), Len(Me.txtLoaded)) = Me.txtLoaded Then
        Me.lstLoaded.Selected(I) = True
 
        Me.lstLoaded.SetFocus
        Me.lstLoaded.ListIndex = I
        Me.cmdLoad.Enabled = True
 
    Else
 
        MsgBox "Not available.", vbInformation
        Me.cmdLoad.Enabled = False
    End If
Next I
Exit Sub

Whilst this will check for 2 acceptable conditions
Code:
Dim I As Integer
If Not IsNull(txtLoaded) Then
For I = 0 To Me.lstLoaded.ListCount - 1
    If Left$(Me.lstLoaded.Column(0, I), Len(Me.txtLoaded)) = Me.txtLoaded Then
        Me.lstLoaded.Selected(I) = True
 
        Me.lstLoaded.SetFocus
        Me.lstLoaded.ListIndex = I
        Me.cmdLoad.Enabled = True
 
    ElseIf Fields = Condition Then
 
        'do something here
    Else
        MsgBox "Not available.", vbInformation
        Me.cmdLoad.Enabled = False
    End If
Next I

Basically the format is:
If conditon then
do something
ElseIf another condition Then
do something
ElseIf another condition Then
do something
ElseIf another condition Then
do something
ElseIf another condition Then
do something
ElseIf another condition Then
do something
ElseIf another condition Then
else
do something
end if
 

mahenkj2

Registered User.
Local time
Today, 19:18
Joined
Apr 20, 2012
Messages
459
Many thanks Isskint.

My aim is one condition such that if there is no match in list box, I want to exit by displaying some message. So I feel your first solution should be OK.

I saw you removed EXIT FOR, does that not require?

Also, I tried something like this as well as what you suggested in solution 1 but although there is matching string in list box, it goes to message written in Else line. Why it does so!!

In case I remove else lines, searching is fine.
 

mahenkj2

Registered User.
Local time
Today, 19:18
Joined
Apr 20, 2012
Messages
459
After displaying the message two times, it goes on correct string. So, searching works but with else message.
 

Isskint

Slowly Developing
Local time
Today, 13:48
Joined
Apr 25, 2012
Messages
1,302
If you are simply looking for a single matching entry in your listbox, then there is a much simpler method. Just assign the value in txtLoaded to the listbox. If there is no match the ListIndex will be -1 and the value will not stick.

Code:
Me.lstLoaded = txtLoaded
If Me.lstLoaded.ListCount = - 1 Then
        MsgBox "Not available.", vbInformation
        Me.cmdLoad.Enabled = False
End If
 

mahenkj2

Registered User.
Local time
Today, 19:18
Joined
Apr 20, 2012
Messages
459
This looks to be much simpler and working if the match is available in list box but when there is no match, it does not display message. What is wrong here.
 

Isskint

Slowly Developing
Local time
Today, 13:48
Joined
Apr 25, 2012
Messages
1,302
Opps, my bad:banghead::banghead: change ListCount to ListIndex :eek:
 

mahenkj2

Registered User.
Local time
Today, 19:18
Joined
Apr 20, 2012
Messages
459
Yes, that works fine. I will make some changes as per my needs and revert if have some issues.

Thanks a lot again.

I am first time using list boxes and find these are more tricky than some other controls I used so far.

best regards.
 

Users who are viewing this thread

Top Bottom