Thanks for the response Jon.
Here is the SQL routine I am trying to run in WORD.
As mentioned earlier, this same Code runs in Access and is used to drive a graph using the MSGRAPH control.
In Access, the code runs the SELECT 6 times to figure out the number of support calls opened, closed and outstanding at the end of each of 6 months. The is a simple form for the operator to choose a customer from a combo as well as a month (1-12) and year.
This whole thing orks great in Access and I simply cut and pasted the code into WORD.
Here is the code:
'******************************************************************************
'REQUIRES REFERENCE:
'MICROSOFT ACTIVE X DATA OBJECTS LIBRARY 2.5
'MICROSOFT CHART CONTROL 6.0 (SP4) (OLEDB)
'******************************************************************************
'Option Explicit
Dim adoConn As Connection
Dim dbs As Recordset
Dim Rs As Recordset
Dim rst As Recordset, rstCalls As Recordset
Dim strSQL As String
Dim dteBegDate As Date, dteEndDate As Date
Dim BegMth As Integer, EndMth As Integer
Dim CusID As String
Dim i As Integer, intNumberOfMonths As Integer 'number of months within in the selected parameters
Dim CallsOpenedCount As Integer
Dim CallsClosedCount As Integer
Dim CallsInProgCount As Integer
Dim aryCalls(1 To 6, 1 To 7) '1 to 3 rows: 1="CallsHdrName, 2=#CallsOpened, 3=#CallsClosed, 4=#CallsInProg and 1 to 6 Cols = number calls for each (row) by month (col):
Dim r, c, row As Integer 'count rows and columns for array manipulation
Dim intDaysInThisMonth As Integer, intNumberDaysBegMonth As Integer
Dim strMMM As String
Dim intStartMonth As Integer
Private Sub Document_Open()
RefreshRecordsets
End Sub
Private Sub cmdSQL_Click()
If IsNull(cboCustomer) Or cboCustomer = "" Then
MsgBox "Select a CUSTOMER from the drop down."
Exit Sub
End If
If IsNull(cboMonth) Or cboMonth = "" Then
MsgBox "Select a cut-off MONTH from the drop down."
Exit Sub
End If
If IsNull(cboYear) Or cboYear = "" Then
MsgBox "Select a YEAR from the drop down."
Exit Sub
End If
GetRecords
End Sub
Private Sub RefreshRecordsets()
GetAllCustomerRecords
Populate_cboCustomers
Populate_cboMonth
Populate_cboYear
End Sub
Public Sub ADO_Connection()
Set adoConn = New ADODB.Connection
adoConn.ConnectionString = "DSN=ClienteleSource;UID=sa;PWD=access_to_sql;"
adoConn.Open
End Sub
Sub Populate_cboCustomers()
Do Until rst.EOF
If Not IsNull(rst!OrgName) Then
cboCustomer.AddItem rst!OrgName
End If
rst.MoveNext
Loop
rst.MoveFirst
End Sub
Sub Populate_cboMonth()
cboMonth.AddItem "Jan"
cboMonth.AddItem "Feb"
cboMonth.AddItem "Mar"
cboMonth.AddItem "Apr"
cboMonth.AddItem "May"
cboMonth.AddItem "Jun"
cboMonth.AddItem "Jul"
cboMonth.AddItem "Aug"
cboMonth.AddItem "Sep"
cboMonth.AddItem "Oct"
cboMonth.AddItem "Nov"
cboMonth.AddItem "Dec"
End Sub
Sub Populate_cboYear()
cboYear.AddItem 2000
cboYear.AddItem 2001
cboYear.AddItem 2002
End Sub
Sub GetCustomerID()
Set rst = adoConn.Execute("SELECT Org_ID, OrgName FROM Org WHERE OrgName = '" & cboCustomer & "' "

If rst.BOF Then
MsgBox "This customer record not found."
End If
CusID = rst!Org_ID
rst.MoveFirst
End Sub
Private Sub GetRecords()
ADO_Connection
strSQL = "SELECT Call.Org_ID, Call.OpenDate, Call.ClosedDate, " & BegMth & " AS SelectBegMth, " & EndMth & " AS SelectEndMth, " _
& "IIf(Month([OpenDate])= " & BegMth & ",1,0) AS CallsOpened, " _
& "IIf(Month([ClosedDate])= " & BegMth & ",1,0) AS CallsClosed, " _
& "IIf(Month([ClosedDate])> " & BegMth & " Or Month([ClosedDate]) Is Null,1,0) AS CallsInProg " _
& "FROM Call " _
& "WHERE " _
& "(((Call.Org_ID)= '" & CusID & " ') AND " _
& "((Call.OpenDate>=#" & dteBegDate & "#) AND (Call.OpenDate <=#" & dteEndDate & "#)) " _
& "OR " _
& "((Call.Org_ID)='" & CusID & " ') AND " _
& "((Call.OpenDate)<=#" & dteEndDate & "#) AND ((Call.ClosedDate) Is Null Or (Call.ClosedDate)>=#" & dteBegDate & "#));"
'*************************************************************************************************************
'
'TEST ONLY: verify a connection and simple extract - (this completes without errors)
'strSQL = "SELECT * FROM Call"
'CODE CRASHES HERE WHEN RUNNING COLPETE SQL STATEMENT ABOVE - WITH ERROR MSG
'[Microsoft][ODBS SQL Driver][SQL Server] Line 1 Incorrect syntax near '='
'*************************************************************************************************************
Set rstCalls = adoConn.Execute(strSQL)
Do Until rstCalls.EOF
Debug.Print rstCalls(0)
rstCalls.MoveNext
Loop
End Sub
Private Sub ResetCounts()
CallsOpenedCount = 0
CallsClosedCount = 0
CallsInProgCount = 0
End Sub
Private Function GetDaysInThisMonth(dteBegDate)
Select Case Month(dteBegDate)
Case 4, 6, 9, 11
intDaysInThisMonth = "30"
Case 2
If Year(dteBegDate) Mod 4 = 0 Then
intDaysInThisMonth = "29"
Else
intDaysInThisMonth = "28"
End If
Case Else
intDaysInThisMonth = "31"
End Select
End Function
Private Sub GetEndOfMonthDate()
' Get number days in BegDate and set dteEndDate to end of first month (instead of all months in parameter entry)
GetDaysInThisMonth (dteBegDate)
dteEndDate = DateAdd("d", intDaysInThisMonth - 1, dteBegDate)
End Sub
Function GetMMM(BegMth)
Select Case BegMth
Case 1
strMMM = "Jan"
Case 2
strMMM = "Feb"
Case 3
strMMM = "Mar"
Case 4
strMMM = "Apr"
Case 5
strMMM = "May"
Case 6
strMMM = "Jun"
Case 7
strMMM = "Jul"
Case 8
strMMM = "Aug"
Case 9
strMMM = "Sep"
Case 10
strMMM = "Oct"
Case 11
strMMM = "Nov"
Case 12
strMMM = "Dec"
End Select
End Function