Below is the code I am using:
Dim safe10_comp As Integer
Dim ref_date As Date
Dim dr As DataRow
Dim dt As DataTable = New DataTable()
Dim da As OleDb.OleDbDataAdapter = New OleDbDataAdapter()
Dim Conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString2").ConnectionString)
Dim sql As String = "with a as (select t.employee_number, location_code, title_code, max(t.start_date) as reference_date from registration_style_view t where t.course_code = 'SAFE-10' and t.status = 'A' and t.registration_status in ('M','F') and title_code in ('MX42','MX02','SU54','TL09','SS12','MX49','MX59','SR21','AS15','631A','197A','SS25','MX01','MX41','SU62','MX54','MX14','MX12','MX13','M411','MX52','MX60','MX11','MX16','MX51','MX53','MX56','LD55','SI66') group by t.employee_number, location_code, title_code union select t.employee_number, location_code, title_code, max(t.start_date) as reference_date from registration_style_view t where t.course_code = 'SAFE10' and t.status = 'A' and t.registration_status in ('M','F') and title_code in ('MX42','MX02','SU54','TL09','SS12','MX49','MX59','SR21','AS15','631A','197A','SS25','MX01','MX41','SU62','MX54','MX14','MX12','MX13','M411','MX52','MX60','MX11','MX16','MX51','MX53','MX56','LD55','SI66') group by t.employee_number, location_code, title_code union select t.employee_number, location_code, title_code, max(t.status_date) as reference_date from registration_style_view t where t.course_code like 'SAFE-10R%' and t.status = 'A' and t.registration_status in ('M','F') and title_code in ('MX42','MX02','SU54','TL09','SS12','MX49','MX59','SR21','AS15','631A','197A','SS25','MX01','MX41','SU62','MX54','MX14','MX12','MX13','M411','MX52','MX60','MX11','MX16','MX51','MX53','MX56','LD55','SI66') group by t.employee_number, location_code, title_code union select t.employee_number, location_code, title_code, max(t.status_date) as reference_date from registration_style_view t where t.course_code like 'SAFE10R%' and t.status = 'A' and t.registration_status in ('M','F') and title_code in ('MX42','MX02','SU54','TL09','SS12','MX49','MX59','SR21','AS15','631A','197A','SS25','MX01','MX41','SU62','MX54','MX14','MX12','MX13','M411','MX52','MX60','MX11','MX16','MX51','MX53','MX56','LD55','SI66') group by t.employee_number, location_code, title_code union select t.employee_number, location_code, title_code, max(t.status_date) as reference_date from registration_style_view t where t.course_code like 'SAFE10S%' and t.status = 'A' and t.registration_status in ('M','F') and title_code in ('MX42','MX02','SU54','TL09','SS12','MX49','MX59','SR21','AS15','631A','197A','SS25','MX01','MX41','SU62','MX54','MX14','MX12','MX13','M411','MX52','MX60','MX11','MX16','MX51','MX53','MX56','LD55','SI66') group by t.employee_number, location_code, title_code) select p.employee_number, p.location_code, p.title_code, max(to_date(a.reference_date)) as reference_date from person p left outer join a on p.employee_number = a.employee_number where p.department = 'MAINTENANCE' and p.title_code in ('MX42','MX02','SU54','TL09','SS12','MX49','MX59','SR21','AS15','631A','197A','SS25','MX01','MX41','SU62','MX54','MX14','MX12','MX13','M411','MX52','MX60','MX11','MX16','MX51','MX53','MX56','LD55','SI66') and p.status = 'A' group by p.employee_number, p.location_code, p.title_code order by location_code"
Conn.Open()
Dim selectCMD As New OleDbCommand(sql, Conn)
da.SelectCommand = selectCMD
da.Fill(dt)
safe10_comp = dt.Rows.Count