1

我有一个案例,我试图从描述字段中提取状态值。但是,我需要根据状态更改一些列。所以这就是我所拥有的

说明 示例:管理 EXECUTIVE CTJACKSONVILLE.FL.32216-4041

Function getState(description As String) As String
    Dim s() As String
    s = Split(description, ".")
    getState = s(UBound(s) - 1)
End Function

我想根据它是否返回正确的州缩写来更改某些列。如果该值未返回有效状态,我想标记发票。所以像这样的事情......

UPDATE tblInvoices
SET [Invoice Flag] = True
Where getState(description) <> ValidStates.Abbreviation

类似的东西。对不起新手。任何帮助表示赞赏!

4

1 回答 1

2

我将创建一个名为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
于 2013-06-27T13:18:27.533 回答