Results 1 to 6 of 6
  1. #1
    JOEHAR is offline Novice
    Windows 10 Access 2007
    Join Date
    Sep 2020
    Posts
    1

    IF statement needed for a query

    If "Name" starts with "Dav" then replace it with corresponding name in "Replace Name"

    Name Replace Name
    Sam Samuel
    Dave David
    Rick Richard
    Ed Edward

    Thanks!

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,815
    Only try suggested code/sql on table copies. Try
    Code:
    UPDATE tblYourTable SET tblYourTable.Name = tblYourTable.ReplaceName 
    WHERE Left(tblYourTable.Name,3) = "Dav";
    You did NOT name your field "Name", right?
    Last edited by Micron; 09-01-2020 at 01:31 PM. Reason: added question
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    What is the context of this? Do you have a main table that you want to change the "nick name" to the real first name using a small table with the cross reference?

    I think you will need to use VBA to get through all of the names or else the criteria will be huge. You will need to explicitly enumerate the "names" in the WHERE clause"
    Code:
    UPDATE tblYourTable SET tblYourTable.Name = tblYourTable.ReplaceName 
    WHERE Left(tblYourTable.Name,3) = "Dav" Or
          Left(tblYourTable.Name,3) = "Sam" Or
          Left(tblYourTable.Name,3) = "Ric" Or
          Left(tblYourTable.Name,3) = "Ed" Or ...

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,445
    I can see lots of potential issues

    what if Sam can also stand for Samantha, or Ed stands for Edwin or Edgar?

    and if the name is already David?

    Simplistically you can link, something along these lines

    SELECT FirstName, ReplaceName
    FROM tblNames INNER JOIN tblReplacements ON tblNames.FirstName=tblReplacements.Name

    OR

    SELECT FirstName, ReplaceName
    FROM tblNames, tblReplacements
    WHERE tblNames.FirstName LIKE tblReplacements.Name & "*"

    Overall you have not set yourself an easy task

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,815
    I can see lots of potential issues
    Me too. All good comments guys; I just figured that after my answer, we'd find out that there was a lot more to it. I just could not imagine using a variation of Left on every possible value in a field.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,445
    you need to have the replacement table - I have a standard one I use where typically one 'master' phrase can have many alternatives. (Master=Edward, Aliases=Ed, Eddie)

    tblPhrases
    PhrasePK, PhraseTypeFK, Phrase, MasterFK

    the 'replacement' is the record where PhrasePK=MasterFK, Phrase and PhraseTypeFK has a compound index, no dups

    PhraseTypeFK - if I need say aliases for employee names and products or perhaps based on language or different offices - identified in a phraseType table

    Phrases can be a single letter (used in translation), a single word or a whole sentence



    you can also use non standard joins rather than a cartesian query - the example sql in my previous post could be combined to look like

    Code:
    SELECT FirstName, ReplaceName
    FROM tblNames INNER JOIN tblReplacements ON tblNames.FirstName LIKE tblReplacements.Name & "*"

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

Similar Threads

  1. macrohelp needed - error message if statement
    By dcorleto in forum Queries
    Replies: 8
    Last Post: 07-22-2013, 12:44 PM
  2. If Statement Criteria Needed for form
    By burrina in forum Forms
    Replies: 4
    Last Post: 12-06-2012, 10:47 PM
  3. Replies: 4
    Last Post: 11-19-2012, 01:59 PM
  4. If Statement Criteria Needed for form
    By burrina in forum Forms
    Replies: 6
    Last Post: 11-16-2012, 11:00 PM
  5. Replies: 11
    Last Post: 03-21-2012, 12:51 PM

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