Solved

instr problem in vba

Posted on 2014-09-16
8
342 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
8 Comments
 
LVL 49

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

773 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