Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I need to check the first 3 characters of a string in VB6

Posted on 2014-04-16
7
Medium Priority
?
309 Views
Last Modified: 2014-05-11
Hi Experts,
I need to check the first 3 characters of a string in VB6.  I need to read a string and find out if the first 3 characters equal 'DF-'  .  How can I do this?

Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
6 Comments
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 40005854
Hi,

pls try

If strYourString Like "DF-*" Then

Open in new window

Regards
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40005857
You need to read from:
- file
- db
- console
-
-
?

To check you use
if Left(yourString, 3) = "DF-" then
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 668 total points
ID: 40006040
Here's yet another way:
If InStr(MyString, "DF-") = 1 Then

Open in new window

If the case of the test strings is variable with some candidates starting "df-", "Df-" etc., you could use the expanded syntax:
If InStr(1, MyString, "DF-", vbTextCompare) = 1 Then

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Assisted Solution

by:ravikantninave
ravikantninave earned 664 total points
ID: 40006102
Dim a, b, c As String
a = "DF-"
b = "DF-abcd"
c = "adjfldjfklsdf"

If Left(b, 3) = "DF-" Then MsgBox "b"
If Left(c, 3) = "DF-" Then MsgBox "c"

Hope it works.
0
 
LVL 1

Accepted Solution

by:
dwe0608 earned 668 total points
ID: 40028798
Hi

you want to test a string to see if the first 3 characters equals DF-

   Dim sResult As String
   dim StrToSearch as string
   Dim B As Integer

   StrToSearch = "DF-1234567890"

   sResult = Mid$(StrToSearch, 1, 3)
   B = StrComp(sResult, "DF-", vbTextCompare)
           
If B = 0 Then the string has been found - otherwise it has not.

You could if you wished eliminate the necessity for sResult like this:

 B = StrComp(Mid$(StrToSearch, 1, 3), "DF-", vbTextCompare)

And you could encapsulate the whole thing into a function as follows:

Function SearchStr(strToSearch As String, sFind As String, iStart As Integer, iLen As Integer) As Boolean
 Dim b%
 b = StrComp(Mid$(strToSearch, iStart, iLen), sFind, vbTextCompare)
 SearchStr = IIf(b = 0, True, False)
End Function

Open in new window


You would call the function like this:

if strtosearch(strtosearch, "DF-",1 ,3) then
    ' found it
else
   ' not found
end if

Open in new window


HTH

DWE
0
 
LVL 1

Expert Comment

by:dwe0608
ID: 40028895
I've tightened up the function above a little more as well ...

Function SearchStr(strToSearch As String, sFind As String, iStart As Integer, iLen As Integer) As Boolean
 SearchStr = IIf(StrComp(Mid$(strToSearch, iStart, iLen), sFind, vbTextCompare) = 0, True, False)
End Function

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

783 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