Inserting subform/Table data into specific fields in Excel (1 Viewer)

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
Hi,

I had this working but now need to insert multiple rows for an item (Description) into either one record in Excel or each row into their own row while keeping PONumber, Supplier, SiteID, Date and Ordered for the same.
The main form - frmPO has the continuous subform - StoresTempSubFrm on it that holds the rows of descriptions as entered by the user.
These descriptions are temporarily stored in the table - StoresTemp which is cleared once the items are saved in the Excel spreadsheet.
I created an autonumber field that resets each time in the hope of using the number 1 to however many items are entered as a possible reference but after trying many things from various sites I cannot get it to work and don't really know exactly what i am doing.

Here is the current code i am using which compiles but it is failing at line 49 saying saying does not support object or method?

Code:
Private Sub SaveAndNewBtn_Click()
On Error GoTo SaveAndNewBtn_Click_Err

Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim rs As DAO.Recordset
Dim lngPO As Long
Dim oRange As Range

If Me.OrderedBy.ListIndex < 0 Then
   ' Cancel = True
    MsgBox "You must specify the Person making the Order", vbOKOnly Or vbExclamation, "Selection required"
    Me.OrderedBy.SetFocus
End If

Call ImportDocument
If Len(Me.SelectedFile & "") = 0 Then
    MsgBox "You did not select any file, record will not be saved."
    Exit Sub
End If


'Copies the selected projects from the db to Excel
 
'Declare variables
Dim startrow, a, b, c As Integer
Dim mypath As String
Dim myvalue As String
Dim rec As Recordset
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim row As Integer
Dim Db As Database
 
'Turn off pop up warnings
DoCmd.SetWarnings False
 
'Initialise the database
Set Db = CurrentDb



Set oBook = objExcelApp.Workbooks.Open(Me.SelectedFile)
Set oSheet = oBook.Sheets(1)

oSheet.Rows("1:1").Select
objExcelApp.Selection.Insert Shift:=xlDown

Set oRange = oSheet.Range("A1")

' If Me.SelectedFile.Text = "Orders Placed*" Then

oBook
oSheet(Me.SelectedFile).Offset(0, 5).Value.CopyFromRecordset rs  'Copy recs to sheet
 
Set wb = Excel.Application.Workbooks.Open(mypath)
 
'Open the Excel file
Excel.Application.Visible = True
 
'Set the source table from Access to be copied to Excel
Set rec = Db.OpenRecordset("StoresTemp")
 
'Set the Worksheet that will accept the copied data
Set ws = wb.Worksheets(Me.SelectedFile)
 
Sheets("Stores Orders placed").Select
 
'Count the field headings for the Loop to use
c = CurrentDb.TableDefs("StoresTemp").Fields.Count
 
'Paste the headings onto the Excel worksheet - adjust the below to suit your headings

        ws.Cells(0, 0).Value = rs!PONumber
        ws.Cells(0, 2).Value = rs!PODate
        ws.Cells(0, 3).Value = DLookup("OrderedByName", "tblOrderedBy", "OrderedByID = " & Nz(rs!OrderedByID, 0)) & ""
        ws.Cells(0, 3).Value = rs!PersonFor
        ws.Cells(0, 5).Value = DLookup("Description", "StoresTemp", "Criteria= 'string'")
        ws.Cells(0, 7).Value = rs!Quantity
        ws.Cells(0, 10).Value = rs!OrderValue
        ws.Cells(0, 18).Value = rs!ETA
        ws.Cells(0, 19).Value = DLookup("SiteName", "tblSite", "SiteID = " & Nz(rs!SiteID, 0)) & ""
 
'Start pasting on Row 2
startrow = 2 'Cell Row number
 
'Start pasting in Column 'A'
a = 1 'Cell Column Column number
b = Nz(0, "") 'Table Field number
'b = "" if the field is blank
'c = count of table fields
 
'Loop through the code until End of File (EOF)
Do Until rec.EOF
 
Do Until b = c
 
ws.Cells(startrow, a) = rec.Fields(b).Value
a = a + 1
b = b + 1
 
