How do I do string processing with arrays for double words?

VBMaster was able to help me put this little program together that demonstrates how to take input from one text box and process it for sql query search string.

The program works fine for single words with operators, example:

windows and display

I would like it to work for double words like the following examples:

-windows 95 and display properties
-windows 95 and display
-windows and display properties, etc.

full points for full source code demonstrating this.

Here is my present code for single words:

--------------------------------------
Dim sOperators() As String
  Dim sWords() As String
  Dim sArray As Variant
  Dim a As Integer
  Dim sSQL As String
 
   
Private Sub cmdProcess_Click()

PrimaryInput = Text1.Text

sArray = Split(PrimaryInput)


  ReDim sWords(UBound(sArray) \ 2)
  ReDim sOperators(UBound(sArray) \ 2 - 1)
  For a = 0 To (UBound(sArray) - 1) Step 2
    sWords(a \ 2) = sArray(a)
    sOperators(a \ 2) = sArray(a + 1)
  Next
  sWords(a \ 2) = sArray(a)
 
 
  sSQL = "select * from windows where"
  For a = 0 To (UBound(sWords) - 1)
    sSQL = sSQL & " problem like '%" & sWords(a) & "%' " & sOperators(a)
  Next
  sSQL = sSQL & " problem like '%" & sWords(a) & "%'"
Text2.Text = sSQL
End Sub
---------------------------------------
end of code
jawad_hussainAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
VbmasterConnect With a Mentor Commented:
I posted some codes on the earlier question. Tell me how it works.
0
 
jawad_hussainAuthor Commented:
VbMaster's code works perfectly, here is the code:

-----------------------start of code
  Dim sOperators() As String
  Dim WordCount As Integer
  Dim OpCount As Integer
  Dim sWords() As String
  Dim sResult As String
  Dim sArray As Variant
  Dim a As Integer
  Dim PrimaryInput As String
 



 
Private Sub cmdProcess_Click()
PrimaryInput = Text1.Text
sArray = Split(PrimaryInput)
   
  sResult = ""
  Erase sOperators
  Erase sWords
  WordCount = 0
  OpCount = 0
  a = 0
   
  If (UBound(sArray) = 0) Then
    WordCount = 1
    ReDim sWords(WordCount - 1)
    sWords(WordCount - 1) = sArray(0)
    sResult = sArray(0)
  Else
    WordCount = 1
    ReDim sWords(WordCount - 1)
    Do Until (a > UBound(sArray))
      sResult = sResult & sArray(a)
      Select Case UCase$(sArray(a))
      Case "AND", "OR"
        OpCount = OpCount + 1
        ReDim Preserve sOperators(OpCount - 1)
        sOperators(OpCount - 1) = sArray(a)
        WordCount = WordCount + 1
        ReDim Preserve sWords(WordCount - 1)
      Case Else
        If (Len(sWords(WordCount - 1)) > 0) Then
          sWords(WordCount - 1) = sWords(WordCount - 1) & " " & sArray(a)
        Else
          sWords(WordCount - 1) = sArray(a)
        End If
      End Select
      a = a + 1
    Loop
  End If

   
  Dim sSQL As String
    sSQL = "select * from windows where"
  For a = 0 To (WordCount - 2)
    sSQL = sSQL & " problem like '%" & sWords(a) & "%' " & sOperators(a)
  Next
  sSQL = sSQL & " problem like '%" & sWords(a) & "%'"
 
  Text2.Text = sSQL
End Sub

-----------------------END OF CODE
0
 
jawad_hussainAuthor Commented:
Thanks for the great answer, I've accepted your answer. and posted the code at the bottom.
0
 
jawad_hussainAuthor Commented:
VBMaster:

I tried changing the case clause to also include "AND NOT" but it does not seem to work.

do you have any idea?

Case "AND", "OR", "AND NOT"
0
All Courses

From novice to tech pro — start learning today.