我有五个停止
-------- -------------甲乙------------ -----------Ç -D ------------ë
我在原始表[Trip]中有三列,我想根据逻辑
编写方向如果我的车从A驶向E,或者朝那个方向停靠(B-D,C-E),方向是南
如果我的车从E驶向A,或朝那个方向停靠(E-C,D-A),方向为北
Trip CarID Stops
1 1000 A
1 1000 B
1 1000 C
2 1001 C
2 1001 D
2 1001 E
3 1002 D
3 1002 C
3 1002 B
我想要下面的结果:
Trip CarID Stops Direction
1 1000 A South
1 1000 B South
1 1000 C South
2 1001 C South
2 1001 D South
2 1001 E South
3 1002 D North
3 1002 C North
3 1002 B North
任何人都知道如何在访问vba或查询中对此进行编码?真正的桌子有超过5站。
答案 0 :(得分:1)
我想这会给出一些想法
Private Sub Command0_Click()
On Error Resume Next
Dim rst As DAO.Recordset
Dim PreviousStop As String
Dim Direction As String
Dim PreviousDirection As String
Dim rstCounter As Integer
Dim currentCar As String
Set rst = CurrentDb.OpenRecordset("Trip")
With rst
rstCounter = rst.RecordCount
.MoveFirst
Do Until .EOF
If rstCounter = 1 Then
Direction = calculatedDirection(PreviousStop, .Fields("Stops"))
.Edit
.Fields("Direction") = Direction
.Update
Exit Do
Else
PreviousStop = .Fields("Stops")
currentCar = .Fields("CarID")
.MoveNext
If currentCar <> .Fields("CarID") Then
PreviousDirection = ""
Else
If Len(PreviousDirection) > 0 Then
Direction = PreviousDirection
Else
Direction = calculatedDirection(PreviousStop, .Fields("Stops"))
PreviousDirection = Direction
End If
End If
.MovePrevious
If Len(Direction) > 0 Then
.Edit
.Fields("Direction") = Direction
.Update
End If
.MoveNext
End If
rstCounter = rstCounter - 1
Loop
End With
End Sub
Private Function calculatedDirection(PreviousStop As String, CurrentStop As String) As String
If PreviousStop > CurrentStop Then
calculatedDirection = "North"
Else
calculatedDirection = "South"
End If
End Function