'Loop through the data & Paste into Excel until finished
Loop
 
Cells.Select
With Selection
.WrapText = False
End With
startrow = startrow + 1
rec.MoveNext
a = 1
b = 0
Loop
 
MsgBox "Data Exported!", vbCritical, "Export Successful"
 
'Save workbook & Unset variables
wb.Save
wb.Close
Set ws = Nothing
Set rec = Nothing
Set wb = Nothing
Set Db = Nothing
 
'Turn on pop up warnings
DoCmd.SetWarnings True

SaveAndNewBtn_Click_Exit:
    Exit Sub

SaveAndNewBtn_Click_Err:
    MsgBox Error$
    Resume SaveAndNewBtn_Click_Exit
    
End Sub

Any help would be amazing thank you guys
 

Attachments

  • Excel Sheet.png
    Excel Sheet.png
    33.5 KB · Views: 113
  • Form Design view.png
    Form Design view.png
    167.3 KB · Views: 111
  • Form View.png
    Form View.png
    97.7 KB · Views: 106
  • StoresTemp Table.png
    StoresTemp Table.png
    28 KB · Views: 113

June7

AWF VIP
Local time
Today, 06:34
Joined
Mar 9, 2014
Messages
5,466
There are no line numbers. Don't make me count or copy code to another app to generate line numbers. Which line is line 49?
 

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
There are no line numbers. Don't make me count or copy code to another app to generate line numbers. Which line is line 49?
Oops sorry about that!

Its this line - Set oRange = oSheet.Range("A1")

Not sure what else it will fail on after that yet?

I'll be back on here tomorrow now........
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:34
Joined
Sep 21, 2011
Messages
14,238
More than likely you will need to fully qualify each excel object , ie use objExcelApp as you set them.
 

June7

AWF VIP
Local time
Today, 06:34
Joined
Mar 9, 2014
Messages
5,466
Not showing declaring and setting objExcelApp.

Dim objExcelApp As Excel.Application
Set objExcelApp = CreateObject("Excel.Application")

Do you have Option Explicit in header for each module?

Also, declare oSheet with Excel prefix:

Dim oRange As Excel.Range

Why are you declaring/setting/using another set of Excel variables?

Disable error handler for debugging. Only implement after code works properly.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 15:34
Joined
Sep 21, 2011
Messages
14,238
Not showing declaring and setting objExcelApp.

Dim objExcelApp As Excel.Application
Set objExcelApp = CreateObject("Excel.Application")

Do you have Option Explicit in header for each module?
Nice catch @June7 :)
 

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
Good morning and thank you guys.

I have this for the form declarations:

Code:
Option Compare Database
Option Explicit

