'WORD FUNCTIONS
'
'An ACCESS BASIC module for Microsoft Access
'
'
'(C) 1993 by Jeffrey May
'
'   This Microsoft Access module is distributed as "FAIR WARE".  In other words
'   I did spend some time writing and debugging it.  I think it's useful.  I hope you
'   do too.  I ask you to be fair.
'
'       -   If you use this module or distribute it to others either by itself or as a
'           part of an application please do not alter it or remove this comment text
'           and copyright notice.  If you post it for distribution, please do so in the
'           original ZIP file including all the original components without modification.
'
'       -   If you use it in YOUR OWN databases and applications, be my guest.
'           Compensation is optional.
'
'       -   If you use it in something you are writing for someone else and
'           are compensated for your efforts, it is only fair that you reimburse
'           me for my time.  $25.00 is the recommended amount.
'
'                       Jeffrey May
'                       54 Seneca Drive
'                       Meriden, CT  06450
'                       U.S.A.
'
'   Your comments and suggestions are welcome.
'
'                       Internet: 74475.1757@COMPUSERVE.COM
'                       Compuserve Address: 74475,1757
'                       GEnie Address: J.MAY13
'
'                       Comments and suggestions are welcome.
'
'
'This module adds 5 functions to an Access Basic database.
'
'These functions are:
'
'
'       WORD()  Return a particular word from the input string.
'       FCAP()  Converts the first letter of a string to uppercase.
'       WCAP()  Converts the first letter of EACH WORD in a string to uppercase.
'       XCAP()  Converts a string to lowercase and sets the first letter to uppercase.
'       ZCAP()  Converts a string to lowercase and sets the first letter of EACH WORD
'               to uppercase.
'
'
'
'       FOR DETAILS SEE THE FILE  READ-WFM.WRI WHICH ACCOMPANIES THIS MODULE
'====================================================================================

Option Compare Text   'Use text order for string comparisons

Dim Wordlist(255), StartPos(255) As Integer
Global TotalWords As Integer


'Variable list

'NAME                   TYPE                             USEAGE

'Wordlist()         Global Variant array      List of words in input string
'StartPos()         Global Integer array      Start position of each word in input string
'TotalWords         Global Integer            Total number of words found in input string

'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'
'
'                                       FCAP
'
'
'
'                Return target text with first character in uppercase
'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'
'Variable list
'
'NAME                   TYPE                             USEAGE
'
'Wordlist()         Global Variant array      List of words in input string
'StartPos()         Global Integer array      Start pos of each word in input string
'TotalWords         Global Variant            Total number of words found in input string
'Firstchar          Local Variant             First character of string
'
'
Function Fcap (Text)                       'Declare function Fcap.  Text is user input text

Dim Firstchar                              'Declare local variables

Firstchar = Left(Text, 1)                  'Get the first character of the input text
Firstchar = UCase(Firstchar)               'Upper case it if it isn't already
Mid(Text, 1, 1) = Firstchar                'Insert converted character back into text

Fcap = Text                                'Set the function variant

End Function                               'Back to caller

'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'
'
'                                  WCAP
'
'      Return target text with first letter of each word in uppercase
'
'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'
'Variable list
'
'NAME                   TYPE                             USEAGE
'
'Wordlist()         Global Variant array      List of words in input string
'StartPos()         Global Integer array      Start pos of each word in input string
'TotalWords         Global Variant            Total number of words found in input string
'Dummy              Local variant             Dummy variable for calling Word()
'Count              Local integer             Counter for FOR/NEXT loop.
'
'
Function Wcap (Text)            'Declare the function WCAP.  Text is user input

Dim Dummy As Variant, Count As Integer     'Declare local variables

Dummy = Word(Text, 1)           'Call the WORD function to get word count and pos

For Count = 1 To TotalWords                                              'Get first of each word
  Mid$(Text, StartPos(Count), 1) = UCase(Mid$(Text, StartPos(Count), 1)) 'Uppercase it
Next Count                                                               'Round 'n' round

Wcap = Text                     'Set function variant

End Function                    'Return to caller

'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'
'
'                                   WORD
'
'
'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'Variable list
'
'NAME                   TYPE                             USEAGE
'
'Wordlist()         Global Variant array      List of words in input string
'StartPos()         Global Integer array      Start pos of each word in input string
'TotalWords         Global Variant            Total number of words found in input string
'Backwards          Local Integer             0 is count from start.  1 is count from end
'LastPos            Local Integer             Where was the last space?
'Count              Local Integer             Which word are we on?
'SpacePos           Local Integer             Where is this space?
'Wordlen            Local Integer             How long is this word?
'Position           Local Integer             Where does the selected word start?
'Sp                 Local Variant Constant    Space Character (ASCII 20h)
'
'========================================================================================
Function Word (InText, Pos1)        'Declare function Word.  InText is input text
                                    'Pos1 is desired word position within InText

  
