Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Access VBA syntax to include Access Table fields

Posted on 2013-05-14
3
Medium Priority
?
431 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 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

877 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