Hello everyone. This is closely related to my prior post - which was corrected. I no have encountered a new bug.
I would like to transfer an Excel spreadsheet into an Access table using the TransferSheet Method. The caveat is that the name of the file needs to involve a variable (a date) based on an entry from a TextBox. The name of the table is tblMTMReportData. It already has column headings. The name of the Excel spreadsheets is as follows: mtm_report_asof_yyyymmdd.xls. Now the date extension "yyyymmdd" varies.
I have created a userform and on that userform is a TextBox and a Command Button. The textBox and command button are on a page called pagCMDReturnMTMPortfolio. The textbox is called txtDate. The command button is called CommandReturnMTMPortfolio.
What I would like to happen is for the user to enter a date say 20110419. After that the user presses the button and the macro(or code) retrieves the file based on the date extension. The path to the files and folders are contained in another table.
I am getting an error : object variable or with block variable not set. I am assuming the object in question is txtDate the TextBox where the user enters a date.
I tried correcting this error by adding the following snipet:
but that does not work at all and causes the computer to crash. Any suggestions: the entire code is below:Set txtDate = Forms!.Controls.Add("vb.TextBox","txtDate")
The code I have so far is below:Sub GetMTMPortfolioData() Dim appXL As Excel.Application Dim strFilePath As String Dim strFileName As String Dim strFile As String Dim asofDATE As String Dim strFilePrefix As String Dim rstMTM As DAO.Recordset Dim strSQL As String Dim wbk As Excel.Workbook Dim strFullFileName As String Dim txtDate As TextBox On Error GoTo HandleErr strFilePrefix = "mtm_report_asof_" strSQL = "Select * From tblFilePath Where Name='MTMSummary'" Set rstMTM = CurrentDb.OpenRecordset(strSQL) strFilePath = rstMTM.Fields("Path") strFile = Dir(strFilePath) asofDATE = txtDate.Text 'asofDATE = Format(asofDATE, "yyyymmdd") Set appXL = New Excel.Application strFullFileName = strFilePrefix & asofDATE & ".xls" Set wbk = appXL.Workbooks.Open(strFilePath & strFullFileName) DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tblMTMSummary", strFilePath & strFullFileName, True MsgBox "Your MTM file has been imported." ExitHere: On Error Resume Next appXL.ScreenUpdating = True rstMTM.Close Set rstMTM = Nothing appXL.Quit Set appXL = Nothing Exit Sub HandleErr: MsgBox Err & ": " & Err.Description Resume ExitHere Resume End Sub
Last edited by AnthonyWB; 05-23-2011 at 07:47 PM.
On which line does the code crash?
The snippet you added doesn't work because of the !. An exclamation point and then a period is incorrect syntax. Anyway, this snippet looks like it will add a new textbox to your form, is that what you are trying to do?
Is your code running too slowly?
Does your workbook or database have a bunch of duplicate pieces of data?
Have a look at this article to learn the best ways to set up your projects.
It will save both time and effort in the long run!
Dave
Thanks for your response. I do not want to add a new textbox to my form.
lPease see the updated code below. There are no bugs per se, however the asofDATE is still not being interpreted correctly. Difficult to explain the error, but a warning pops up stating that -
....... mtm_report_asof_<div>date entered in txtDate TextBox </div>.xls
cannot be found. So I am still having problems.
Private Sub cmdReturnMTMPortfolio_Click() Dim appXL As Excel.Application Dim strFilePath As String Dim strFileName As String Dim strFilePrefix As String Dim strFullFileName As String Dim strFile As String Dim asofDATE As String Dim txtDate As TextBox Dim wbk As Excel.Workbook On Error GoTo HandleErr strFilePrefix = "mtm_report_asof_" strFilePath = "N:\CBI-DEPT\Application Delivery\Assignments and Projects\FCIP Program\Spreadsheet Automation\Product\Excel Automation\Robin\Communication\" asofDATE = Me.txtDate.Value 'asofDATE = Format(asofDATE, "yyyymmdd") Set appXL = New Excel.Application strFullFileName = strFilePrefix & asofDATE & ".xls" Set wbk = appXL.Workbooks.Open(strFilePath & strFullFileName) DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tempMTMReportData", strFilePath & strFullFileName, True MsgBox "Your MTM file has been imported." ExitHere: On Error Resume Next appXL.ScreenUpdating = True appXL.Quit Set appXL = Nothing Exit Sub HandleErr: MsgBox Err & ": " & Err.Description Resume ExitHere Resume End Sub
It would seem txtDate is not returning the date. How is this textbox populated? Can you ensure that a date is being shown when the code runs?
It doesn't look correct that you declare txtDate as a variable. If it is the name of a control on your form, you don't have to declare a variable for it.
Is your code running too slowly?
Does your workbook or database have a bunch of duplicate pieces of data?
Have a look at this article to learn the best ways to set up your projects.
It will save both time and effort in the long run!
Dave
All I would like to do is pass the date that was entered into the TextBox to a variable so that it can be used as part of a file name. How would you do that? Any suggestions?
Name of TextBox = txtDate
Name of variable that captures the value entered into txtDate = asofDATE
Your code looks correct for what you are trying to do, but there's no way to verify since I can't see your db. Put a breakpoint in on that line, and use the immediate window to find out the value of your textbox.
As a note, unless Access has changed from 03 to 07, you don't need to open an Excel workbook to import it.
Is your code running too slowly?
Does your workbook or database have a bunch of duplicate pieces of data?
Have a look at this article to learn the best ways to set up your projects.
It will save both time and effort in the long run!
Dave
Thanks for the advice. I broke the code right where the asofDATE is assigned. You were right. asofDATE is empty. Furthermore so is strFullFileName. Nothing is passed onto to those variables. Any further suggestion?
You should step through the code using F8 to see what gets assigned to each of those variables, and make sure it is what was intended. You can use the immediate window to find out the values of properties on your form.
Is your code running too slowly?
Does your workbook or database have a bunch of duplicate pieces of data?
Have a look at this article to learn the best ways to set up your projects.
It will save both time and effort in the long run!
Dave
Perhaps I am mising qoutes (single or double) around asofDATE in the expression:
Not sure? I stepped through the code via F8 and asofDATE = _<div>date entered in txtDate TextBox </div>. So for example if I enter a date of 20110107 thenrFullFileName = strFilePrefix & asofDATE & ".xls"
I am not sure where or what the <div> and </div> are coming from?asofDATE = "<div>20110107</div>"
The div are HTML tags. I don't know why Access is adding them, are you using Access 07?
It happens because you have used rich text format.
Select the text button and go to properties > Data > Text Format > Select Plain Text from there
In the database goto table in design mode > select the field > properties (General Tab) Text Format > select "Plain Text", if applicable.
Is your code running too slowly?
Does your workbook or database have a bunch of duplicate pieces of data?
Have a look at this article to learn the best ways to set up your projects.
It will save both time and effort in the long run!
Dave
Here is some code that I use regularly to export queries to Excel. While I realize that you already have your code, I also use an unbound text box as part of the code. I thought that seeing this may tip you to a solution.
As you can see I use the unbound text box txtfilepath to let the user select where he wishes the file to go. I hope this helps you with your issue.Private Sub Command5_Click() On Error GoTo Err_Command5_Click Dim reportName As String Dim theFilePath As String Select Case Me.Frame6.Value Case 1 reportName = "MonthlyActivity" End Select theFilePath = Me.txtfilepath.Value theFilePath = theFilePath & reportName & "_" & Format(Date, "yyyy-mm-dd") & ".xls" DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, reportName, theFilePath, True MsgBox "Look on your desktop for the report." Exit_Command5_Click: Exit Sub Err_Command5_Click: MsgBox Err.Description Resume Exit_Command5_Click End Sub
Alan
Alan
Click the * below to say thanks.
Database Principles
Pivot Table Tips
Good Excel Video Tutorials
Sumifs or SumProduct
DataPig Access Tutorials
MS Query Tutorial
Worst Pie Chart Ever?
The code below works. So I will close the thread. However I need to use relative paths. Also there is an error importing the data that leads to lost records. But technically it transfers the spreadsheet.
Private Sub cmdReturnMTMPortfolio_Click() Dim appXL As Excel.Application Dim strFilePath As String Dim strFileName As String Dim strFilePrefix As String Dim strFullFileName As String Dim strFile As String Dim asofDATE As String Dim txtDate As TextBox Dim wbk As Excel.Workbook On Error GoTo HandleErr strFilePrefix = "mtm_report_asof_" strFilePath = "N:\CBI-DEPT\Application Delivery\Assignments and Projects\FCIP Program\Spreadsheet Automation\Product\Excel Automation\Robin\Communication\Portfolio List\" asofDATE = Me.txtDate.Value asofDATE = Format(asofDATE, "yyyymmdd") Set appXL = New Excel.Application strFullFileName = strFilePrefix & asofDATE & ".xls" Set wbk = appXL.Workbooks.Open(strFilePath & strFullFileName) DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tempMTMReportData", strFilePath & strFullFileName, True, "MTM Detail!" MsgBox "Your MTM file has been imported." ExitHere: On Error Resume Next appXL.ScreenUpdating = True appXL.Quit Set appXL = Nothing Exit Sub HandleErr: MsgBox Err & ": " & Err.Description Resume ExitHere Resume End Sub
You could use a file dialog object to have the user select the path each time it is run.
Again, you don't have to open the workbook before importing it.
If you are importing to an existing table, the only records lost will be records that don't fit the data type that is stored in each field of the table. This should not affect any future calculations, just be aware that invalid data will show up as null.
Is your code running too slowly?
Does your workbook or database have a bunch of duplicate pieces of data?
Have a look at this article to learn the best ways to set up your projects.
It will save both time and effort in the long run!
Dave
I am actually losing quite a bit of data. So I will have to look at my fields again and see whats going on. I am not familiar with file dialog.
Also how can my code be modified to not open the file. Opening the file is all I can think of.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks