Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms / May 2008

Tip: Looking for answers? Try searching our database.

How can I search part of text from a field in VBA?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hanski - 19 May 2008 08:03 GMT
Hi.

I have 2 fields. Both are text fields. First field includes a text
'Cat'. Second field includes a text 'Mouse Dog Cat Horse'.

I would like to find out if the text in first field includes in the in
secon field via VBA.

What kind of function I have to make or is there some other way to do
it?

Hannu
Douglas J. Steele - 19 May 2008 11:23 GMT
In VBA, you can use the InStr function:

Dim strField1 As String
Dim strField2 As String

 strField1 = "Mouse Dog Cat Horse"
 strField2 = "cat"
 If InStr(strField1, strField2) > 0 Then
' strField2 appears in strField1
 Else
' strField2 does not appear in strField1
 End If

Of course, I'm assuming that your example is simpler than what you're
actually trying to do. That formula would also say that cat appears in the
string "Toss Scatters Strews". If all of your words are separated by spaces,
you could use

 If InStr(" " & strField1 & " ", " " & strField2 & " ") > 0 Then
' strField2 appears in strField1
 Else
' strField2 does not appear in strField1
 End If

Another approach would be to use the Split function to take the contents of
strField1 and put each word in an array:

Dim booFound As Boolean
Dim lngLoop As Long
Dim strField1 As String
Dim strField2 As String
Dim varWords As String

 strField1 = "Mouse Dog Cat Horse"
 varWords = Split(strField1, " ")
 strField2 = "cat"
 For lngLoop = LBound(varWords) To UBound(varWords)
   If varWords(lngLoop) = strField2 Then
     booFound = True
     Exit For
   End If
 Next lngLoop
 If booFound = True Then
' strField2 appears in strField1
 Else
' strField2 does not appear in strField1
 End If

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)

> Hi.
>
[quoted text clipped - 8 lines]
>
> Hannu
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.