"End If without Block if" error

scubadiver007

Registered User.
Local time
Today, 03:35
Joined
Nov 30, 2010
Messages
317
I want to check whether data has already imported and if not, run a different macro depending on the quarter prefix (currently substituted as a message box to check).

Dim DB As DAO.Database, MyRec As DAO.Recordset
Set DB = CurrentDb
Set MyRec = DB.OpenRecordset("Select * From tble_activity Where quartercode = '" & Me.Quarter_ID & "'")
If MyRec.EOF Then
If Left([Forms]![menu]![Quarter_ID], 2) = "Q1" Then MsgBox "Q1"
ElseIf Left([Forms]![menu]![Quarter_ID], 2) = "Q2" Then MsgBox "Q2"
ElseIf Left([Forms]![menu]![Quarter_ID], 2) = "Q3" Then MsgBox "Q3"
ElseIf Left([Forms]![menu]![Quarter_ID], 2) = "Q4" Then MsgBox "Q4"
End If
End If
 
It's because you have the msgbox "" on the same line as the If ... Then

Code:
Dim DB As DAO.Database, MyRec As DAO.Recordset
Set DB = CurrentDb
Set MyRec = DB.OpenRecordset("Select * From tble_activity Where quartercode = '" & Me.Quarter_ID & "'")
If MyRec.EOF Then
  If Left([Forms]![menu]![Quarter_ID], 2) = "Q1" Then 
    MsgBox "Q1"
  ElseIf Left([Forms]![menu]![Quarter_ID], 2) = "Q2" Then 
    MsgBox "Q2"
  ElseIf Left([Forms]![menu]![Quarter_ID], 2) = "Q3" Then 
    MsgBox "Q3"
  ElseIf Left([Forms]![menu]![Quarter_ID], 2) = "Q4" Then 
    MsgBox "Q4"
  End If
End If
Should work though a SELECT Case would be clearer and more efficient as you only need to test your case once;

Code:
Select Case Left([Forms]![menu]![Quarter_ID], 2)
  case "Q1"
    msgbox "q1"
  case "Q2"
    msgbox "q2"
  case "Q3"
    msgbox "q3"
  case "Q4"
    msgbox "q4"
  Case else ' catch all just in case you get something unexpected
    msgbox "eeep something we didn't expect"
end Select
 
Thanks for that.

Your first code works now but how would I incorporate the second code because I still need to check whether data is already in the table.
 

Users who are viewing this thread

Back
Top Bottom