有没有理由在VBA中使用ENUM?

时间:2009-04-24 14:19:27

标签: vba ms-access access-vba

我尝试在Access 2007的代码中使用ENUMS。但它非常不舒服。使用馆藏可能会更好吗?

我的代码无法正常工作,我觉得它会很有用。

Public Function 

    GetEnumId(Name As String, ReferenceTable As String) As Long
        Dim rs As DAO.RecordSet

        Set rs = CurrentDb.OpenRecordset("Select * From " & ReferenceTable & _
                    " Where Name= '" & Name & "'")
        GetEnumId = rs("ID")

        rs.Close

    End Function

    ' Doesn't work because of wrong type params
    Public Function GetEnumName(ID As Long, ReferenceTable As String) As String
        Dim rs As DAO.RecordSet

        Set rs = CurrentDb.OpenRecordset("Select * From " & ReferenceTable & _
                    " Where ID= '" & ID & "'")
        GetEnumName = rs("Name")

        rs.Close

    End Function

1 个答案:

答案 0 :(得分:1)

ID不是号码吗?然后不要用引号括起来:

Set rs = CurrentDb.OpenRecordset("Select * From " & ReferenceTable & _
            " Where ID= " & ID )