我将创建一个名为isValidState()
返回布尔值的函数
Function isValidState(st As String) As Boolean
Select Case st
Case "AL", "FL", "NY" ...
isValidState = True
Case Else
isValidState = False
End Select
End Function
另一种方法可能是有一个包含所有状态代码的表(让我们称之为MyStateTable
)
Function isValidState(st As String) As Boolean
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("MyStateTable")
rs.FindFirst ("StateFieldName = '" & st & "'")
If rs.NoMatch Then
isValidState = False
Else
isValidState = True
End If
rs.Close
End Function
所以你的查询看起来像
UPDATE tblInvoices
SET [Invoice Flag] = True
Where isValidState(getState(description)) = True
编辑:
Function getState(description As String) As String
Dim s() As String
s = Split(description, ".")
If (UBound(s) > 1) then
getState = s(UBound(s) - 1)
Else
getState = vbNullString 'Or change this to "" or something else that makes sense for your usage'
End if
End Function