Sunday, May 1, 2011

Is there any reason to use ENUM's in VBA ?

I have tried to use ENUMS in my code for Access 2007. But it's very uncomfortable. May be it's better to use collections ?

My code isn't working is such way I thought it would be useful.

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
From stackoverflow
  • Isn't ID a number? Then don't wrap it in quotes:

    Set rs = CurrentDb.OpenRecordset("Select * From " & ReferenceTable & _
                " Where ID= " & ID )
    
    Konoplianko : Thank you. I thought ID cannot be passed as long, only by enum type.
    Oorang : Enums are Longs, they are totally interchangeable:)

0 comments:

Post a Comment