mercredi 27 juin 2018

VBA - Nested "With Statements" within "IF Statements"

Language: VBA - MS Access

I am using User-Defined-Types (UDT) within my code. I would like to be able determine which section of the UDT i'm loading data into based on a state variable. My first attempt was to use "With" statements nested into an "IF" statement. This doesn't work (I get a compiler error that says Else without if). Is there a way to make this work? or another way of going about using a state variable to determine which section of the UDT i'm loading?

Type MyOtherType
    Name as String
    Age as Integer    
End Type

Type MyType
    aMyOtherType() as MyOtherType
    X as Integer
    Y as Integer
    Z as Integer  
End Type

Sub QuestionableCode()
Dim UDT(0 To 0) as MyType
Dim State as String
ReDim Preserve UDT(0).X(0 to 0) as MyOtherType
ReDim Preserve UDT(0).Y(0 to 0) as MyOtherType
ReDim Preserve UDT(0).Z(0 to 0) as MyOtherType

    State = "B"

    If State = "A" Then
        With UDT(0).X(0)
    ElseIf State = "B" Then
        With UDT(0).Y(0)
    Else 
        With UDT(0).Z(0)
    End If
            .Name = "George"
            .Age = 30
        End With
End Sub

Aucun commentaire:

Enregistrer un commentaire