I am attempting populate a field (String) in a table based on the value of the last character in a string of another field in the same table.
The field I want to populate is called PhotoCaption, the field that contains the string whose last character it to be tested is called FileName.
The format of FileName is XXXXX-X. eg 40150-1,40150-2 etc.
I have written code that will do most of this, except I am unsure how to test what that last character is. (I think it has something to do with Mid or Len but ....
Could someone look at my code and suggest how I accomplish this.
I am new to VBA and programming in general so if you notice other faults I am open and thankful for all suggestions.
All help appreciated.
DJ
Public Sub PopulateField()
On Error GoTo ErrorHandler
Dim rst As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim intFileNum As Integer
Dim lngCount As Long
Dim AndFile As String
'Update tblPhoto.PhotoCaption based on FileName
Set cnn = CurrentProject.Connection
rst.Open "tblPhotos", cnn, , , acCmdTable
rst.MoveFirst
With rst
Do While Not .EOF
'Populate based on last character in FileName
'If last character =1 then PhotoCaption is Looking North
If rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Looking North"
'If last character =2 then PhotoCaption is Looking East
ElseIf rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Looking East"
'If last character =3 then PhotoCaption is Looking South
ElseIf rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Looking South"
'If last character =4 then PhotoCaption is Looking West
ElseIf rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Looking West"
'If last character =5-9 then PhotoCaption is Other
ElseIf rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Other"
End If
'Goto next record
.MoveNext
Loop
End With
'Close the Recordset and Connection Objects
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
ErrorHandler:
Set rst = Nothing
Set cnn = Nothing
Err.Clear
End Sub
The field I want to populate is called PhotoCaption, the field that contains the string whose last character it to be tested is called FileName.
The format of FileName is XXXXX-X. eg 40150-1,40150-2 etc.
I have written code that will do most of this, except I am unsure how to test what that last character is. (I think it has something to do with Mid or Len but ....
Could someone look at my code and suggest how I accomplish this.
I am new to VBA and programming in general so if you notice other faults I am open and thankful for all suggestions.
All help appreciated.
DJ
Public Sub PopulateField()
On Error GoTo ErrorHandler
Dim rst As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim intFileNum As Integer
Dim lngCount As Long
Dim AndFile As String
'Update tblPhoto.PhotoCaption based on FileName
Set cnn = CurrentProject.Connection
rst.Open "tblPhotos", cnn, , , acCmdTable
rst.MoveFirst
With rst
Do While Not .EOF
'Populate based on last character in FileName
'If last character =1 then PhotoCaption is Looking North
If rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Looking North"
'If last character =2 then PhotoCaption is Looking East
ElseIf rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Looking East"
'If last character =3 then PhotoCaption is Looking South
ElseIf rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Looking South"
'If last character =4 then PhotoCaption is Looking West
ElseIf rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Looking West"
'If last character =5-9 then PhotoCaption is Other
ElseIf rst!Fields("FileName") Then _
rst!Fields("PhotoCaption") = "Other"
End If
'Goto next record
.MoveNext
Loop
End With
'Close the Recordset and Connection Objects
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
ErrorHandler:
Set rst = Nothing
Set cnn = Nothing
Err.Clear
End Sub