C
Cire via AccessMonster.com
Cire said:[quoted text clipped - 41 lines]recordset could cause trouble. What version of Excel and Access are you
using?
i still got error pointing to " .Range("A2").CopyFromRecordset rst "
but when i cycle through the rest of the code using F8, i still end up with a
prompt to make changes to "Book1" even though i left the name as Results(i'm
using results instead of export). when i said yes and selected my desktop as
the save path, the export complete message appeared and the file was on my
desktop with all the data and the fields(after adding that extra code)
so apparently it manages to complete the recordset and copy from it but
somehow access vba still detects an error on that line. i'll post my whole
code up
Heres my code:
Private Sub cmd_RunQuery_Click()
On Error GoTo Error_Handler
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim sqlstring As String
Dim lngMax As Long
Dim lngCount As Long
Dim strPath As String
strPath = "C:\Results.xls"
strPath = InputBox("Enter Excel Path " & _
"(e.g. " & strPath & ")", _
"Results", strPath)
If Not strPath Like "*.xls" Then
Exit Sub
End If
sqlstring = "SELECT .....
sqlstring = "From .....
quot = "'"
' Incomplete criteria
If IsNull(Me.cbx_int_org.Value) Or IsNull(Me.txt_last_open_date.Value) Or
_
Not IsNull(Me.txt_yr_opened.Value) Then
MsgBox "Incomplete criteria, please fill in the mandatory criteria " & _
"before running the query. Click Ok to continue filling in the
criteria"
Set qdf = Nothing
' All tickets By GE,regardless of year and created before a user-input
date
Elseif Me.cbx_int_org.Value <> "Select All" And IsNull(txt_yr_opened.
Value) Then
sqlstring = sqlstring & " WHERE int_org.iorg_name = " & quot & Me.
cbx_int_org.Value & quot
sqlstring = sqlstring & " AND DATEADD(ss, request.open_date,'1970-1-1') <= "
& quot & txt_last_open_date.Value & quot
sqlstring = sqlstring & " ORDER BY request.open_date DESC, request.ref_num
DESC "
Set db = CurrentDb()
Set qdf = db.QueryDefs("all_requests")
qdf.Sql = sqlstring
DoCmd.Minimize
'DoCmd.OpenQuery "qry_append_all_requests"
Set rst = qdf.OpenRecordset(dbOpenForwardOnly)
If rst.EOF Then
MsgBox "No records to export", vbInformation
GoTo Exit_Handler
End If
If Len(Dir(strPath)) < 0 Then
Kill strPath
End If
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
With xlSheet
.Name = "Results"
.UsedRange.ClearContents
lngMax = rst.Fields.Count
For lngCount = lngMax To 1 Step -1
.Cells(1, lngCount).Value = rst.Fields(lngCount - 1).Name
Next lngCount
.Range("A2").CopyFromRecordset rst
End With
lngMax = xlBook.Worksheets.Count
For lngCount = lngMax To 1 Step -1
If xlBook.Worksheets(lngCount).Name <> "Results" Then
xlBook.Worksheets(lngCount).Delete
End If
Next lngCount
xlBook.SaveAs strPath
MsgBox "Export Complete", vbInformation
Set qdf = Nothing
rest of my code - more elseif statements
..
..
..
End If
i got a feeling my if else loops overlap each other somewhere...and i
understand how a function works now, if i make a function within the form and
declare it public, i can just invoke them into my sub cmd_click after each of
my elseif statements although i'm not sure whats the syntax of it. Thus for
testing purpose, i've put one set of code into one of the elseif statements
as shown above and enter criteria the same way as my elseif statement has
specified.