Question : Access VBA If Elseif Syntax Error

Hello EE,

I'm receiving a syntax error on the module below.  I'm using Access 2007 in compatability mode.  The purpose of the module is to convert different text fields into a consistent MMDDYYYY string format (for import into SAP.)

Could you please take a look at the code below and let me know what syntax I'm using incorrectly.  I get a syntax error on the second ElseIf.

Thanks,
LVBarnes
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
Public Function ConvertStrDate(ByVal dt As Variant) As String

  Dim datDate As String

  If IsNull(dt) Then
    dt = Null
  ElseIf IsDate(dt) Then
      datDate = Format(dt, "mmddyyyy")
  ElseIf Len(dt) = 8 And Left(dt,4) In ("2008","2009","2010","2011","2012") Then '20091225  SYNTAX ERROR HERE
      datDate = Format(DateSerial(Left(dt, 4), Mid(dt, 5, 2), Mid(dt, 7, 2)), "mmddyyyy")
  ElseIf Len(dt) = 8 And Right(Dt,4) In ("2008","2009","2010","2011","2012") Then '12252009
      datDate = Format(DateSerial(Right(dt, 4), Mid(dt, 1, 2), Mid(dt, 3, 2)), "mmddyyyy")
  ElseIf Len(dt) = 5 Isdate(Cdate(dt)) = True Then 'Excel date in number format
      datDate = Format(CDate(dt), "mmddyyyy")
  ElseIf Len(dt) = 7 Then 'a mmddyyyy date with the leading zero omitted
      dt = "0" & dt
        If Left(dt,4) In ("2008","2009","2010","2011","2012") Then
            datDate = Format(DateSerial(Right(dt, 4), Mid(dt, 1, 2), Mid(dt, 3, 2)), "mmddyyyy")
        End If
  Else
        datDate = "N/A"
  End If

  ConvertStrDate = datDate

End Function

Answer : Access VBA If Elseif Syntax Error

Well, unfortunately you can't use the IN() operator here ... so you need to go with a different approach.

mx
Random Solutions  
 
programming4us programming4us