Solved

MS Access VBA syntax to include Access Table fields

Posted on 2013-05-14
3
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 85

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

624 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