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 intField1, intField2, intField4, intField5 As Integer
Dim strField3, strField6, strField7, strField8, strField9 As String
Dim intPrevField1, intPrevField2, intPrevField4, intPrevField5 As Integer
Dim strPrevField3, strPrevField6, strPrevField7, strPrevField8, strPrevField9 As String
Dim intNewField1, intNewField2, intNewField4, intNewField5 As Integer
Dim strNewField3, strNewField6, strNewField7, strNewField8, strNewField9 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 [Photo_Link] ORDER BY [Easting_UTM]"
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
intField1 = rs![Easting_UTM]
intField2 = rs![Northing_UTM]
strField3 = rs![FSVeg_Loc]
intField4 = rs![Stand_Num]
intField5 = rs![Photo_Year]
strField6 = rs![IMG_North]
strField7 = rs![IMG_East]
strField8 = rs![IMG_South]
strField9 = rs![IMG_West]
intRecordCount = intRecordCount + 1
Else 'Not first record.
intNewField1 = rs![Easting_UTM]
If intNewField1 = intPrevField1 And intNewField2 = intPrevField2 Then 'Same Field1 - concatenate values.
strNewField3 = rs![FSVeg_Loc]
intNewField4 = rs![Stand_Num]
intNewField5 = rs![Photo_Year]
If strNewField5 <> strPrevField5 Then
strField5 = strField10
End If
If strNewField6 <> strPrevField6 Then
strNewField6 = strField11
End If
If strNewField7 <> strPrevField7 Then
strField7 = strField12
End If
If strNewField8 <> strPrevField8 Then
strField8 = strField13
End If
If strNewField9 <> strPrevField9 Then
strField9 = strField14
End If
Else 'Field1 changed - Write the record to other table.
'Create Insert SQL.
strSQL = "INSERT INTO Photo_Link_Combined (Easting_UTM, Northing_UTM, FSVeg_Loc, Stand_Num, Photo_Year, IMG_North, IMG_East, IMG_South, IMG_West, Photo_Year2, IMG_North2, IMG_East2, IMG_South2, IMG_West2) "
strSQL = strSQL & "VALUES (" & "'" & intNewField1 & "'" & ", " & "'" & intField2 & "'" & ", " & "'" & strField3 & "'" & ", " & "'" & intField4 & "'" & ", " & "'" & intNewField5 & "'" & ", " & "'" & strField6 & "'" & ", " & "'" & strField7 & "'" & ", " & "'" & strField8 & "'" & ", " & "'" & strField9 & "'" & ", " & "'" & intNewField10 & "'" & ", " & "'" & strField11 & "'" & ", " & "'" & strField12 & "'" & ", " & "'" & strField13 & "'" & ", " '" & strNewField14 & "'" & "); "
'Execute Insert SQL
DoCmd.RunSQL strSQL
'Populate current row values into variables.
intField1 = rs![Easting_UTM]
intField2 = rs![Northing_UTM]
strField3 = rs![FSVeg_Loc]
intField4 = rs![Stand_Num]
intField5 = rs![Photo_Year]
strField6 = rs![IMG_North]
strField7 = rs![IMG_East]
strField8 = rs![IMG_South]
strField9 = rs![IMG_West]
End If 'End If strNewField1 = strField1 Then
End If 'End If intRecordCount = 1
intPrevField1 = intNewField1
intPrevField2 = intNewField2
strPrevField3 = strNewField3
intPrevField4 = intNewField4
intPrevField5 = intNewField5
strPrevField6 = strNewField6
strPrevField7 = strNewField7
strPrevField8 = strNewField8
strPrevField9 = strNewField9
.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_Link_Combined (Easting_UTM, Northing_UTM, FSVeg_Loc, Stand_Num, Photo_Year, IMG_North, IMG_East, IMG_South, IMG_West) "
strSQL = strSQL & "VALUES (" & "'" & intField1 & "'" & ", " & "'" & intField2 & "'" & ", " & "'" & strField3 & "'" & ", " & "'" & intField4 & "'" & ", " & "'" & intField5 & "'" & ", " & "'" & strField6 & "'" & ", " & "'" & strField7 & "'" & ", " & "'" & strField8 & "'" & ", " & "'" & strField9 & "'" & "); "
'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