Dim Backwards As Integer, LastPos As Integer, Count As Integer
Dim SpacePos As Integer, Wordlen As Integer, Position As Integer

Const Sp = " "                                  ' Sp is a SPACE character



If Pos1 < 0 Then                                'If argument is negative
  Backwards = 1                                 'Set the search from end flag
  Pos1 = Abs(Pos1)                              'And clear the negative argument
Else                                                    'Otherwise
  Backwards = 0                                 'Clear the search from end flag.
End If

If Pos1 > 255 Then GoTo TooBig:


TotalWords = 0                                    'Initialize word counter
LastPos = 0                                       '     "     last space marker
StartPos(1) = 1                                   '     "     StartPos at chr pos 1
Count = 1                                         '     "     Count at first word


Do
  SpacePos = InStr(LastPos + 1, InText, Sp)

  'Find the next space in the string
  If LastPos >= Len(InText) Then Exit Do                'unless we're at the end of it
  If SpacePos > 0 Then                                  'If you do find a SPACE
    TotalWords = TotalWords + 1                         'Increment the word counter
    Wordlen = (SpacePos - LastPos) - 1                  'Calculate the length of the word
    Wordlist(Count) = Mid(InText, LastPos + 1, Wordlen) 'Add it to the word list
    Count = Count + 1                                   'Increment current word counter
    StartPos(Count) = SpacePos + 1                      'Mark the start position counter
    LastPos = SpacePos                                  'Update last space position flag

    If Mid(InText, LastPos + 1, 1) = Sp Then            'Ignore consecutive spaces
      LastPos = LastPos + 1
    End If
    
  End If
If Count > 254 Then Exit Do

Loop While SpacePos > 0                      'Keep doing it until you're out of spaces

If Count > 254 Then GoTo TooBig:             'More than 255 words in InText is illegal

TotalWords = TotalWords + 1                  'Increment the total word counter and
Wordlist(Count) = Mid(InText, LastPos + 1)   'Always get in the last word

Position = Pos1                              'Duplicate user request unless

If Backwards = 1 Then                        'If search from end flag is set, in which case...
  Position = (TotalWords + 1) - Pos1         'Count backwards from last word
End If

If Position < 1 Then Position = 1                'If you run off the start, give the first word
If Position > TotalWords Then                    'If you run off the end, give the last word
  Word = Wordlist(TotalWords)
Else                                             'Otherwise...
  Word = Wordlist(Position)                      'Else give em' what they asked for
End If

GoTo AllDone:

TooBig:

MsgBox "Cannot parse more than 255 words in a string", 16, "WORD ( ) function error"

AllDone:

End Function                                     'That's all, folks

'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'
'
'                                  XCAP
'
'      Return target text with first letter in uppercase, all others lowercase
'
'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
Function Xcap (Text)           'Declare function Xcap.  Text is user input

Text = LCase(Text)             'Convert user input to lower case
Xcap = Fcap(Text)              'Uppercase the first character & set the function variant

End Function                   'That's all there is to it.

'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'
'
'                                  ZCAP
'
'Return target text with first letter of each word in uppercase, all others in lower case
'
'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'
'Variable list
'
'NAME                   TYPE                             USEAGE
'
'Wordlist()         Global Variant array      List of words in input string
'StartPos()         Global Integer array      Start pos of each word in input string
'TotalWords         Global Variant            Total number of words found in input string
'Dummy              Local variant             Dummy variable for calling Word()
'Count              Local integer             Counter for FOR/NEXT loop.
'
'====================================================================================
Function Zcap (Text)                'Declare function Zcap.  Text is user input.

Dim Dummy, Count                    'Declare local variables

Text = LCase(Text)                  'convert input text to lower case

Dummy = Word(Text, 1)               'invoke WORD function to get word count and position


For Count = 1 To TotalWords                                               'Get each word
  Mid$(Text, StartPos(Count), 1) = UCase(Mid$(Text, StartPos(Count), 1))  'Uppercase it
Next Count                                                                'Etc, Etc, Etc.

Zcap = Text                         'Set function variant

End Function                        'Return to caller

