Extracting data from a string/field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • artemetis
    New Member
    • Jul 2007
    • 76

    Extracting data from a string/field

    Ok, I'm back again.
    I've got the following table.


    TableName = tblOriginal
    uid - self explanatory
    firstName - self explanatory
    lastName - self explanatory
    groupMaster - contains a numeric string containing 21 digits.

    My question surrounds the 'groupMaster' field.
    I need the first four groups of three, from the right to be inserted into another existing table, along with the other corresponding fields.

    sample string from groupMaster: 333444555666777 888999

    Looking at the above string:
    999 will be inserted into tblMaster.group 3
    777 will be inserted into tblMaster.group 2
    666 will be inserted into tblMaster.group 1
    *all other fields will insert into corresponding names.

    TableName = tblMaster
    uid
    firstName
    lastName
    groupMaster
    group1
    group2
    group3


    As always......... ...thanks in advance!!!
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    Hi artemetis,
    You want to use the Mid Function (http://office.microsoft.com/en-us/ac...288811033.aspx)
    See if you have any specific problems with that.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by artemetis
      Ok, I'm back again.
      I've got the following table.


      TableName = tblOriginal
      uid - self explanatory
      firstName - self explanatory
      lastName - self explanatory
      groupMaster - contains a numeric string containing 21 digits.

      My question surrounds the 'groupMaster' field.
      I need the first four groups of three, from the right to be inserted into another existing table, along with the other corresponding fields.

      sample string from groupMaster: 333444555666777 888999

      Looking at the above string:
      999 will be inserted into tblMaster.group 3
      777 will be inserted into tblMaster.group 2
      666 will be inserted into tblMaster.group 1
      *all other fields will insert into corresponding names.

      TableName = tblMaster
      uid
      firstName
      lastName
      groupMaster
      group1
      group2
      group3


      As always......... ...thanks in advance!!!
      Assuming a precise, 21-digit Format, a code-based approach would look something like this:
      Code:
      Dim MyDB As DAO.Database
      Dim rstOriginal As DAO.Recordset
      Dim rstMaster As DAO.Recordset
      
      Set MyDB = CurrentDb
      Set rstOriginal = MyDB.OpenRecordset("tblOriginal", dbOpenForwardOnly)
      Set rstMaster = MyDB.OpenRecordset("tblMaster", dbOpenDynaset)
      
      With rstOriginal
        Do While Not .EOF
          rstMaster.AddNew
            rstMaster![GroupMaster] = ![GroupMaster]
            rstMaster![Group4] = Right(![GroupMaster], 3)
            rstMaster![Group3] = Mid(![GroupMaster], 16, 3)
            rstMaster![Group2] = Mid(![GroupMaster], 13, 3)
            rstMaster![Group1] = Mid(![GroupMaster], 10, 3)
          rstMaster.Update
          .MoveNext
        Loop
      End With
      
      RefreshDatabaseWindow
      
      rstOriginal.Close
      rstMaster.Close
      Set rstOriginal = Nothing
      Set rstMaster = Nothing

      Comment

      • artemetis
        New Member
        • Jul 2007
        • 76

        #4
        Thanks Folks!

        ADezii - how does your code process a blank location, or would it?
        For example(s):
        "777 999"
        "777888 "
        where each block of missing values has three null spaces?
        I'm seeing that in many of my records.

        Thanks again in advance!!!

        Comment

        • ChipR
          Recognized Expert Top Contributor
          • Jul 2008
          • 1289

          #5
          Edited this out so you don't accidentally do it before reading subsequent posts.

          Comment

          • ChipR
            Recognized Expert Top Contributor
            • Jul 2008
            • 1289

            #6
            Sorry, ignore that last suggestion. I though from your example that the items had spaces between them, but a null value is a different story. If you don't use [ code ] tags, your multiple spaces get turned into single spaces.
            ADezii's code will still work fine in this case. Blanks are treated much differently from NULLs.

            Comment

            • artemetis
              New Member
              • Jul 2007
              • 76

              #7
              Thanks ChipR.

              Yeah, the data that I've been given is a MESS.
              In that field of 21, if there is no three digit value, there are three spaces.
              It's like 7 fields of three in one field. Grrrrrrrrrrr!

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32662

                #8
                This should be an easy SQL job :
                Code:
                INSERT INTO tblMaster
                      (uid,
                       firstName,
                       lastName,
                       groupMaster,
                       group1,
                       group2,
                       group3)
                
                SELECT uid,
                       firstName,
                       lastName,
                       groupMaster,
                       Mid([groupMaster],10,3) AS group1,
                       Mid([groupMaster],13,3) AS group2,
                       Mid([groupMaster],19,3) AS group3

                Comment

                Working...