Solved Exporting Error If fist query has no records... (1 Viewer)

Number11

Member
Local time
Today, 08:09
Joined
Jan 29, 2020
Messages
607
I have this code that creates a report combining two separate queries into an excel template with two sheets, this runs perfect, until say no records are in the first query, throughs out an error.. how to i tell it to continue onto the next pls?

1664545858010.png


Dim rst1
Dim vFile1
vFile1 = "Template_Location.xlsx"
Set rst1 = CurrentDb.OpenRecordset("Data_1")
rst1.MoveLast
With XL
.Visible = False
.Workbooks.Open vFile1
.Sheets("New").Select
.Range("A2").Select
.ActiveCell.CopyFromRecordset rst1
.ActiveWorkbook.SaveAs filename:=("C:\Local-Saved_Location.xlsx")
.ActiveWorkbook.Close

Dim rst2
Dim vFile2
vFile2 = "C:\Local-Saved_Location.xlsx"
Set rst2 = CurrentDb.OpenRecordset("Data_2")
rst2.MoveLast
With XL
.Visible = False
.Workbooks.Open vFile2
.Sheets("Update").Select
.Range("A2").Select
.ActiveCell.CopyFromRecordset rst2
.ActiveWorkbook.SaveAs filename:=("C:\Report Location.xlsx")
.ActiveWorkbook.Close
.Application.Quit
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:09
Joined
Oct 29, 2018
Messages
21,473
Maybe do something like this?
Code:
...
Set rst1 = CurrentDb.OpenRecordset("Data_1")
rst1.MoveLast
If Not (rst1.BOF AND rst1.EOF) Then
    With XL
        ...
    End With
End If
...
 

Number11

Member
Local time
Today, 08:09
Joined
Jan 29, 2020
Messages
607
Maybe do something like this?
Code:
...
Set rst1 = CurrentDb.OpenRecordset("Data_1")
rst1.MoveLast
If Not (rst1.BOF AND rst1.EOF) Then
    With XL
        ...
    End With
End If
...
No it didnt work same error
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:09
Joined
Sep 21, 2011
Messages
14,299
You do not move to a record unless you know you are at a record? :(
So test for EOF first before trying anything.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 03:09
Joined
May 21, 2018
Messages
8,529
I doubt that is the real code. There is no end with so that should not compile. Can you post your updated code with the EOF/BOF check? I expect something like.

Code:
Set rst1 = CurrentDb.OpenRecordset("Data_1")
if NOT (Rst1.EOF and RSt1.BOF) then
  rst1.MoveLast
  With XL
    .Visible = False
    .Workbooks.Open vFile1
    .Sheets("New").Select
    .Range("A2").Select
   .ActiveCell.CopyFromRecordset rst1
   .ActiveWorkbook.SaveAs filename:=("C:\Local-Saved_Location.xlsx")
   .ActiveWorkbook.Close
  end with
end if
 

Eugene-LS

Registered User.
Local time
Today, 10:09
Joined
Dec 7, 2018
Messages
481
this runs perfect, until say no records are in the first query
One more solution:
Code:
' ...
    Set rst1 = CurrentDb.OpenRecordset("Data_1")
    If rst1.RecordCount = 0 Then
        MsgBox "There are no records to export!", vbExclamation, "No entries"
        rst1.Close
        Set rst1 = Nothing
        Exit Sub
    End If
' ...
 
Last edited:

Number11

Member
Local time
Today, 08:09
Joined
Jan 29, 2020
Messages
607
I doubt that is the real code. There is no end with so that should not compile. Can you post your updated code with the EOF/BOF check? I expect something like.

Code:
Set rst1 = CurrentDb.OpenRecordset("Data_1")
if NOT (Rst1.EOF and RSt1.BOF) then
  rst1.MoveLast
  With XL
    .Visible = False
    .Workbooks.Open vFile1
    .Sheets("New").Select
    .Range("A2").Select
   .ActiveCell.CopyFromRecordset rst1
   .ActiveWorkbook.SaveAs filename:=("C:\Local-Saved_Location.xlsx")
   .ActiveWorkbook.Close
  end with
end if
Code:
Private Sub Command25_Click()


Dim XL As Excel.Application
Set XL = CreateObject("excel.application")

Dim rst1
Dim vFile1

vFile1 = "Template_Location.xlsx"

Set rst1 = CurrentDb.OpenRecordset("Data_1")
rst1.MoveLast
If Not (rst1.BOF And rst1.EOF) Then
With XL
   .Visible = False
   .Workbooks.Open vFile1
   .Sheets("New").Select
   .Range("A2").Select
   .ActiveCell.CopyFromRecordset rst1
   .ActiveWorkbook.SaveAs filename:=("C:\Local-Saved_Location.xlsx")
   .ActiveWorkbook.Close
End With
End If

Dim rst2
Dim vFile2

vFile2 = "C:\Local-Saved_Location.xlsx"

Set rst2 = CurrentDb.OpenRecordset("Data_2")
rst2.MoveLast
If Not (rst2.BOF And rst2.EOF) Then
With XL
   .Visible = False
   .Workbooks.Open vFile2
   .Sheets("Update").Select
   .Range("A2").Select
   .ActiveCell.CopyFromRecordset rst2
   .ActiveWorkbook.SaveAs filename:=("C:\Report Location.xlsx")
   .ActiveWorkbook.Close
   .Application.Quit
End With
End If


End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 03:09
Joined
May 21, 2018
Messages
8,529
I left the movelast, but I do not think there is any reason or need for moving to the last record if one exists.
 

Eugene-LS

Registered User.
Local time
Today, 10:09
Joined
Dec 7, 2018
Messages
481
The same code with less strings:
Code:
Private Sub Command25_Click()
' ... procedure description here  ...
'---------------------------------------------------------------------------------------------------
Dim XL As Object
Dim rst
Dim vFile
On Error GoTo Command25_Click_Err

    vFile = "Template_Location.xlsx"
    Set XL = CreateObject("Excel.Application")

    Set rst = CurrentDb.OpenRecordset("Data_1")
    If Not (rst.BOF And rst.EOF) Then
        With XL
           .Visible = False
           .Workbooks.Open vFile
           .Sheets("New").Select
           .Range("A2").Select
           .ActiveCell.CopyFromRecordset rst
           .ActiveWorkbook.SaveAs "C:\Local-Saved_Location.xlsx"
        End With
    End If
    rst.Close
   
    Set rst = CurrentDb.OpenRecordset("Data_2")
    If Not (rst.BOF And rst.EOF) Then
        With XL
           .Sheets("Update").Select
           .Range("A2").Select
           .ActiveCell.CopyFromRecordset rst
           .ActiveWorkbook.SaveAs "C:\Report Location.xlsx"
           .ActiveWorkbook.Close
           .Application.Quit
        End With
    End If


Command25_Click_End:
    On Error Resume Next
    Set XL = Nothing
    rst.Close
    Set rst = Nothing
    Err.Clear
    Exit Sub

Command25_Click_Err:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub : " & _
           "Command25_Click - mod00Test.", vbCritical, "Error!"
    Err.Clear
    Resume Command25_Click_End
End Sub
 

Users who are viewing this thread

Top Bottom