Results 1 to 14 of 14
  1. #1
    pepok4 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    6

    Query using VBA code

    Hi all,


    I have very little progamming experience with Access. I have field "User" which is filled with "firstname" and "lastname" (format looks like: firstname.username). Next I have second field "Nickname". I would like to fill "Nickname" automatically when I go to next record. Nickname contains 6 characters (all uppercase). Format of Nickname is: first character from "firstname" and first five characters from "lastname" (behind dot).For example:

    For example:
    1st user
    Field User: Peter.Smith
    Field Nickname: PSMITH

    2nd user
    Field User: Thomas.Sikoooo
    Field Nickname: TSIKOO

    3rd user
    ...
    etc.

    I have this query, which work correct for me, but I do not want to run an update query.
    Code:
    UPDATE Table1 SET Table1.NickName = UCase(Left(Left([User],InStr(1,[User],".")-1),1) & Left(Right([User],Len([User])-InStr(1,[User],".")),5));
    How I can convert this query to VBA code. I want, than update was automatically executing, when I'm going to next record in form. Necessary condition to run code: Field "user" must be filled!

    Many thanks!
    Attached Files Attached Files

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Having the first name and last name in the same field is not good practice. It violates normalization rules.
    How do you do you handle names like:
    O'Brian (apostrophe)
    Van Horn (space in name)
    Del La Cruz (spaces in name)

    I would use the form before update:
    Code:
    Private Sub Form_BeforeUpdate()
    
       Dim TestArray() As String
    
       TestArray = Split(Me.User, ".")
       Me.NickName = Left(TestArray(0), 1) & Left(TestArray(1), 5)
    
    End Sub

  3. #3
    pepok4 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    6
    Hi ssanfu,
    good tip, but my database will be used only with people, which has not apostrophe or spaces in name. I do not have to solve these cases.

    I do not why, but when I copy your code and use as form before update, it does not work. :-(

  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
    An alternative is to not save the value but to just calculate whenever needed. Can be in query or on form or on report.

    Why doesn't work - error message, wrong results, nothing happens?
    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
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I do not why, but when I copy your code and use as form before update, it does not work
    Did go back to the form in design view,
    open the properties dialog box for the form,
    click on the events tab,
    selected [Event Procedure] in the Before Update and clicked on the 3 dots?

    This will connect the code to the event.

  6. #6
    pepok4 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by June7 View Post
    An alternative is to not save the value but to just calculate whenever needed. Can be in query or on form or on report.
    Why doesn't work - error message, wrong results, nothing happens?
    Nothing happens.

    Quote Originally Posted by ssanfu View Post
    Did go back to the form in design view,
    open the properties dialog box for the form,
    click on the events tab,
    selected [Event Procedure] in the Before Update and clicked on the 3 dots?

    This will connect the code to the event.
    I make this steps, but it doesn't work. Nothing happens - when I switch between records. I try change my file to other version of MS Access, but it is the same.
    Attached Files Attached Files

  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
    Again, why do you need to save this data? It can simply be calculated whenever needed.

    However, I tried the procedure and I get an error message: "The expression Before Update you entered as the event property setting produced the following error: Procedure declaration does not match description of event or procedure having the same name." The BeforeUpdate sub declaration has an argument:

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    ...
    End Sub
    Last edited by June7; 01-30-2013 at 03:05 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.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The BeforeUpdate sub declaration has an argument:
    Sometimes I hate cut and paste...


    Should have been (as June pointed out ):
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
       Dim TestArray() As String
    
       TestArray = Split(Me.User, ".")
       Me.NickName = Left(TestArray(0), 1) & Left(TestArray(1), 5)
    
    End Sub

    ------------------------
    You really should change this >> Having the first name and last name in the same field is not good practice. It violates normalization rules.

  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
    And if you want the result in all upper case:

    UCase(Left(TestArray(0), 1) & Left(TestArray(1), 5))
    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
    pepok4 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by June7 View Post
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    ...
    End Sub
    I don't understand it. I tested it with argument "Cancel As Integer", but no change in field NickName. NickName is still empty, when I switch between records.

    Does it work correct in your Access?

  11. #11
    pepok4 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by ssanfu View Post
    ------------------------
    You really should change this >> Having the first name and last name in the same field is not good practice. It violates normalization rules.
    I have the following code in a control source to automatically pick up the username of the person logged on to the PC.
    Code:
    Private Sub Form_Current()
        If IsNull(Me.User) Then
                  Me.User = Environ("Username")
        End If
    End Sub
    Username of logged person is in this format: Peter.Smith
    I want create specific NickName for this person, which contains from first character of FirstName and 5 character of LastName.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The 'nickname' can be calculated from the username whenever it is needed. There is no requirement to save the nickname to table.

    The code in the posted db works, after fixing the procedure declaration.
    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.

  13. #13
    qa12dx is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    india
    Posts
    112
    Quote Originally Posted by June7 View Post
    The 'nickname' can be calculated from the username whenever it is needed. There is no requirement to save the nickname to table..
    that is the best option.
    but if u really want to populate the feild then you can use 'after update' event in USER textbox to set value -item - control nickname. epxr - UCase(Left([User],1) & Mid([User],InStr([User],"."),100))

    for the mid 2nd value i didn't use len([abc]) because why run another query calculating field lenght. i just put 100 and access will just go upto the last char

  14. #14
    pepok4 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by June7 View Post
    The code in the posted db works, after fixing the procedure declaration.
    I find how it works.

    I had to modify code, but now it works as I wanted. I added button "Save" and then I used event procedure on click:

    Code:
    Private Sub Save_Click()
       Dim TestArray() As String
    
       If IsNull(Me.NickName) Then
            TestArray = Split(Me.User, ".")
            Me.NickName = UCase(Left(TestArray(0), 1) & Left(TestArray(1), 5))
       End If 
    End Sub
    Many thanks all for yours advices!!!

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

Similar Threads

  1. Query, Form, Code help
    By empyrean90 in forum Access
    Replies: 2
    Last Post: 08-08-2012, 05:44 AM
  2. query using code
    By rohini in forum Queries
    Replies: 1
    Last Post: 05-17-2012, 03:46 AM
  3. connecting SQL code for a Query
    By Huddle in forum Queries
    Replies: 1
    Last Post: 02-08-2012, 03:42 PM
  4. Accessing Code For a Query
    By TexMax007 in forum Programming
    Replies: 3
    Last Post: 08-29-2009, 08:08 AM
  5. code help for updat query
    By Grant in forum Queries
    Replies: 1
    Last Post: 01-31-2008, 05:53 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