Public Function GroupRecords()
Dim SQL As String
Dim StartDate As String
Dim rs As Recordset
Dim rs2 As Recordset
StartDate = InputBox("Please enter the year and month to be used in determining the member records." & vbCrLf & vbCrLf & "(Use the YYYYMM date format.)", "Start Date")
DoCmd.SetWarnings False
For i = 0 To CurrentDb.TableDefs.Count - 1
If CurrentDb.TableDefs(i).Name = "Tmp_Group_Recordset" Then
DoCmd.DeleteObject acTable, "Tmp_Group_Recordset"
Exit For
End If
Next
For i = 0 To CurrentDb.TableDefs.Count - 1
If CurrentDb.TableDefs(i).Name = "Tmp_Group_Results" Then
DoCmd.DeleteObject acTable, "Tmp_Group_Results"
Exit For
End If
Next
SQL = "SELECT dbo_Source.* INTO Tmp_Group_Recordset "
SQL = SQL & "FROM dbo_Source "
SQL = SQL & "WHERE (((dbo_Source.YearMonth) = '" & StartDate & "')) "
SQL = SQL & "ORDER BY YearMonth;"
DoCmd.RunSQL SQL
SQL = "CREATE TABLE Tmp_Group_Results(ContractNumber varchar(5) Null, YearMonth varchar(6) Null, MemberNumber varchar(12) Null, "
SQL = SQL & "LastName varchar(25) Null, FirstName varchar(25) Null, MI varchar(1) Null, "
SQL = SQL & "DOB Datetime Null, Gender integer Null, SSN varchar(9) Null, Status varchar(25) Null);"
DoCmd.RunSQL SQL
Set rs = CurrentDb.OpenRecordset("Tmp_Group_Recordset")
Set rs2 = CurrentDb.OpenRecordset("Tmp_Group_Results")
rs.MoveFirst
Do Until rs.EOF
For j = 13 To rs.Fields.Count - 1
If rs(j) = -1 And rs(5) = StartDate Then
With rs2
.AddNew
!ContractNumber = rs(3)
!YearMonth = rs(5)
!MemberNumber = rs(6)
!LastName = rs(7)
!FirstName = rs(8)
!MI = rs(9)
!DOB = rs(10)
!Gender = rs(11)
!SSN = rs(12)
!Status = rs(j).Name
.Update
End With
End If
Next
rs.MoveNext
Loop
rs.Close
rs2.Close
Set rs = Nothing
Set rs2 = Nothing
DoCmd.SetWarnings True
End Function
|