Hi Michael,
Try this variation instead, if you really want to embed the SQL statement
within your code, instead of just referencing a saved query. To assist in
debugging, let's create a separate subroutine first, where you can print the
results to the Immediate window (open with <Ctrl><G>).
Note:
If you do not already have "Option Explicit" shown as the second line of
code, at the top of your module, as indicated below, then add it now:
Option Compare Database
Option Explicit
If you had to add this very important line of code, then see this Gem Tip to
learn more about Option Explicit, and how to configure your Visual Basic
Editor (VBE) to always include these two very important words in all newly
created modules:
Always Use Option Explicit
http://www.access.qbuilt.com/html/gem_tips.html#VBEOptions
Okay, so here is the test subroutine. My assumption is that this is a class
module, associated with a form, given your references to intSevB, intSevP,
intSevC, txtDmOps and txtDmOssd.
Sub Michael()
Dim strSQL As String
strSQL = _
"SELECT tblTurnOver.txtIr, tblTurnOver.txtSystem, " _
& "tblTurnOver.txtDept, " _
& [intSevB] & " / " & [intSevP] & " / " & [intSevC] & " AS Severity, " _
& "tblTurnOver.dteDateReported, tblTurnOver.dteTimeReported, " _
& "tblTurnOver.txtGroupEsc, OPS " & [txtDmOps] _
& "/ OSSD " & [txtDmOssd] & " AS DM, " _
& "tblTurnOver.txtDesc, tblLIrStatus.txtStatus, " _
& "tblTurnOver.dteDateResolved, tblTurnOver.dteTimeResolved, " _
& "tblTurnOver.memComments, tblTurnOver.booResolved, " _
& "tblTurnOver.booPasson " _
& "FROM tblLIrStatus " _
& "INNER JOIN tblTurnOver ON tblLIrStatus.ID = tblTurnOver.intStatus " _
& "WHERE tblTurnOver.booPasson=" & True
Debug.Print strSQL
End Sub
After copying and pasting the above subroutine into the module associated
with your form, click on Debug | Compile ProjectName (where ProjectName is
the name of your VBA project). Correct any compile time errors that you may
find, including in any other unrelated modules.
Add a temporary command button to your form, with a On Click action to run
the Michael subroutine. Examine the results in the Immediate Window. If you
do not see any obvious errors, then try copying and pasting the result from
the Immediate window into the SQL view of a new query. Try running your new
query. If there is an error, the query parser will usually give you a better
clue as to the source of the error versus trying to run the same SQL
statement in VBA. The query should run without any errors, if your
constructed SQL statement is correct.
After you are able to produce a working SQL statement, copy the declaration
for strSQL and the strSQL = line of code into your existing procedure. Then
make the following change:
Set rs = db.OpenRecordset (strSQL, dbOpenSnapshot)
Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
michaelg51 said:
Hi Tom, sorry I didn't include everything earlier. I'm using
sCopyRSToNamedRange, the question marks were a marker for myself that I
should have taken out, I was trying to figure out the correct syntax for the
SQL statement, and I tried dbOpenForwardOnly just to see if it would make a
difference.
The error I'm getting is 'Runtime error 13 Type mismatch', I checked on your
site and I changed the declarations for db and rs to match what you
reccomended.
Here is the code I have so far, I still think I'm not formatting the SQL
correctly but I can't see where the error is.
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As DAO.database
Dim rs As DAO.Recordset
Const conMAX_ROWS = 20000
Const conSHT_NAME = "Report"
Const conWKB_NAME = "C:\report.xls"
Const conRANGE = "A4:K4"
Set db = CurrentDb
Set objXL = New Excel.Application
Set rs = db.OpenRecordset("SELECT tblTurnOver.txtIr,
tblTurnOver.txtSystem, tblTurnOver.txtDept, [intSevB] & " / " & [intSevP] & "
/ " & [intSevC] AS Severity, tblTurnOver.dteDateReported,
tblTurnOver.dteTimeReported, tblTurnOver.txtGroupEsc, ""OPS "" & [txtDmOps] &
" / " & ""OSSD "" & [txtDmOssd] AS DM, tblTurnOver.txtDesc,
tblLIrStatus.txtStatus, tblTurnOver.dteDateResolved,
tblTurnOver.dteTimeResolved, tblTurnOver.memComments,
tblTurnOver.booResolved, tblTurnOver.booPasson FROM tblLIrStatus INNER JOIN
tblTurnOver ON tblLIrStatus.ID = tblTurnOver.intStatus WHERE
(((tblTurnOver.booPasson)=True));", dbOpenSnapshot)
With objXL
.Visible = True
Set objWkb = .Workbooks.Open(conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Worksheets(conSHT_NAME)
If Not err.Number = 0 Then
Set objSht = objWkb.Worksheets.Add
objSht.Name = conSHT_NAME
End If
err.Clear
On Error GoTo 0
objSht.Range(conRANGE).CopyFromRecordset rs
End With
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rs = Nothing
Set db = Nothing