Dim objExcelApp As Excel.Application
Dim wb As Excel.Workbook
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:34
Joined
Sep 21, 2011
Messages
14,238
No good having the last two lines where they are. :(

The top two line should be once at the top of each module.
The bottom two lines should be in each sub/function. Perhaps you should set them to nothing after use?, in case they point to old locations from last usage?

You need to show all your code when posting, or at least advise of global variables.
I still think you need to fully qualify the Excel objects from within Excel. Most of the code works as is, when copied from Excel, except for the setting of objects.
 
Last edited:

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
Not showing declaring and setting objExcelApp.

Dim objExcelApp As Excel.Application
Set objExcelApp = CreateObject("Excel.Application")

Do you have Option Explicit in header for each module?

Also, declare oSheet with Excel prefix:

Dim oRange As Excel.Range

Why are you declaring/setting/using another set of Excel variables?

Disable error handler for debugging. Only implement after code works properly.
Hi,

Done everything i think

This - Why are you declaring/setting/using another set of Excel variables? is just because i copied some code to try and pasted it to see if i can get it working, thats all
 

isladogs

MVP / VIP
Local time
Today, 15:34
Joined
Jan 14, 2017
Messages
18,211
Two questions
1. Is the error number 429? If not, what is it?
2. There is no PK on the StoresTemp table. Does adding a PK help?

Also it should be Dim db As DAO.Database
 

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
No good having the last two lines where they are. :(

The top two line should be once at the top of each module.
The bottom two lines should be in each sub/function unless you make them Global, which you have not.
Ah! very good shout as i always do the same which i thought was fine.
I'm learning all the time. Thank you
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:34
Joined
Sep 21, 2011
Messages
14,238
Ah! very good shout as i always do the same which i thought was fine.
I'm learning all the time. Thank you
Sam, I edited my post there. Not realising they were global variables. :(
I tend to just assign local variables within the procedure unless I am needing to have values between subs/functions. Even then I would use a TempVar.
Sorry about that. :(
 

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
Two questions
1. Is the error number 429? If not, what is it?
2. There is no PK on the StoresTemp table. Does adding a PK help?

Also it should be Dim db As DAO.Database
Hi, the error is - "Object does not support this property or method"

The StoresTemp table is only a temporary storage and is cleared after every save action
 

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
I just tried all again and its still failing at the same line?

I'm going to walk away for a bit and do something else........

Back later :unsure:
 

June7

AWF VIP
Local time
Today, 06:34
Joined
Mar 9, 2014
Messages
5,466
After the changes I suggested the code would run through that line. If you want to provide your file(s), this might be easier to resolve.

I am still confused as to why you have another set of Excel variables. The code has two Excel objects open and is doing things to each of them. Do you really want that?
 

isladogs

MVP / VIP
Local time
Today, 15:34
Joined
Jan 14, 2017
Messages
18,211
Hi, the error is - "Object does not support this property or method"

The StoresTemp table is only a temporary storage and is cleared after every save action

1. Yes I know what the error DESCRIPTION is. I asked what the error NUMBER is to help diagnose the issue. In fact its probably error 438

1657016891252.png

2. I would still suggest using a PK field
 

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
1. Yes I know what the error DESCRIPTION is. I asked what the error NUMBER is to help diagnose the issue. In fact its probably error 438

View attachment 101664
2. I would still suggest using a PK field
Right then here is the message I am getting
 

Attachments

  • Screenshot 2022-07-05 121059.png
    Screenshot 2022-07-05 121059.png
    6.9 KB · Views: 102

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
After the changes I suggested the code would run through that line. If you want to provide your file(s), this might be easier to resolve.

I am still confused as to why you have another set of Excel variables. The code has two Excel objects open and is doing things to each of them. Do you really want that?
I would only keep one set once i can get it working but i really do not know what I am doing, so trying to find code and a way to do this which i will eventually. so thank you for any help and guidance.

I have attached the DB and the excel spreadsheets I have to work with in case that helps
 

Attachments

  • P and L Database.zip
    1.2 MB · Views: 126

isladogs

MVP / VIP
Local time
Today, 15:34
Joined
Jan 14, 2017
Messages
18,211
Your code shows MsgBox Error$ in the error handler, which suggests the error handler isn't being reached.
Perhaps the error is in Excel rather than Access?

Try changing that line to:
Code:
MsgBox "Error " & Err.Number & " in SaveAndNewBtn_Click procedure: " & Err.Description

What error message do you see now?

I've downloaded your file but have no idea which button to click to trigger this behaviour
I don't have time to look at your db properly now but suggest you tell us what exactly to do for the error to occur
Help us to help you
 

Sam Summers

Registered User.
Local time
Today, 15:34
Joined
Sep 17, 2001
Messages
939
Your code shows MsgBox Error$ in the error handler, which suggests the error handler isn't being reached.
Perhaps the error is in Excel rather than Access?

Try changing that line to:
Code:
MsgBox "Error " & Err.Number & " in SaveAndNewBtn_Click procedure: " & Err.Description

What error message do you see now?

I've downloaded your file but have no idea which button to click to trigger this behaviour
I don't have time to look at your db properly now but suggest you tell us what exactly to do for the error to occur
Help us to help you
Hi and thank you.
I will try that code shortly.

If you open the DB with the the bypass key and open the form frmPO the error is generated from the 'Save' button after entering data into the subform
 

Users who are viewing this thread

Top Bottom