Solved

instr problem in vba

Posted on 2014-09-16
8
347 Views
Last Modified: 2014-09-16
I am using an if statement to determine another process but I have an instr problem:

If (InStr(RstWordGroups.Fields("ConcatenatedUnwantedRemoval").Value, RstAutoRange.Fields("LookFor").Value) <> 0) And (RstWordGroups.Fields("MarqueAlias").Value = RstAutoRange.Fields("Marque").Value) Then

Open in new window



Fields("ConcatenatedUnwantedRemoval").Value = "C 320 AVANTGARDE"
RstAutoRange.Fields("LookFor").Value = "C 32"

How do I change this so that the instr doesnt pick up in this event?

RstAutorange has in it:

"C32"
"C 32"
"C 320"
"C320"

So I only want it to be true if the particular string is in the string
in my example
"C 320 AVANTGARDE" c 32 gets by the instr

I am in vba
0
Comment
Question by:PeterBaileyUk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 40324850
You could look for a "word" with a trailing Space, "C 32 ":

InStr(RstWordGroups.Fields("ConcatenatedUnwantedRemoval").Value, RstAutoRange.Fields("LookFor").Value & " ")

/gustav
0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40324852
Can I cheat? Can I say that you are looking for C32 either at the end of the string, or C32 followed by a space?

If so, then the INSTR then becomes:

InStr(RstWordGroups.Fields("ConcatenatedUnwantedRemoval").Value & " ", RstAutoRange.Fields("LookFor").Value & " ") <> 0)
0
 

Author Comment

by:PeterBaileyUk
ID: 40324861
various clients do this:

"C32"
"C 32"
"C 320"
"C320"

I dont know which part of the string in will be in it might be first or in middle (surrounded by space)

Here is some actual strings for this example:

concatenated
C 320 AVANTGARDE
C 320 AVANTGARDE AUTO
C 320 AVANTGARDE SE
C 320 AVANTGARDE SE CDI
C-CLASS C 320 CDI AVANTGARDE
C-CLASS C 320 CDI AVANTGARDE AUTO
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:PeterBaileyUk
ID: 40324863
i will try the new instr see if it cures
0
 

Author Comment

by:PeterBaileyUk
ID: 40324976
it worked
0
 

Author Closing Comment

by:PeterBaileyUk
ID: 40324977
ok i shared the points hope thats ok
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40324978
That's fine.
0
 
LVL 57
ID: 40324992
Is your problem case sensitivity, the spaces, or both?

Couple of comments:

1. On Case sensitivity, you can:

a. use UCase() to force a string to all upper case, then do the compare.
b. place your check procedure in its own module, then use the Option Compare statement.  There are three settings, Binary, Text, and Database.  Each works differently:

Option Compare Binary results in string comparisons based on a sort order derived from the internal binary representations of the characters. In Microsoft Windows, sort order is determined by the code page. A typical binary sort order is shown in the following example:

A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø

Option Compare Text results in string comparisons based on a case-insensitive text sort order determined by your system's locale. When the same characters are sorted using Option Compare Text, the following text sort order is produced:

(A=a) < ( À=à) < (B=b) < (E=e) < (Ê=ê) < (Z=z) < (Ø=ø)

Option Compare Database can only be used within Microsoft Access. This results in string comparisons based on the sort order determined by the locale ID of the database where the string comparisons occur.

2. As for the spaces, when I do certain compares, I use a procedure to remove all white space (spaces, tabs, etc) and "collapse" the string so that it's nothing but characters.

  It's easy to write a routine like that using replace().   i.e.

         strText = Replace(strText, " ", "")

  I find compares on collapsed strings more accurate.

HTH,
Jim.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question