Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does INSTR not work here?

Status
Not open for further replies.

Chats

Technical User
Mar 12, 2002
88
0
0
GB
Hi,

I am trying to set up some code which will use a listbox (multiselect) to populate a textbox. A second bit of code runs when the userform opens, checks the contents of the textbox to see what has been previously entered, and sets the .selected property on the listbox entries to match. This is the part causing me problems.

I have the following set up in Word 2000:

- A textbox named txtElectricalSystem which I have placed in the document.

- A userform with a listbox (multiselect) called ElectricalSystem

What I want to do is check each of the items in the listbox valuelist to see wether the text of that ElectricalSystem listbox item is contained within the text of the txtElectricalSystem textbox.

My code is this:

Code:
For i = 0 To ElectricalSystem.ListCount - 1
            If InStr(0, ElectricalSystem.List(i, 0), txtElectricalSystem, vbTextCompare) > 0 Then
            End If
            
    MsgBox (InStr(1, ElectricalSystem, ThisDocument.txtElectricalSystem.Value))
            
Next i

This doesn't work - even when the text in the ElectricalSystem listbox category matches the text in the txtElectricalSystem, INSTR returns 0.

I have tried all sorts with this - changing the vbcompare setting, using two string variables to hold the contentds of the listbox and textbox... I have checked the LEN of the two strings and they match exactly.

Can anybody spot what is wrong with the INSTR instruction?

Thanks for any help.


 
Hiya,
it's a bit early in the morning for me still, but shouldn't there be a property, like .Value after txtElectricalSystem in your InStr function call?

K :)
 
Hi
The first thing I see should actually generate an error!!

Change the first argument of the InStr function from 0 to 1 and see if that helps.

Happy Friday

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Ignore my comments above as I didn't look closely enough at what you already had.

You appear to have two versions of the instr function. I don't think the first should work for the reasons I mentioned first time round. The second one is trying to compare text to a listbox.

I haven't tested this but you could try combing what you have posted

ie
For i = 0 To ElectricalSystem.ListCount - 1
If InStr(1, ElectricalSystem.List(i, 0), _
ThisDocument.txtElectricalSystem.Value, vbTextCompare) > 0 Then
End If
Next i


Too early for me too!
Happy Friday

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Thanks for the help so far - it is still not working for me (driving me potty!) I have changed my code to try to diagnose what is wrong. Now a msgbox shows the value of the textbox, the value of the current listbox item (i) and the results of the INSTR match. It makes no sense to me why it doesn't work.

My code is now this:

Code:
For i = 0 To ElectricalSystem.ListCount - 1
    

        MsgBox ("txtElectricalSystem= " & ThisDocument.txtElectricalSystem) & " /ListBox value= " _
        & ElectricalSystem.List(i, 0) & " /INSTR= " & InStr(1, ElectricalSystem.List(i, 0), _
        ThisDocument.txtElectricalSystem.Value, vbTextCompare)
Next i

The For...Next loop runs through each of the listbox values and for diagnostic purposes I have put in a messagebox for each step of the loop. The message box puts up on the screen the value of the field txtElectricalSystem, the value of ElectricalSystem.List(i,0) for each step of i, then the results of the INSTR match.

Here is the output of the messagebox where there should be a match (for the iteraction of the loop where the listbox value matches the textbox value):-

[blue]txtElectricalSystem= CCTV /ListBox Value= CCTV /INSTR= 0 [/blue]

The INSTR match should be >0 "CCTV" is clearly in both the textbox and the listbox.

Any ideas??
 
I'm at a loss!! I've tried to recreate the document & form but even if the text box is blank I get a value of 1 from the InStr function. Strange in iteself? If I enter something that matches the listbox I get the 1 required.

Sorry, no idea!!

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
I think I've worked it out....

Here is the version which works:

Code:
 MsgBox ("txtElectricalSystem= " & ThisDocument.txtElectricalSystem) & " /ListBox value= " _
        & ElectricalSystem.List(i, 0) & " /INSTR= " & InStr(1, ThisDocument.txtElectricalSystem, _
        ElectricalSystem.List(i, 0), vbTextCompare)

Here is the version which doesn't

Code:
        MsgBox ("txtElectricalSystem= " & ThisDocument.txtElectricalSystem) & " /ListBox value= " _
        & ElectricalSystem.List(i, 0) & " /INSTR= " & InStr(1, ElectricalSystem.List(i, 0), _
        ThisDocument.txtElectricalSystem.Value, vbTextCompare)

Whats the difference? Well, in the second code sample, I have got the ElectricalSystem.List and the ThisDocuemnt.txtElectricalSystem the other way round from the first code sample.

I would have thought the match should work both ways, but it doesn't seem to - strange, but no stranger than life!

Thanks for all the attempts to help, hopefully this being posted will help somebody else avoid the hours of head scratching I've had over this one in the future!
 
Seems that ElectricalSystem.List(i, 0) has trailing spaces ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top