Solved

MS Access VBA syntax to include Access Table fields

Posted on 2013-05-14
3
422 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now