Question : I need to adapt VB script to run on SQL Server

Having successfully developed VB script to create a Results table (thanks to EE assistance), it was determined it needs to be adapted to run in SQL.  The VB script is as follows:

 
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:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
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


The hope is to create the "Tmp_Group_Results" table on the SQL Server, pulling data directly from the source ("dbo_Source"). The source table is HUGE (millions of records). Additionally, to lessen the total number of records in the results table, I have been asked to establish a MIN and MAX on the YearMonth field, grouping on the other fields. The fields being as follows:

ContractNumber
YearMonth
MemberNumber
LastName
FirstName
MI
DOB
Gender
SSN
Status
FromDate
ToDate

While I am familiar with SQL, I am not an expert by any stretch.  Any help I can get would be greatly appreciated.

Answer : I need to adapt VB script to run on SQL Server

I should mention, this assumes the Server Service is set to Automatic and started.
Random Solutions  
 
programming4us programming4us