Calling / executings a Public Function sCase

teiben

Registered User.
Local time
Today, 20:19
Joined
Jun 20, 2002
Messages
462
I got this cool function to convert fields to sentenance case, so I created a module name caseModule and pasted the code in.

Public Function sCase(ByRef strIn As String) As String
Dim bArr() As Byte, i As Long, i2 As Long
If strIn = vbNullString Then Exit Function
Let bArr = strIn
Select Case bArr(0)
Case 97 To 122
bArr(0) = bArr(0) - 32
End Select
For i = 2 To UBound(bArr) Step 2
Select Case bArr(i)
Case 105
If Not i = UBound(bArr) - 1 Then
Select Case bArr(i + 2)
Case 32, 33, 39, 44, 46, 58, 59, 63, 148, 160
If bArr(i - 2) = 32 Then _
bArr(i) = bArr(i) - 32
End Select
ElseIf bArr(i - 2) = 32 Then _
bArr(i) = bArr(i) - 32
End If
Case 33, 46, 58, 63
For i2 = i + 2 To UBound(bArr) Step 2
Select Case bArr(i2)
Case 97 To 122
bArr(i2) = bArr(i2) - 32
i = i2: Exit For
End Select
Select Case bArr(i2)
Case 32, 33, 46, 63, 160
Case Else
i = i2: Exit For
End Select
Next
End Select
Next
sCase = bArr
End Function

How do I call it? I've tried a few things, and I can get it to run only if I comment out an existing Call statement. I'm trying to execute it on Form Load event.

i.e.
Private Sub Form_Load()
DoCmd.Maximize
gHW = Me.hwnd

If IsHooked Then
Call Unhook
End If

'Call procedure to begin capturing messages for this window
'Call Hook
Call sCase 'this only works if I comment out call hook

End Sub
 
If you surround your code with code tags, the indenting is preserved and the code is easier to read.
Code:
Public Function sCase(ByRef strIn As String) As String
   Dim bArr() As Byte, i As Long, i2 As Long
   If strIn = vbNullString Then Exit Function
   Let bArr = strIn
   Select Case bArr(0)
      Case 97 To 122
         bArr(0) = bArr(0) - 32
   End Select
   For i = 2 To UBound(bArr) Step 2
      Select Case bArr(i)
         Case 105
            If Not i = UBound(bArr) - 1 Then
               Select Case bArr(i + 2)
                  Case 32, 33, 39, 44, 46, 58, 59, 63, 148, 160
                     If bArr(i - 2) = 32 Then _
                        bArr(i) = bArr(i) - 32
               End Select
            ElseIf bArr(i - 2) = 32 Then _
                   bArr(i) = bArr(i) - 32
            End If
         Case 33, 46, 58, 63
            For i2 = i + 2 To UBound(bArr) Step 2
               Select Case bArr(i2)
                  Case 97 To 122
                     bArr(i2) = bArr(i2) - 32
                     i = i2: Exit For
               End Select
               Select Case bArr(i2)
                  Case 32, 33, 46, 63, 160
                  Case Else
                     i = i2: Exit For
               End Select
            Next
      End Select
   Next
   sCase = bArr
End Function
Functions such as this return values and this one expects to receive an input string so:
ReturnedString = sCase(InputString)
or
ReturnedString = sCase("InputString")
...would be the method of invoking this function.
 
Last edited:
Thanks. Question though, I thought I would invoke this so it would apply to all fields on the form, which I don't think is the case?
What event should I use to invoke it, afterupdate I suspect?
Thanks
 
I don't know the structure of your form but you may want to put it in the BeforeUpdate event of each affected control.
 

Users who are viewing this thread

Back
Top Bottom