Solved

MS Access VBA syntax to include Access Table fields

Posted on 2013-05-14
3
424 Views
Last Modified: 2013-05-15
Hello Experts,

Please provide assistance to include a MS Access table field that displays the Path and the File Name as I am receiving a Run Time Error 1004 that displays "Could not be found".  I first tried the Spreadsheet Name without the http:// prefix then I tried what is below.
ID      SpreadsheetName                                                                SpreadsheetTab
1      http://X:\2013 reports\cfi consol nod 0313.xlsm                           Output 1
2      http://X:\2013 reports\cfmi consol 0313.xlsm                               Output 1
3      http://X:\Reports Close\CFMI Pre_Close_w_filters_RiskNEW.xlsm  Output 1 (100.100.T05)

and the VBA:
Private Sub WorksheetsCopy_Click()
Dim CopyFrom As Object
Dim CopyTo As Object
Dim CopyThis As Object
Dim xl As Object
Dim SpreadsheetName As String
Dim SpreadsheetTab As String
Dim ID As Field
Dim rs As Recordset
Dim oldPath As String, newPath As String
''Late binding
Set xl = CreateObject("Excel.Application")
xl.Visible = True
newPath = "H:\PDF"
Set rs = CurrentDb.OpenRecordset("TblReports")

Do While Not (rs.EOF)
''To use a password: Workbooks.Open Filename:="Filename", Password:="Password"
Set CopyFrom = xl.Workbooks.Open([SpreadsheetName])
Set CopyThis = CopyFrom.Sheets([SpreadsheetTab]) ''Sheet number 1
Set CopyTo = xl.Workbooks.Open("H:\PDF\MasterCopy.xlsx")
CopyThis.Copy After:=CopyTo.Sheets(CopyTo.Sheets.Count)
    rs.MoveNext
   CopyFrom.Close False

Loop

rs.Close

End Sub


Thanks,
Bob
0
Comment
Question by:CFMI
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39164351
Where do you get this error?

In order to refer to a Field in your Recordset you use syntax like this:

rs("YourFieldName")

There are other variations to this also:

rs![YourFieldName]

Either is fine.
0
 
LVL 1

Author Comment

by:CFMI
ID: 39164406
Great, the field is recogized but now I receive a type mismatch on the second statement, "Set CopyThis = CopyFrom.Sheets(rs("SpreadsheetTab"))"
0
 
LVL 1

Author Closing Comment

by:CFMI
ID: 39167877
The field syntax worked well.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Filter cbo on datasheet - 11 33
Can not open the Access Help ? 3 53
User Level Security 6 38
update all email addresses SQL 1 23
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question