Stop export to Excel when no records match criteria (1 Viewer)

jom1918

Registered User.
Local time
Today, 23:07
Joined
Apr 26, 2011
Messages
30
I have a sales sytem in Access 2010. New customers have to be imported to MYOB daily. I have a query that finds new customers and appends them to a table for importing at a later stage. I export the records in that table if the field "imported" is set to false. The results are exported to Excel so they can be imported into MYOB after some additional data is added. If there are no records to append to the import table I want to stop the export from happening. I am new to VBA and don't know how to express that the select query "000 Append New Customers...." should only run if the append query has records. If there are no records found, I want to put a message saying "no records found". Can someone please help. I am sure this is really simple, but I am learning so much at the moment I think I am confusing myslef and I am unsure of how to do it.

Here is the code I have behind the command button:

Private Sub PrepCustcmd_Click()
'Turn warnings off
DoCmd.SetWarnings False
DoCmd.OpenQuery "000 Append New Customers to MYOB Customers", acViewNormal, acEdit
' This is where I need to have some code that tells VB what to do if no records to append

DoCmd.OpenQuery "200 Select Customers To Import To MYOB", acViewNormal, acEdit
DoCmd.OutputTo acOutputQuery, "200 Select Customers To Import To MYOB", "ExcelWorkbook(*.xlsx)", "R:\Josephine Mellahn\Sales Orders\200 Select Customers To Import To MYOB_" & Format(Date, "yymmdd") & ".xlsx", True, "", , acExportQualityPrint
'Turn warnings on
DoCmd.SetWarnings True
End Sub
 

DavidAtWork

Registered User.
Local time
Today, 14:07
Joined
Oct 25, 2011
Messages
699
you could try using DCount on the query and if > 0, export, else message

Code:
If Dcount("*","200 Select Customers To Import To MYOB") > 0 Then
    DoCmd.OutputTo .... etc
    Else: Msgbox "no records found",vbokonly + vbinformation
End If

David
 

jom1918

Registered User.
Local time
Today, 23:07
Joined
Apr 26, 2011
Messages
30
Thanks DavidAtWork. I really appreciate it. Your suggestion worked perfectly and I have learnt something new.
 

Users who are viewing this thread

Top Bottom