Results 1 to 14 of 14
  1. #1
    yohansetiawan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    24

    Question Force First Letter of every word to UPPERCASE (no StrConv)

    Hello,



    I am very new to VBA in Access, and I have question.

    How do I force first letter of every word on fields/spesificField to uppercase?
    I know function called StrConv and the value is ProperCase to make that,
    but that function is to make "McDonald" text to "Mcdonald" instead of "McDonald".
    I only need forcing the first letter only to uppercase, and ignore the rest after first letter.
    So when I type "mcDonald mcD" it converts to "McDonald McD" instead of "Mcdonald Mcd".

    thanks.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    That would involve a complex custom user defined VBA coded function to accomplish. Or somehow customizing the spell checker dictionary. Why is this correction necessary? Maybe need to consider use of a combobox that restricts entry to items in list.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    yohansetiawan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    24
    yes it would necessary for my database though, but when you say its complex then maybe I'll look alternative way.
    maybe the procedure will be detect first input then after the space charater it will reset the procedure and continue itself all the way till end of input?
    I'm wondering if this issue can be archive by someone
    btw thanks for ur reply June7.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Yes, your pseudocode logic is what code would do. Use string manipulation functions. Split function could parse the string to a one dimension array and then loop code would read through the array elements and reconstruct the string with capitalization. Something like:
    Code:
    Function Capital(strWords As String)
    Dim aryWords As Variant
    Dim strCapWords As String, i As Integer
    aryWords = Split(strWords, " ")
    For i = 0 To UBound(aryWords)
        strCapWords = strCapWords & " " & UCase(Left(aryWords(i), 1)) & Mid(aryWords(i), 2)
    Next
    Capital = strCapWords
    End Function
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480

    Not a fix but

    I know this is not the solution... I use this to make every letter uppercase in a certain txtbox. Might be a step in the right direction.

    Code:
    Private Sub txtAccession_KeyPress(KeyAscii As Integer)
      Select Case KeyAscii
      Case Asc("a") To Asc("z")
      KeyAscii = KeyAscii + Asc("A") - Asc("a")
      End Select
      End Sub
    Maybe with some trimming it could work?

  6. #6
    yohansetiawan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    24
    Thanks to you guys for the replies.
    I found this StrConv ProperCase built-in function code and maybe with some modification it would be nice
    Code:
    Function StrConvProperCase(sInp)
           Dim nPos
           Dim nSpc
    
           nPos = 1
           Do While InStr(nPos, sInp, " ", 1) <> 0
                   nSpc = InStr(nPos, sInp, " ", 1)
                   StrConvProperCase = StrConvProperCase & UCase(Mid(sInp, nPos, 1))
                   StrConvProperCase = StrConvProperCase & LCase(Mid(sInp, nPos + 1, nSpc - nPos))
                   nPos = nSpc + 1
           Loop
    
           StrConvProperCase = StrConvProperCase & UCase(Mid(sInp, nPos, 1))
           StrConvProperCase = StrConvProperCase & LCase(Mid(sInp, nPos + 1))
           StrConvProperCase = StrConvProperCase
    End Function
    Last edited by yohansetiawan; 03-21-2012 at 12:02 PM.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    A variation on my suggestion. Have you tried any of the suggestions offered?

    I just noticed a bug in my suggestion. It will place a space at beginning of string. Need to trim that:

    Capital = Trim(strCapWords)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    yohansetiawan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    24
    I already saved your function on public module, but I still confused how to use it.
    where should I place your function in the event on my textField properties?

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Depends. Do you want to permanently change the data? Then use in the UpdateTo row of an UPDATE query. If you just want to have data display with this structure but not be changed, use the function in a constructed field of query or in ControlSource of a textbox or in AfterUpdate event code for textbox to correct the user input and save the correction to the field.

    The strWords argument would be the fieldname, like:

    =Capital([fieldname here])
    Last edited by June7; 03-21-2012 at 05:47 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    yohansetiawan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    24
    An error occurred when I run update query with UpdateTo value is Capital([myField])
    my update query is
    Code:
    UPDATE myTable SET myTable.myField = Capital([myField]);
    the error says "undefined function 'Capital' in expression"

    I already saved your function in module:
    Code:
    Public Function Capital(strWords As String)
        Dim aryWords As Variant
        Dim strCapWords As String, i As Integer
        aryWords = Split(strWords, " ")
        For i = 0 To UBound(aryWords)
        strCapWords = strCapWords & " " & UCase(Left(aryWords(i), 1)) & Mid(aryWords(i), 2)
        Next
        Capital = strCapWords
        Capital = Trim(strCapWords)
    End Function

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Can't reproduce your error. If you want to provide project, will look at it.

    The field name is actually myField?

    I tested the function and it runs but I am encountering a different error. Null values in field causing grief. Revised function.
    Code:
    Public Function Capital(varWords As Variant) As Variant
        Dim aryWords As Variant
        Dim varCapWords As Variant, i As Integer
        varCapWords = Null
        If Not IsNull(varWords) Then
            aryWords = Split(varWords, " ")
            For i = 0 To UBound(aryWords)
            varCapWords = varCapWords & " " & UCase(Left(aryWords(i), 1)) & Mid(aryWords(i), 2)
            Next
        End If
        Capital = Trim(varCapWords)
    End Function
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    yohansetiawan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    24
    no, I just creating new database for testing out this function, so I called it myField though LOL.
    my actual database is too big to upload, could you upload your testing database with this function and query attached so I can look at it?
    because I encounter same error when testing your revised function (again doh!)

    so here I uploaded my testing database that produce same error.
    the error says "Undefined function 'Capital' in expression."
    Am I doing it wrong? please have a look at it.
    many thanks for helping me so far.
    Attached Files Attached Files

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Problem is the code module and function have the same name. Change the code module name.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    yohansetiawan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    24
    wow.. it works very nice!!!
    much much thanks to you June7, thanks for your time and help!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Count uppercase letters
    By Misha in forum Access
    Replies: 8
    Last Post: 03-01-2012, 10:37 AM
  2. uppercase?
    By slimjen in forum Forms
    Replies: 2
    Last Post: 01-31-2012, 03:30 PM
  3. Query for Partial Uppercase Text in Field
    By pjordan@drcog.org in forum Queries
    Replies: 15
    Last Post: 12-29-2011, 12:53 PM
  4. Changing table names from lowercase to uppercase
    By supernix in forum Database Design
    Replies: 1
    Last Post: 09-03-2010, 10:08 PM
  5. Uppercase to Lowercase
    By tigers in forum Reports
    Replies: 1
    Last Post: 09-28-2009, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums