Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79

    VBA Loop to Combine Records

    Hello all....I'm back........

    Okay...well I managed to get all of my queries and such to run just by using SQL....problem is....management doesn't like the result. So....I need to go back to resorting to VBA to combine the records. I have however, managed to restructure things a bit to deal with the records a little differently.

    The gist of what I need to do is take all of the information from Record 1 and combine the last 5 fields of Record 2 into a new record (in another table). The last 5 fields of record 2 have the same column names as record 1, so they need to go into "new" fields. I have written a create table SQL so that the records have somewhere to go, but now quite sure if I am going in the right direction.



    Here is my code:
    Code:
    OPTION EXPLICIT
    Function Get_DB_Values()
    'Get values from a table using a query in VBA.
    'Process values row by row.
    'Insert processed row into another Table.
     
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Dim intCoordID As Integer
    Dim intUnique_ID As Integer
    Dim intProc_Forest As Integer
    Dim strProc_Region As String
    Dim strFSVeg_Location As String 
    Dim strFSVeg_Stand_Num As String
    Dim intUTM_Easting As Long 
    Dim intUTM_Northing As Long
    Dim intUTM_Zone As Integer
    Dim strUTM_Datum As String 
    Dim intLAT_DD As Long
    Dim intLON_DD As Long
    Dim strLAT_LON_Datum As String
    Dim intPhoto_Year As Integer 
    Dim strNorth As String 
    Dim strEast As String 
    Dim strSouth As String
    Dim strWest As String
    Dim intPrevCoordID As Integer
    Dim intPrevUnique_ID As Integer
    Dim intPrevProc_Forest As Integer
    Dim strPrevProc_Region As String
    Dim strPrevFSVeg_Location As String 
    Dim strPrevFSVeg_Stand_Num As String
    Dim intPrevUTM_Easting As Long 
    Dim intPrevUTM_Northing As Long 
    Dim intPrevUTM_Zone As Integer
    Dim strPrevUTM_Datum As String 
    Dim intPrevLAT_DD As Long
    Dim intPrevLON_DD As Long
    Dim strPrevLAT_LON_Datum As String
    Dim intPrevPhoto_Year As Integer
    Dim strPrevNorth As String 
    Dim strPrevEast As String 
    Dim strPrevSouth As String
    Dim strPrevWest As String
    Dim intNewCoordID As Integer
    Dim intNewUnique_ID As Integer
    Dim intNewProc_Forest As Integer
    Dim strNewProc_Region As String
    Dim strNewFSVeg_Location As String
    Dim strNewFSVeg_Stand_Num As String
    Dim intNewUTM_Easting As Long
    Dim intNewUTM_Northing As Long
    Dim intNewUTM_Zone As Integer
    Dim strNewUTM_Datum As String 
    Dim intNewLAT_DD As Long
    Dim intNewLON_DD As Long
    Dim strNewLAT_LON_Datum As String
    Dim intNewPhoto_Year As Integer
    Dim strNewNorth As String
    Dim strNewEast As String
    Dim strNewSouth As String
    Dim strNewWest As String
    Dim intSQL As Integer
    Dim strSQL As String
    Dim intRecordCount As Integer
     
    'On Error GoTo Error_Handle
    
    CurrentDB.Execute "DROP TABLE Photo_Test" 'This is to delete the table from previous runs of the code
    CurrentDB.Execute "Create_Photo_Test" 'This is the create table query where the data needs to end up
     
    Set db = CurrentDb
     
        strSQL = "Select * From Link2 ORDER BY CoordID "
        intRecordCount = 1
     
    Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
     
    With rs
     
    'This Do While loop goes through all the records in strSQL.
    Do While Not rs.EOF
     
    If intRecordCount = 1 Then
     intCoordID = rs![CoordID]
     intUnique_ID = rs![Unique_ID]
     strProc_Region = rs![Proc_Region]
     intProc_Forest = rs![Proc_Forest]
     strFSVeg_Location = rs![FSVeg_Location]
     strFSVeg_Stand_No = rs![FSVeg_Stand_Num]
     intUTM_Easting = rs![UTM_Easting]
     intUTM_Northing = rs![UTM_Northing]
     intUTM_Zone = rs![UTM_Zone]
     strUTM_Datum = rs![UTM_Datum]
     intLAT_DD = rs![LAT_DD]
     intLON_DD = rs![LON_DD]
     strLAT_LON_Datum = rs![LAT_LON_Datum]
     intPhoto_Year = rs![Photo_Year]
     strIMG_North = rs![IMG_North]
     strIMG_East = rs![IMG_East]
     strIMG_South = rs![IMG_South]
     strIMG_West = rs![IMG_West]
     
        intRecordCount = intRecordCount + 1
     
    Else 'Not first record.
        intNewUnique_ID = rs![Unique_ID]
     
    If intNewUnique_ID = intPrevUnique_ID Then 'Same Field1 - concatenate values.
     intNewCoordID = rs![CoordID]
     intNewUnique_ID = rs![Unique_ID]
     strNewProc_Region = rs![Proc_Region]
     intNewProc_Forest = rs![Proc_Forest]
     strNewFSVeg_Location = rs![FSVeg_Location]
     strNewFSVeg_Stand_No = rs![FSVeg_Stand_Num]
     intNewUTM_Easting = rs![UTM_Easting]
     intNewUTM_Northing = rs![UTM_Northing]
     intNewUTM_Zone = rs![UTM_Zone]
     strNewUTM_Datum = rs![UTM_Datum]
     intNewLAT_DD = rs![LAT_DD]
     intNewLON_DD = rs![LON_DD]
     strNewLAT_LON_Datum = rs![LAT_LON_Datum]
     intNewPhoto_Year = rs![Photo_Year]
     strNewIMG_North = rs![IMG_North]
     strNewIMG_East = rs![IMG_East]
     strNewIMG_South = rs![IMG_South]
     strNewIMG_West = rs![IMG_West]
     
        'This is where the FUN begins.....Yeah Right
     
    If intNewUnique_ID <> intPrevUnique_ID Then
     intNewPhoto_Year = intPhoto_Year_2
     strNewNorth = strNorth_2
     strNewEast = strEast_2
     strNewSouth = strSouth_2
     strNewWest = strWest_2
    End If
     
     
    Else 'Field1 changed - Write the record to other table.
    'Create Insert SQL.
     
    strSQL = "INSERT INTO Photo_Test (CoordID, Unique_ID, Proc_Region, Proc_Forest, FSVeg_Location, FSVeg_Stand_Num, UTM_Easting, UTM_Northing, UTM_Zone, UTM_Datum, LAT_DD, LON_DD, LAT_LON_Datum, Photo_Year, North, East, South, West, Photo_Year2, North_2, East_2, South_2, West_2) "
    
    'The following is commented out since I have not modified it yet 
    
    'strSQL = strSQL & "VALUES (" & "'" & intNewUTM_Easting & "'" & ", " & "'" & intNorthing_UTM & "'" & ", " & "'" & strFSVeg_Location & "'" & ", " & "'" & strFSVeg_Stand_No & "'" & ", " & "'" & intNewPhoto_Year & "'" & ", " & "'" & strIMG_North & "'" & ", " & "'" & strIMG_East & "'" & ", " & "'" & strIMG_South & "'" & ", " & "'" & strIMG_West & "'" & ", " & "'" & intNewPhoto_Year2 & "'" & ", " & "'" & strIMG_North2 & "'" & ", " & "'" & strIMG_East2 & "'" & ", " & "'" & strIMG_South2 & "'" & ", " & "'" & strNewIMG_West2 & "'" & "); "
     
    'Execute Insert SQL
    DoCmd.RunSQL strSQL
     
    'Populate current row values into variables.
     intCoordID = rs![CoordID]
     intUnique_ID = rs![Unique_ID]
     strProc_Region = rs![Proc_Region]
     intProc_Forest = rs![Proc_Forest]
     strFSVeg_Location = rs![FSVeg_Location]
     strFSVeg_Stand_No = rs![FSVeg_Stand_Num]
     intUTM_Easting = rs![UTM_Easting]
     intUTM_Northing = rs![UTM_Northing]
     intUTM_Zone = rs![UTM_Zone]
     strUTM_Datum = rs![UTM_Datum]
     intLAT_DD = rs![LAT_DD]
     intLON_DD = rs![LON_DD]
     strLAT_LON_Datum = rs![LAT_LON_Datum]
     intPhoto_Year = rs![Photo_Year]
     strNorth = rs![North]
     strIEast = rs![East]
     strSouth = rs![South]
     strWest = rs![West]
     
    End If 'End If strNewField1 = strField1 Then
    End If 'End If intRecordCount = 1
     
     intPrevCoordID = rs![CoordID]
     intPrevUnique_ID = rs![Unique_ID]
     strPrevProc_Region = rs![Proc_Region]
     intPrevProc_Forest = rs![Proc_Forest]
     strPrevFSVeg_Location = rs![FSVeg_Location]
     strPrevFSVeg_Stand_No = rs![FSVeg_Stand_Num]
     intPrevUTM_Easting = rs![UTM_Easting]
     intPrevUTM_Northing = rs![UTM_Northing]
     intPrevUTM_Zone = rs![UTM_Zone]
     strPrevUTM_Datum = rs![UTM_Datum]
     intPrevLAT_DD = rs![LAT_DD]
     intPrevLON_DD = rs![LON_DD]
     strPrevLAT_LON_Datum = rs![LAT_LON_Datum]
     intPrevPhoto_Year = rs![Photo_Year]
     strPrevNorth = rs![IMG_North]
     strPrevEast = rs![IMG_East]
     strPrevSouth = rs![IMG_South]
     strPrevWest = rs![IMG_West]
     
    .MoveNext 'Move to next record in recordset.
     
    Loop 'Back to 'Do While' to check if we are at the end of the file.
    'Create SQL for Last Row of data that is still stored even though Access found the EOF.
     
    strSQL = "INSERT INTO Photo_Test (CoordID, Unique_ID, Proc_Region, Proc_Forest, FSVeg_Location, FSVeg_Stand_No, UTM_Easting, UTM_Northing, UTM_Zone, UTM_Datum, LAT_DD, LON_DD, LAT_LON_Datum, Photo_Year, North, East, South, West, Photo_Year2, North_2, East_2, South_2, West_2) "
    
    'The following is commented out since I have not modified it yet
     
    'strSQL = strSQL & "VALUES (" & "'" & intEasting_UTM & "'" & ", " & "'" & intNorthing_UTM & "'" & ", " & "'" & strFSVeg_Location & "'" & ", " & "'" & strFSVeg_Stand_No & "'" & ", " & "'" & intPhoto_Year & "'" & ", " & "'" & strIMG_North & "'" & ", " & "'" & strIMG_East & "'" & ", " & "'" & strIMG_South & "'" & ", " & "'" & strIMG_West & "'" & ", " & "'" & intPhoto_Year2 & "'" & ", " & "'" & strIMG_North2 & "'" & ", " & "'" & strIMG_East2 & "'" & ", " & "'" & strIMG_South2 & "'" & ", " & "'" & strIMG_West2 & "'" & "); "
     
    Execute Insert SQL.
     
    DoCmd.RunSQL strSQL
     
    Exit_Get_DB_Values:
    If Not rs Is Nothing Then
    rs.Close
    Set rs = Nothing
    End If
    Set db = Nothing
    Exit Function
    'Error_Handle:
    Resume Exit_Get_DB_Values
    End With
    End Function
    Thoughts?....Please add your comments to the above code so I know where you are referring to specifically.

    The data is coming from a query called Link2.
    The data needs to go to the table Photo_Test.

  2. #2
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79

    Lightbulb Source Table Example

    Attached is the source table in Excel format (in .zip file) so you can see what I am dealing with. I have highlighted alternating sets of records. Most are paired, but there are a few singular records.

    The CoordID (yellow) is the foreign key used to pair the records. The Unique_ID (Orange) is the PK for the tables that this table is based upon.

    The last 5 fields are what need to get mashed into the leading record of the pair. Preferably I want the smallest Photo_Year (and associated image paths) value first, the larger Photo_Year (and associated image paths) value second.

    All field values are required in the final table except the Unique_ID. If needed I can simplify the table and add the rest of the info (Rgion, Forest, location, stand, and all coordinate values [UTM and Lat/Lon]) in another query later on.
    Attached Files Attached Files

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The gist of what I need to do is take all of the information from Record 1 and combine the last 5 fields of Record 2 into a new record (in another table). The last 5 fields of record 2 have the same column names as record 1, so they need to go into "new" fields.
    It helps to see the Excel spreadsheet.
    Lets see if I understand what you are trying to do.......

    There are two lines with CoordID = 1. Those two lines have different Unique_ID's.
    You are opening a recordset using the SQL of : "Select * From Link2 ORDER BY CoordID " Q: does it matter which [Unique_ID] is selected first?

    You want to create a new record in another table, "Photo_Test", taking all of the field of the first row in recordset "rs", append the last 5 fields of the second row of the recordset "rs" (total 23 fields?).

    Then what? What happens to the second row of the recordset?
    And the other records??

    (still working through the code )

  4. #4
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79
    Quote Originally Posted by ssanfu View Post
    It helps to see the Excel spreadsheet.
    Lets see if I understand what you are trying to do.......

    There are two lines with CoordID = 1. Those two lines have different Unique_ID's.
    You are opening a recordset using the SQL of : "Select * From Link2 ORDER BY CoordID " Q: does it matter which [Unique_ID] is selected first?
    I would prefer to select the Unique_ID that corresponds to the earliest Photo_Year first, but am not really picky at this point. I just need to get the matching CoordID's on the same line....if need be, I can deal with writing another query to parse out what info I do need if all the info ends up on the same line.

    You want to create a new record in another table, "Photo_Test", taking all of the field of the first row in recordset "rs", append the last 5 fields of the second row of the recordset "rs" (total 23 fields?).

    Then what? What happens to the second row of the recordset?
    And the other records??
    Like I said above....if all of the information gets jammed into the same line so be it...I can just write a new select query to parse out what I need. For this, we may need to use a simplified version of the table you are seeing that includes just the CoordID, Unique_ID, Photo_Year, North, East, South, West. I can add the rest of the information back in later. All of the records need to be looped through so that all pairs are joined together, but yet preserves any singular records.

    (still working through the code )
    The code is a work in progress...it was originally from Robeen awhile back when we made a first attempt at this, so cut/paste and chop away

  5. #5
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79
    Anyone?.....

  6. #6
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79

    Simplified Source Table

    Here is a simplified table in Exel format. It has been chopped down to the minimum required fields. I can add in all the other stuff with a query later. This cuts down on the amout of VBA that need to be written/modified.
    Attached Files Attached Files

  7. #7
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Still not sure....

    For "CoordID" = 1...... you want to append (or replace) the columns Photo_Year, North, East, South & West where the photo year = 2014 to the row where "CoordID" = 1 and Photo year = 2009. Will there be only 1 record in the new table "Photo_Test" or 2 records where "CoordID" = 1?

    Do this for all records where there are two lines for the same "CoordID"?

    What should happen where there is only one line for "CoordID" ([CoordID]=3)?

  8. #8
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79
    Quote Originally Posted by ssanfu View Post
    Still not sure....

    For "CoordID" = 1...... you want to append (or replace) the columns Photo_Year, North, East, South & West where the photo year = 2014 to the row where "CoordID" = 1 and Photo year = 2009. Will there be only 1 record in the new table "Photo_Test" or 2 records where "CoordID" = 1?

    Do this for all records where there are two lines for the same "CoordID"?

    What should happen where there is only one line for "CoordID" ([CoordID]=3)?
    Okay...this is what needs to happen...
    1. Look at Row 1, write all values to table "Photo_Test"
    2. Look at Row 2, IF the CoordID matches the CoordID of Row 1 AND the Unique_ID does NOT match, append Photo_Year, and file(s) (depends upon the table we are using) to Row 1 as Photo_Year 2, and *File*_2. Otherwise, write all values into new row of Photo_Test
    3. Look at Row 3, Does the CoordID match Row 1?....Yes...append as above using _3 in field name...No (most likely)....Does it match any other row?...Yes...append to that row in Photo_Test...No...Write all values into next row of "Photo_Test".
    4. Look at Row 4,....same process as above from point 2...
    5. ....and so on.
    6. Loop until EOF
    Basically it needs to look at the CoordID's of each record and match them together into the same record of "Photo_Test"

    The final table should look like this (if using the smaller table):

    CoordID Photo_Year_1 File_Base_1 Photo_Year_2 File_Base_2 ... ...
    1 2009 cp06_0070 2014 cp06_0070
    2 2011 2011CO0604091702 2015 2015CO0604091702

    Or, if using the larger table (names and values truncated to preserve space in post):

    CoordID Region Forest Location Stand Easting Northing Zone Datum Lat Lon Datum Year_1 North_1 East_1 South_1 West_1 Year_2 North_2 South_2 West_2 ... ...
    1 02 10 CP0604 0917 445... 4512... 13 W84 ... ... W84 2009 ... ... ... ... 2014 ... ... ... ... ...
    2 02 10 CP0604 0917 4452.. 45127.. 13 W84 ... ... W84 2011 ... ... ... ... 2015 ... ... ... ... ...
    ...

    If there is just a single record for a CoordID, simply write it's values into the Photo_Test table and move on. So that is what needs to happen in a nutshell.

  9. #9
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Got it! (finally )

    One last question.. You have

    CurrentDB.Execute "Create_Photo_Test"

    Would you post the SQL of the make table query so I know the field names/types?

  10. #10
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79

    SQL as requested

    Here is the SQL for the creat table:

    Code:
    CREATE TABLE Photo_Test (CoordID AUTOINCREMENT, Proc_Region Double, Proc_Forest Double, FSVeg_Location Text, FSVeg_Stand_Num Text, UTM_Easting Double, UTM_Northing Double, UTM_Zone Double, UTM_Datum Text, LAT_DD Double, LON_DD Double, LAT_LON_Datum Text, Photo_Year_1 Double, North_1 Text, East_1 Text, South_1 Text, West_1 Text, Photo_Year_2 Double, North_2 Text, East_2 Text, South_2 Text, West_2 Text, PRIMARY KEY (CoordID));
    If you decide to go with the smaller of the two tables that I posted earlier, just take out the North*, East*, South*, West* , Lat/Lon*, UTM, Proc*, FSVeg* fields. Then just add <File_Base_1 text, File_Base_2 text> in their place. Just make sure that the Photo_Year* comes before each of the File_Base* fields.

    One more thing....since the data is coming from another table, the CoordID field in Photo_Test can just be a Double if needed....that way it preserves the original CoordID data.

  11. #11
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79
    I am willing to start with a fresh VBA if that is easier than trying to deal with the beast in my first post.

  12. #12
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79
    Anyone?.....am getting a bit frustrated with this.

  13. #13
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79

    Angry Arrgh!!!

    Okay....been working with the VBA a bit...and basing it off of the smaller table. Can someone PLEASE take a look at it and see where I am going wrong? If you need guidance as to what I need to do, refer to the previous 5-6 posts, and look at the Simple_qry.xlsx in the second zip file.

    Code:
    Option Compare Database
    Option Explicit
    Function Get_DB_Values()
    'Get values from a table using a query in VBA.
    'Process values row by row.
    'Insert processed row into another Table.
     
    CurrentDb.Execute "DROP TABLE Photo_Test3"
    CurrentDb.Execute "Create_Photo_Test3"
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim intCoordID As Integer
    Dim intUnique_ID As Integer
    Dim intPhoto_Year As Integer
    Dim strFile_Base As String
    Dim intPhoto_Year_2 As Integer
    Dim strFile_Base_2 As String
    Dim intPrevCoordID As Integer
    Dim intPrevUnique_ID As Integer
    Dim intPrevPhoto_Year As Integer
    Dim strPrevFile_Base As String
    Dim intNewCoordID As Integer
    Dim intNewUnique_ID As Integer
    Dim intNewPhoto_Year As Integer
    Dim strNewFile_Base As String
    Dim intSQL As Integer
    Dim strSQL As String
    Dim intRecordCount As Integer
     
    'On Error GoTo Error_Handle
     
    Set db = CurrentDb
     
        strSQL = "Select * From Simple_qry ORDER BY CoordID "
        intRecordCount = 1
     
    Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
     
    With rs
     
    'This Do While loop goes through all the records in strSQL.
    Do While Not rs.EOF
     
    If intRecordCount = 1 Then
        intCoordID = rs![CoordID]
        intUnique_ID = rs![Unique_ID]
        intPhoto_Year = rs![Photo_Year]
        strFile_Base = rs![File_Base]
        
        intRecordCount = intRecordCount + 1
     
    Else 'Not first record.
        intNewCoordID = rs![CoordID]
     
    'If intNewCoordID = intPrevCoordID Then 'Same Field1 - Combine records.
        'intNewPhoto_Year = rs![Photo_Year_2]
        'strNewFile_Base = rs![File_Base_2]
     
        'This is where the FUN begins.....Yeah Right
     
    If intNewCoordID <> intPrevCoordID Then
        intNewCoordID = rs![CoordID]
        intNewUnique_ID = rs![Unique_ID]
        intNewPhoto_Year = rs![Photo_Year]
        strNewFile_Base = rs![File_Base]
    'End If
     
     
    Else 'Field1 changed - Write the record to other table.
    'Create Insert SQL.
     
    strSQL = "INSERT INTO Photo_Test3 (CoordID, Photo_Year, File_Base, Photo_Year_2, File_Base_2) "
     
    strSQL = strSQL & "VALUES (intNewCoordID, intNewPhoto_Year, strNewFile_Base, intPhoto_Year_2, strFile_Base_2);"
    'Execute Insert SQL
    DoCmd.RunSQL strSQL
     
    'Populate current row values into variables.
        intCoordID = rs![CoordID]
        intUnique_ID = rs![Unique_ID]
        intPhoto_Year = rs![Photo_Year]
        strFile_Base = rs![File_Base]
        
     
    End If 'End If strNewField1 = strField1 Then
    End If 'End If intRecordCount = 1
     
        intPrevCoordID = rs![CoordID]
        intPrevUnique_ID = rs![Unique_ID]
        intPrevPhoto_Year = rs![Photo_Year]
        strPrevFile_Base = rs![File_Base]
     
    .MoveNext 'Move to next record in recordset.
     
    Loop 'Back to 'Do While' to check if we are at the end of the file.
    'Create SQL for Last Row of data that is still stored even though Access found the EOF.
     
    strSQL = "INSERT INTO Photo_Test3 (CoordID, Photo_Year, File_Base, Photo_Year_2, File_Base_2) "
     
    strSQL = strSQL & "VALUES (intCoordID, intPhoto_Year, strFile_Base, intPhoto_Year_2, strFile_Base_2); "
    'Execute Insert SQL.
     
    DoCmd.RunSQL strSQL
     
    Exit_Get_DB_Values:
    If Not rs Is Nothing Then
    rs.Close
    Set rs = Nothing
    End If
    Set db = Nothing
    Exit Function
    'Error_Handle:
    Resume Exit_Get_DB_Values
    End With
    End Function

  14. #14
    admessing's Avatar
    admessing is offline GIS DBase Tamer
    Windows XP Access 2007
    Join Date
    Dec 2011
    Location
    Northern CO
    Posts
    79
    Okay...due to time constraints and the slim likelyhood of ever having more than 2 matching coordinate values, I have opted to just use a simple SQL statement to join my records. I will still be working on the VBA in the event that we may encounter more than 2 records with matching coordinate values in the future.

    For now, this issue is resolved. Stay tuned...

  15. #15
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by admessing View Post
    Here is the SQL for the creat table:

    Code:
    CREATE TABLE Photo_Test (CoordID AUTOINCREMENT, Proc_Region Double, Proc_Forest Double, FSVeg_Location Text, FSVeg_Stand_Num Text, UTM_Easting Double, UTM_Northing Double, UTM_Zone Double, UTM_Datum Text, LAT_DD Double, LON_DD Double, LAT_LON_Datum Text, Photo_Year_1 Double, North_1 Text, East_1 Text, South_1 Text, West_1 Text, Photo_Year_2 Double, North_2 Text, East_2 Text, South_2 Text, West_2 Text, PRIMARY KEY (CoordID));
    If you decide to go with the smaller of the two tables that I posted earlier, just take out the North*, East*, South*, West* , Lat/Lon*, UTM, Proc*, FSVeg* fields. Then just add <File_Base_1 text, File_Base_2 text> in their place. Just make sure that the Photo_Year* comes before each of the File_Base* fields.

    One more thing....since the data is coming from another table, the CoordID field in Photo_Test can just be a Double if needed....that way it preserves the original CoordID data.

    Here is the code I came up with. There were a lot of changes.... both the code and the table "Photo_Test". (but it was fun!!!)
    After you link the Excel spreadsheet, execute the code in the module. Attached is the A2K mdb with the table "Photo_Test". (I have been trying for two days to get this uploaded. )


    Code:
    Option Compare Database
    Option Explicit
    
    
    Function Get_DB_Values()
    'Get values from a table using a query in VBA.
    'Process values row by row.
    'Insert processed row into another Table.
    
    'On Error GoTo Error_Handle
    
       Dim db As DAO.Database
       Dim rs As DAO.Recordset
    
       Dim RC As Long   ' number of records in recordset "r"
    
       Dim lngCoordID As Long
       Dim strProc_Region As String
       Dim strProc_Forest As String
       Dim strFSVeg_Location As String
       Dim strFSVeg_Stand_Num As String
       Dim dblUTM_Easting As Double
       Dim dblUTM_Northing As Double
       Dim lngUTM_Zone As Long
       Dim strUTM_Datum As String
       Dim dblLAT_DD As Double
       Dim dblLON_DD As Double
       Dim strLAT_LON_Datum As String
       Dim intPhoto_Year As Integer
       Dim strNorth As String
       Dim strEast As String
       Dim strSouth As String
       Dim strWest As String
    
       Dim lngNewCoordID As Long
       Dim intPrevCoordID As Integer
    
       Dim intRecordCount As Integer
    
       Dim intNewPhoto_Year As Integer
       Dim strNewNorth As String
       Dim strNewEast As String
       Dim strNewSouth As String
       Dim strNewWest As String
       Dim strSQL As String
    
       'initalize variables
       Set db = CurrentDb
       intRecordCount = 0
    
       'I don't like deleting and recreating tables. create it once - delect all records to clear table.
       '   CurrentDb.Execute "DROP TABLE Photo_Test"   'This is to delete the table from previous runs of the code
       '   CurrentDb.Execute "Create_Photo_Test"   'This is the create table query where the data needs to end up
    
       db.Execute "DELETE * FROM Photo_Test"   'This is to clear the table from previous runs of the code
    
       'open record set
       strSQL = "Select * From Link2 ORDER BY CoordID, Photo_Year "
    
       Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
       rs.MoveLast   ' to fully populate the recordset
       rs.MoveFirst
       RC = rs.RecordCount
       With rs
    
          'This Do While loop goes through all the records in strSQL.
          Do While Not rs.EOF
    
    
    
             If intRecordCount < 1 Then
                lngCoordID = ![CoordID]
                strProc_Region = ![Proc_Region]
                strProc_Forest = ![Proc_Forest]
                strFSVeg_Location = ![FSVeg_Location]
                strFSVeg_Stand_Num = ![FSVeg_Stand_Num]
                dblUTM_Easting = ![UTM_Easting]
                dblUTM_Northing = ![UTM_Northing]
                lngUTM_Zone = ![UTM_Zone]
                strUTM_Datum = ![UTM_Datum]
                dblLAT_DD = ![LAT_DD]
                dblLON_DD = ![LON_DD]
                strLAT_LON_Datum = ![LAT_LON_Datum]
                intPhoto_Year = ![Photo_Year]
                strNorth = ![North]
                strEast = ![East]
                strSouth = ![South]
                strWest = ![West]
    
             Else   'Not first record.
    
                lngNewCoordID = ![CoordID]
    
                If lngNewCoordID = intPrevCoordID Then   'Same CoordID - concatenate values.
    
                   'This is where the FUN begins.....Yeah Right
                   intNewPhoto_Year = ![Photo_Year]
                   strNewNorth = ![North]
                   strNewEast = ![East]
                   strNewSouth = ![South]
                   strNewWest = ![West]
    
                Else   'CoordID changed - Write the record to other table.
                   'Create Insert SQL string.
                   strSQL = "INSERT INTO Photo_Test (CoordID, Proc_Region, Proc_Forest, FSVeg_Location,"
                   strSQL = strSQL & " FSVeg_Stand_Num, UTM_Easting, UTM_Northing, UTM_Zone, UTM_Datum,"
                   strSQL = strSQL & " LAT_DD, LON_DD, LAT_LON_Datum, Photo_Year_1, North_1, East_1,"
                   strSQL = strSQL & " South_1, West_1, Photo_Year_2, North_2, East_2, South_2, West_2)"
                   strSQL = strSQL & " VALUES ( " & lngCoordID & ", '"
                   strSQL = strSQL & strProc_Region & "', '" & strProc_Forest & "', '"
                   strSQL = strSQL & strFSVeg_Location & "', '" & strFSVeg_Stand_Num & "', "
                   strSQL = strSQL & dblUTM_Easting & ", " & dblUTM_Northing & ", "
                   strSQL = strSQL & lngUTM_Zone & ", '" & strUTM_Datum & "', "
                   strSQL = strSQL & dblLAT_DD & ", " & dblLON_DD & ", '"
                   strSQL = strSQL & strLAT_LON_Datum & "', "
                   strSQL = strSQL & intPhoto_Year & ", '" & strNorth & "', '"
                   strSQL = strSQL & strEast & "', '" & strSouth & "', '"
                   strSQL = strSQL & strWest & "', " & intNewPhoto_Year & ", '"
                   strSQL = strSQL & strNewNorth & "', '" & strNewEast & "', '"
                   strSQL = strSQL & strNewSouth & "', '" & strNewWest & "'); "
    
                   '               Debug.Print strSQL
    
                   'Execute Insert SQL string
                   db.Execute strSQL, dbFailOnError
    
                   'Populate current (new) row values into variables.
                   lngCoordID = ![CoordID]
                   strProc_Region = ![Proc_Region]
                   strProc_Forest = ![Proc_Forest]
                   strFSVeg_Location = ![FSVeg_Location]
                   strFSVeg_Stand_Num = ![FSVeg_Stand_Num]
                   dblUTM_Easting = ![UTM_Easting]
                   dblUTM_Northing = ![UTM_Northing]
                   lngUTM_Zone = ![UTM_Zone]
                   strUTM_Datum = ![UTM_Datum]
                   dblLAT_DD = ![LAT_DD]
                   dblLON_DD = ![LON_DD]
                   strLAT_LON_Datum = ![LAT_LON_Datum]
                   intPhoto_Year = ![Photo_Year]
                   strNorth = ![North]
                   strEast = ![East]
                   strSouth = ![South]
                   strWest = ![West]
    
                   'clear variables
                   intNewPhoto_Year = Empty
                   strNewNorth = Empty
                   strNewEast = Empty
                   strNewSouth = Empty
                   strNewWest = Empty
    
                End If   'End If lngNewCoordID = intPrevCoordID Then
             End If   'End If intRecordCount < 1
    
    
             intPrevCoordID = lngCoordID
    
             If Not .EOF Then
                intRecordCount = intRecordCount + 1
    
                .MoveNext   'Move to next record in recordset.
             End If
          Loop   'Back to 'Do While' to check if we are at the end of the file.
    
          'Create SQL for Last Row of data that is still stored even though Access found the EOF.
          strSQL = "INSERT INTO Photo_Test (CoordID, Proc_Region, Proc_Forest, FSVeg_Location,"
          strSQL = strSQL & " FSVeg_Stand_Num, UTM_Easting, UTM_Northing, UTM_Zone, UTM_Datum,"
          strSQL = strSQL & " LAT_DD, LON_DD, LAT_LON_Datum, Photo_Year_1, North_1, East_1,"
          strSQL = strSQL & " South_1, West_1, Photo_Year_2, North_2, East_2, South_2, West_2)"
          strSQL = strSQL & " VALUES ( " & lngCoordID & ", '"
          strSQL = strSQL & strProc_Region & "', '" & strProc_Forest & "', '"
          strSQL = strSQL & strFSVeg_Location & "', '" & strFSVeg_Stand_Num & "', "
          strSQL = strSQL & dblUTM_Easting & ", " & dblUTM_Northing & ", "
          strSQL = strSQL & lngUTM_Zone & ", '" & strUTM_Datum & "', "
          strSQL = strSQL & dblLAT_DD & ", " & dblLON_DD & ", '"
          strSQL = strSQL & strLAT_LON_Datum & "', "
          strSQL = strSQL & intPhoto_Year & ", '" & strNorth & "', '"
          strSQL = strSQL & strEast & "', '" & strSouth & "', '"
          strSQL = strSQL & strWest & "', " & intNewPhoto_Year & ", '"
          strSQL = strSQL & strNewNorth & "', '" & strNewEast & "', '"
          strSQL = strSQL & strNewSouth & "', '" & strNewWest & "'); "
    
          '               Debug.Print strSQL
    
          'Execute Insert SQL.
          db.Execute strSQL, dbFailOnError
       End With
    
    Exit_Get_DB_Values:
       If Not rs Is Nothing Then
          rs.Close
          Set rs = Nothing
       End If
       Set db = Nothing
       MsgBox "DONE!!"
       Exit Function
       'Error_Handle:
       Resume Exit_Get_DB_Values
    End Function
    Last edited by ssanfu; 03-01-2012 at 07:16 PM. Reason: spelling

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

Similar Threads

  1. Loop through records
    By sam10 in forum Programming
    Replies: 12
    Last Post: 07-07-2011, 02:30 PM
  2. Combine master detail records in one query
    By mariost in forum Queries
    Replies: 4
    Last Post: 05-14-2011, 11:25 AM
  3. Replies: 8
    Last Post: 01-21-2011, 10:28 AM
  4. Loop through subform records
    By asmith in forum Forms
    Replies: 6
    Last Post: 10-06-2010, 10:31 AM
  5. Loop through Records and Make ID
    By rob4465 in forum Programming
    Replies: 3
    Last Post: 01-14-2010, 10:46 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