Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    SunOffice is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    15

    Identify strings into 2nd table's records then update 1st table (kind of Vlookup)

    Hi All,


    Could you please help on below situation in MS Access 2013:


    I have two database tables as below:
    tbl1_MainDB --- It has a field named as "City" where I get huge data for some city names. Sometimes This field may have some unknown/new names which are not listed in our 2nd table ("tbl2_RefrDB")


    tbl2_RefrDB --- It's a reference table which has raw names for cities, and then standard names of their city and state in another fields.


    Target --- I want to create a VBA prorgram (Sql query) which can look from tbl1_MainDB.[City] to tbl2_RefrDB.[Raw_City] field, and if found then pick the "Standard_State" and "Standard_City" record values from there, and update into the 1st table "tbl1_MainDB".
    ...if not found in "tbl2_RefrDB" table, then user can be informed & ask for updating the new/unmatched city record as a new record in this table.


    Please review the attached sample database for more details.


    Thanks & Regards,


    SunOffice
    Attached Files Attached Files

  2. #2
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,459

  3. #3
    SunOffice is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    15
    I am new to this, so I posted it there too as looking for a fast solution/help.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    Quote Originally Posted by SunOffice View Post
    I am new to this, so I posted it there too as looking for a fast solution/help.
    IMHO that's not the best thing to do. Please read: http://www.excelguru.ca/content.php?184
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    SunOffice is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    15
    Please find attached as the same file, only the file type(extension) is changed to *.mdb

    Thanks & Regards,
    SO
    Attached Files Attached Files

  6. #6
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,459
    From your data, it is not at all clear to me what you require. I appreciate it is a language issue, but you must be clearer on your terminology

    Please explain why you want to do this and maintain a separate table. Your requirement breaks the normalisation rules (google normalisation for more info), consequently requiring potentially complex code whereas I suspect all you need is to use what is called 'cascading comboboxes' and you do not need tbl1_Main.

  7. #7
    SunOffice is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    15
    Thank you for reply!

    Aim --- I want to update my 1st table's two fields - tbl1_MainDB.[Standard_State] and tbl1_MainDB.[Standard_City] by looking values of tbl1_MainDB.[City] field into 2nd table's tbl2_RefrDB.[Raw_City] field.
    Process --- We need to search the standard names from
    tbl1_MainDB.[City] field to tbl2_RefrDB.[Raw_City] field, then update the relevant record of the 1st table.

    Example:
    We need to look for "Delhi" from
    tbl1_MainDB.[City] field to tbl2_RefrDB.[Raw_City] field.
    As "Delhi" as a string does not have any ";" sign in the 1st table, so we need to actually look for "Delhi;" string into tbl2_RefrDB.[Raw_City] field. if it is found in tbl2_RefrDB.[Raw_City] field, then take the relevant record details (row) and update the looked up values in tbl1_MainDB.[Standard_State] and tbl1_MainDB.[Standard_City] fields of 1st table.

    ...if city names were matched in "tbl2_RefrDB" table, then user can be informed & ask for updating the new/unmatched city record as a new record in this table.


    Hope now it is more clear....

    Thanks & Regards,
    SO

  8. #8
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,459
    1. Please can you clarify why you need to do this when you can just look at the detail in the second table.

    I still do not understand what you are trying to achieve. Please provide a full example (your current example is not detailed enough) - put into an excel spreadsheet if it helps

    2. some data that the user would want to update, show relevant data from both tables, before update and after update
    3. some data the user would not want to update, show relevant data from both tables and explain why the user would not update
    4. having updated the table, an example of what you use the data for
    5. If you use Mumbai as an example - how would you differentiate between the second and third records in the 2nd table?


    What you require is potentially very complex and before spending a lot of my time I want to be sure it is necessary to your process. As explained before, you need to investigate normalisation - I cannot see how your table2 raw_city field is going to do what you want

  9. #9
    SunOffice is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    15
    My Reply:
    1. Please can you clarify why you need to do this when you can just look at the detail in the second table.
    Answer 1) I have a huge database with a field for City Names, and I want to get their accurate & standard Names from a Reference table.


    I still do not understand what you are trying to achieve. Please provide a full example (your current example is not detailed enough) - put into an excel spreadsheet if it helps
    Answer - The attached database is a demo file with the same kind of layout for what I see in the actual database.


    2. some data that the user would want to update, show relevant data from both tables, before update and after update
    Answer 2) 2nd table is just a reference table for getting the accurate & standard names of cities, and their correct State Names
    for example: "Bombay;" should be updated as "Mumbai" in the 1st table.

    3. some data the user would not want to update, show relevant data from both tables and explain why the user would not update
    Answer 3) In case, in our 1st database we get any city name which is not listed in our 2nd table (reference table)'s "Raw_City" field, that means we need to update that (make a new entry) in the 2nd table for further use, so that next time that will be searchable in there.

    4. having updated the table, an example of what you use the data for
    Answer 4) After getting the standard City names and State names in the 1st table, I will need to perform some analysis using Pivottable tricks.

    5. If you use Mumbai as an example - how would you differentiate between the second and third records in the 2nd table?
    Answer 5) Each full name string is connected by a ";" sign in the tbl2_RefrDB.[Raw_City] field.
    So if you want to look for Mumbai, then search for "Mumbai;" string in
    tbl2_RefrDB.[Raw_City] field, if found then get the tbl2_RefrDB.[Standard_State] and tbl2_RefrDB.[Standard_City] fields into 1st table to have a most accurate database.

    You can think above# 5 in another way as below too:
    tbl2_RefrDB.[Raw_City] field contains most possible city name strings in itself as below, but afterall the standard city name is "Mumbai" only:
    Mumbai & Mumbai(Suburban);
    Mumbai;
    Greater Mumbai;
    Bombay;



    What you require is potentially very complex and before spending a lot of my time I want to be sure it is necessary to your process. As explained before, you need to investigate normalisation - I cannot see how your table2 raw_city field is going to do what you want
    Answer - let's think these two tables in excel VBA.
    If we want to update the Standard Names from sheet2 (table2) into sheet1 (table1), then we need to write a program using Array & Split functions:

    Code:
    ' below VBA code is just for understanding the validation part in excel as we have multiple strings in single cell:
    Dim x as Long, y as Long, z as long
    Dim var as variant
    
    For x = 1 to Sheet1.Range("A" & rows.count).end(xlup).row
     For y = 1 to Sheet2.Range("A" & rows.count).end(xlup).row
       var = Split(Sheet2.Range("A" & y).value,";")
       For z = Lbound(var) to Ubound(var)
        if Sheet1.Range("A" & x).value = var(i) then
          '--- City name found, so pick the standard City and State names from Sheet2, then update their details on Sheet1
    
         '--- early exit from loop
         GOTO iNxt:
        End if 
       Next z
    
       var = Empty
     Next y
    
    iNxt: 
    Next x
    then run a loop...

    ...and here in MS Access i want to avoid looping on large datasets.

  10. #10
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,459
    regret no db/excel attached

    with regards 5

    if you look for 'Mumbai' or 'Mumbai;' on raw_city you will find two records - which record should it choose? Mumbai or Navi Mumbai?

    Standard_State Standard_City Raw_City
    Maharashtra Mumbai Mumbai; Mumbai & Mumbai(Suburban); Greater Mumbai; Bombay;
    Maharashtra Navi Mumbai Navi Mumbai; Panvel; Raigarh;

  11. #11
    SunOffice is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    15
    Answer 5) Each full name string is connected by a ";" sign in the tbl2_RefrDB.[Raw_City] field.

    So if you want to look for Mumbai, then search for "Mumbai;" string instead of only "Mumbai" keyword in
    tbl2_RefrDB.[Raw_City] field, if found then get the tbl2_RefrDB.[Standard_State] and tbl2_RefrDB.[Standard_City] fields into 1st table to have a most accurate database.


    In your attached picture, 1st records is the correct, because "Mumbai;" keyword is their.
    2nd record is not correct for me, because "Navi Mumbai" keyword is not a full name there. Remember... Each full name string is connected by a ";" sign.

  12. #12
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,459
    just worked out what you are trying to do. Access is not a glorified Excel, you need to construct your tables differently

    1. create a new table - call it tblAlias
    2. Have 2 fields, Raw_City and Standard_City. Make Raw_City the primary key - so duplicates not allowed
    3. populate as follows (you can create a bit of vba to run through your existing table to split the current raw_city field

    Raw_City........Standard_City
    NCT of Delhi......Delhi
    New Delhi.........Delhi
    Delhi................Delhi
    Mumbai............Mumbai
    Mumbai & Mumbai(Suburban)..Mumbai
    Greater Mumbai..Mumbai
    Bombay............Mumbai
    Navi Mumbai.....Navi Mumbai
    Panvel..............Navi Mumbai
    Raigarh............Navi Mumbai

    etc


    4. remove the raw_city field from tbl2_Refr
    5. create a query to give you a complete list of aliases and standards

    Code:
    SELECT tblAlias.Raw_City, tblAlias.Standard_City, tbl2_Refr.Standard_State
    FROM tblAlias INNER JOIN tbl2_Refr ON tblAlias.Standard_City=tbl2_Refr.Standard_City
    Ideally, your tbl2_Refr table should have an autonumberID and you would use this ID instead of Standard_City in tblAliases to link the two tables together.

  13. #13
    SunOffice is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    15
    Thank you for quick reply!

    Below is working partially:
    Code:
    UPDATE tbl1_MainDB 
    INNER JOIN tbl2_RefrDB 
    ON tbl1_MainDB.City&";" = MID(tbl2_RefrDB.[Raw_City],InStr(1,tbl2_RefrDB.[Raw_City],tbl2_RefrDB.[Standard_CityTEST]),LEN(tbl2_RefrDB.[Standard_CityTEST])+1) 
    SET tbl1_MainDB.Standard_State = tbl2_RefrDB.[Standard_State];

    Please help me on it as some records are still not coming up.

    Apart from above, please guide me how to deal/learn on your this trick... "Ideally, your tbl2_Refr table should have an autonumberID and you would use this ID instead of Standard_City in tblAliases to link the two tables together."


  14. #14
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,459
    try this

    Code:
    UPDATE tbl1_MainDB, tbl2_RefrDB SET tbl1_MainDB.Standard_State = tbl2_RefrDB.[Standard_State]
    WHERE (((InStr([Raw_City],[city]))<>0));

  15. #15
    SunOffice is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    15
    Thank you!
    ...but it's working on only tbl1_MainDB.Standard_State.

    I tried to modify your query as below, and found that Standard_City value is not correct for "Mumbai" city. It should give "Mumbai" instead of "Navi Mumbai". Please review my modification:
    Code:
    UPDATE tbl1_MainDB, tbl2_RefrDB 
    SET tbl1_MainDB.Standard_State = tbl2_RefrDB.[Standard_State], tbl1_MainDB.Standard_City = tbl2_RefrDB.[Standard_City]
    WHERE (((InStr([Raw_City],[city]))<>0));
    Please help!

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-29-2014, 03:44 AM
  2. Vlookup from the base table
    By irfanparbatani in forum Queries
    Replies: 28
    Last Post: 08-04-2014, 04:40 PM
  3. Replies: 4
    Last Post: 04-01-2014, 02:11 PM
  4. Replies: 2
    Last Post: 10-09-2012, 03:20 PM
  5. Replies: 2
    Last Post: 12-20-2011, 07:33 AM

Tags for this Thread

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