X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Export Only The Results The Search To Richtext File, Access 2007    
post Jun 12 2018, 09:26 AM

Posts: 814
Joined: 23-January 08
From: Lusaka,Zambia


I have a form with several unbound fields for searching the database with. The searching code is behind a search button

Private Sub CommandSearch_Click()

' more code here

If Len(strWhere) > 0 Then
    If Nz(DCount("*", "qryEvents", Left(strWhere, Len(strWhere) - 0)), 0) > 0 Then
       strWhere = "WHERE " & Left(strWhere, Len(strWhere) - 0)
       strSQL = "SELECT * FROM qryEvents " & strWhere & ";"
        Forms![frmEvents_Search].Form.RecordSource = strSQL
        MsgBox "No records matching your criteria were found.", vbInformation, "Events database"
        End If

    End If
End Sub

Now I am trying to export the results of search done with the previous code above to a rich-text file with the following code:

Private Sub CommandExport_Click()
Dim db As Database
Dim qdf As QueryDef
Dim strSQL As String
Dim rsCriteria As Recordset

Set db = CurrentDb
Set rsCriteria = db.OpenRecordset("qryEvents", dbOpenDynaset)

'*** the first record in the Criteria table ***

'*** loop to move through the records in Criteria table
Do Until rsCriteria.EOF

'*** create the Select query based on
' the first record in the Criteria table
strSQL = "SELECT * FROM tblEvents WHERE "
strSQL = strSQL & "[Event_ID] = " & rsCriteria![Event_ID]

'*** delete the previous query
'db.QueryDefs.Delete "qryEvents2"
Set qdf = db.CreateQueryDef("qryEvents2", strSQL)
DoCmd.OutputTo acReport, "rptEvents", "RichTextFormat(*.rtf)", "C:\SARPCCO accdb\SARPCCO_Report.rtf" ', False
End Sub

The problem I have is that the export button is exporting all the records from the database to the rich-text file. I want to export only the results of the search.

Hoping to get some help here.


Go to the top of the page
post Jun 17 2018, 03:34 AM

Posts: 1,184
Joined: 26-January 06
From: .....the wiregrass (either you know or you don't)

The code takes the form data from queryevents table/query but the export does not....the export takes the data from tblevents.

I can only guess that the queryevents is a subset of the tblevents....so it is already filtered and pulled only what you want.....but then you do not export it.....instead you go and export from the whole table tblevents.....

At least, that is what it looks like from a brieft study.
Go to the top of the page

Custom Search
RSSSearch   Top   Lo-Fi    22nd October 2018 - 10:17 AM