Access VBA not recognizing range in Excel spreadsheet - ms-access

I am having a most frustrating time time with the DoCmd.TransferSpreadsheet method. I have a workbook with multiple worksheets in which users are updating data and I have a script that puts all the records back into a single sheet, links the spreadsheet, and updates the data in my Access DB. My problem is in the Range parameter. I pass the following string and get the following error:
DoCmd.TransferSpreadsheet TransferType:=acLink, SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=linkSheet, fileName:=Wb.Path & "\" & Wb.name, _
HasFieldNames:=True, Range:="AccessUpdate!updateTable"
The Microsoft Access database engine could not find the object 'AccessUpdate$updateTable'. Make sure the object exists and that you spell its name and the path name correctly. If 'Access_Update$updateTable' is not a local object, check your network connection or contact the server administrator.
I can't seem to understand why it substitutes the dollar sign for the bang. Any other help in understanding how to specify the range would also be appreciated.
Thanks!

I know this is an year old question but it is an almost timeless problem.
I'm trying to do the same from the Excel side and bumping into the same problem. Access switching the sheet separator "!" for "$"
I found that this is a bug from Access 2000 that was never corrected. Or better, it was partially corrected at some point. So depending on your Access build and the size of the range [yes, size, since this is a bug from Access 2000] the solutions provided by Cisco or HansUp will work.
Another sources explaining the problem and a similar solution is provided by the MS$ themselves
https://answers.microsoft.com/en-us/office/forum/office_2007-access/transferspreadsheet-error-3011-can-not-file-sheet/980b2dc1-9ee1-4b3e-9c3c-a810f1428496
with the help of Bob Larson Former Access MVP (2008-2010) [see his very last post]
Now, if your range is on a different sheet with more than 65536 rows, this bug will come back.
See here for reference
Funny enough, if this is Sheet1 [yes, index 1 of all sheets] it will work with any range size. But any other sheet it wil fail.
This was my original range: BASE!A2:X68506, named REF_ACCESS. BASE is my Sheet5. Access 2010 & Excel 2010
I tried ActivateSheet, assign to string inside command, assign to string outside command, replace(,"$","!""), nothing worked. Even on Office 2016 from a friend
If I use "BASE!A2:X64506", it works. If I use "A2:X68506", Access assumes Sheet1 and works. Attention that all ranges do not have "$", but I guess you already know that
My last test was something like this monster
DoCmd.TransferSpreadsheet TransferType:=acImport, SpreadsheetType:=9, TableName:="TEST", Filename:=ThisWorkbook.FullName, HasFieldNames:=False, Range:=Worksheets("BASE").Name & "!" & Replace(Left(Worksheets("BASE").Range("REF_ACCESS").Address, Len(Worksheets("BASE").Range("REF_ACCESS").Address) - 1), "$", "")
A test that using my range within the 65536 row limit [6553 to be precise] would work. And it did.
So I see solutions with only two options for now. Either copy your range to Sheet1 or another sheet, as RyanM did, or divide your range in multiple DoCmd with 65536 rows.
I know it is long. Sorry, this was 2 full days looking for an answer without any real solution. I hope this helps other people with the same problem.

I tried multiple methods for getting around this without making major modifications to my code but with no avail. I did come up with a solution but it is rather resource intensive and messy. However, in case someone has a similar issue, I will post it here. I wound up separating my update sheet into it's own file from the rest of the workbook and linking that file. This prevented Access from trying to link a different sheet and got me around the whole Range issue. I know it's not elegant or efficient but it worked. If I figure out a cleaner way I'll post it here.
Set xl = Wb.Parent
xl.ScreenUpdating = False
xl.DisplayAlerts = False
strFile = mypath & "\TempIss.xlsx"
For i = 1 To Wb.Worksheets.count
If InStr(1, Wb.Worksheets(i).name, "Update", vbTextCompare) > 0 Then
tableId = i
Exit For
End If
Next i
If tableId = 0 Then
MsgBox "This workbook does not seem to have the necessary worksheet for updating " & _
"the Participant Issues Log in Access.", vbInformation, "Uh oh..."
Exit Function
Else
Set upWs = Wb.Worksheets(i)
upWs.Select
upWs.Copy
xl.ActiveSheet.SaveAs fileName:=strFile
xl.ActiveWorkbook.Close
Call rmSheet(Wb, "AccessUpdate")
xl.ScreenUpdating = True
linkSheet = "tempIssLog"
DoCmd.TransferSpreadsheet TransferType:=acImport, SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=linkSheet, fileName:=strFile, _
HasFieldNames:=True
Kill (strFile)

If the range is a named range (in Excel) follow the instruction above (HansUp comment).
If the range is defined in MS-Access be sure to pass a string (something like "A1:G12") and not the control name.
Dim StrRange as variant
Dim NameofMySheet as string
NameofMySheet = "xxxxxx" ' <- Put here the name of your Excel Sheet
StrRange = NameofMySheet & "!" & "A1:G12"
DoCmd.TransferSpreadsheet TransferType:=acLink, SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=linkSheet, fileName:=Wb.Path & "\" & Wb.name, _
HasFieldNames:=True, Range:= StrRange
Note 1: StrRange with no quotes!

Related

Can't find msgbox code (Deleted it) but msgbox still appears

I haven't even been able to find anyone with this problem online, let alone a solution!
I had several msgboxes in some VBA code I was writing to provide feedback for debugging. After a while the code was stable and I commented out the MsgBox lines.
One set of msgboxes wouldn't vanish though. I even deleted it rather than removing it. In the end I decided I must have a duplication somewhere, somehow, even though all the related code ended up calling this specific function where I'd removed the MsgBox.
I couldn't see any other explanation as I'd literally deleted the lines of code. I searched the entire project for fragments of the string which formed part of the MsgBox's text. Ctrl + F, set to search entire project, tried with and without pattern matching.
I found literally nothing. I decided the string must be more constructed than I thought, and instead opted to search using 'MsgBox' to search through every time I had used the command. I checked every single MsgBox in every project file and found nothing even remotely like the debugging message the code was still generating.
Anyone have any other ideas what may be going on? My best theory is that I've somehow hidden or duplicated something, but aside from depreciating a form whose code doesn't contain a single MsgBox or call code which does, I've done nothing I can think of to cause this.
I'd paste the code but you'd still just be taking my word for it that the MsgBox isn't there.
Can anyone help? It has to be hiding in there somewhere, somehow.
Many thanks,
Code where the MsgBox originally appeared (it provided feedback based on the RecordSet's NoMatch property):
Public Function createRecordSnapshot(Table As String, dbRefIn As String, payNum As String)
Dim fieldNames As String
Dim oldDataSet As String
Dim rs As Recordset
Dim fieldCount As Integer
Dim recordTot As Integer
Dim srchString As String
Set rs = CurrentDb.OpenRecordset(Table, dbOpenDynaset)
srchString = "DBRef = '" & dbRefIn & "' and PayrollRef = '" & payNum & "'"
fieldCount = rs.Fields.Count
rs.FindFirst (srchString)
Dim o As Integer
For o = 0 To fieldCount - 1
fieldNames = fieldNames & rs.Fields(o).Name & "|"
Next o
For o = 0 To fieldCount - 1
oldDataSet = oldDataSet & rs.Fields(o).Value & "|"
Next o
createRecordSnapshot = fieldNames & vbNewLine & oldDataSet
End Function
After a modification it is good practice to re-compile the code manually, as it makes running the code faster (can skip the compilation on next run) and refreshes previously compiled code. The option is in the VB Editor's Debug menu.

VBA Access transferspreadsheet - do warnings, but no data

I have the following code:
StrSheetName = "Weekly Account Balances"
StrTableName = "Account Weekly Balances"
fReportingFile = fDirectory & "\" & fReportingFile
DoCmd.SetWarnings True
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, StrTableName, _
fReportingFile, True, StrSheetName & "!"
I have verified that the sheet name, table names are correct and fReportingFile are correct using a break at the DoCmd.TransferSpreadsheet. I have also verified that 123 records are in the "Weekly Account Balances" sheet.
However, no records are transferred and I don't receive a warning.
I have also checked to see if there are any error tables created in the Access DB, but there are none.
Obviously, I have something wrong with the transfer call, but because I'm not getting any messages, I don't know what it is.
Why might I not be receiving any messages and no data is transferred?
Obviously, I have something wrong with the transfer call
No, that is correct, and it works - if, of course, your path and file names is pointing to an existing file, and you have specified the correct worksheet name.
So, double-check every parameter and your workbook.

Re-map linked tables in Access 2010

I am a relative novice with Access and starting from scratch with coding, so be gentle.
I have an Access 2010 database with dozens of linked tables based on .txt files. Sometimes the database moves, the source files move or the file server just gets re-named. In these events I am looking for a simple way for a user of the database to remap and refresh the linked tables. Ideally, it would be user prompted, i.e. the user pushes a button to refresh from a navigation form or something. Then, the system prompts for the new folder location. The folder location would house all of the necessary files, so it only needs to be selected one time. Once selected, all linked tables should remap and refresh with the user getting an error or success message.
I have seen a lot of these questions asked, but they seem to be in older versions of Access or it is not asking for a user prompt or for a user to browse for the new path.
Thanks.
While I agree with Marc B that this seems like a very oddly constructed database you could use the following code the manually link the tables again to the proper location. You would need to work this into a system that can loop through all tables and do them all one by one or adjust this code to do that automatically. But you may want to just rethink your system.
Function SetTableLinkPath(strTableName As String, strTablePath As String)
If Nz(strTableName, "") <> "" And Nz(strTablePath, "") <> "" Then
Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.TableDefs(strTableName).Connect = ";DATABASE=" & strTablePath
cdb.TableDefs(strTableName).RefreshLink
MsgBox "Table link for " & strTableName & " has been successfully set to the path: " & strTablePath & "."
Else
MsgBox "You must enter a valid Table path and name!"
End If
End Function

I need to dynamically (and quickly) import part of the data in an Access table (a large one) into Excel

I'm trying to create a dynamic report in Excel. I have lots of sales data and I want the user to be able to slice and dice it according to his needs.
Normally I would use two sheets: one hidden, containing the raw data, and one visible, containing all the buttons and form controls so that the user can dinamically select and visualize only a small subset of the original data at the time.
The problem is that this time I need to handle 6.000.000+ rows of data (and counting). Storing it all in an excel sheet is not an option. Besides, the data is already in the form of an Access table.
I tried accessing it dinamically via a query that "filters out" the un-needed information based on what the user selects in the form control on the Excel sheet. For some reason, this is very slow. It takes 4-5 minutes to pull out as little as 10 rows of data.
There has to be a quicker way to do this! I need this whole process to feel "instantaneous".
Any thoughts?
Edit: Ok, so the problem seems to be related to the fact that my access table is actually a linked table pointing to a *.txt file. This slows the import down a lot.
I tried both of the suggested solutions.
iDevlop's idea works quite fast (200k rows imported in 10-15 secs), but it has the downside of me having to update the table every time. I'll post another question, like he suggested, to see how and if the process can be automated.
Remou's script works perfectly too now (I had a hard time getting it right but he was really open and helpful so know I got it) and, although slower, it has the advantage of not requiring any database mantainance.
There's a few more things I need to get straight before choosing which approach to use. For now, all I want to say is thank you guys for you help! I could have never made it without you!!!
Don't bother going through Access if you have a text file. This may hold you until you can get a better system in place.
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer
strFile = "z:\docs\"
''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''
''Connection strings : http://www.connectionstrings.com/excel
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Text;HDR=Yes;IMEX=1"";"
''Late binding, so no reference is needed
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT * " _
& "FROM [test.txt] a " _
& "WHERE a.FirstName ='Bernard'"
rs.Open strSQL, cn, 3, 3
''Pick a suitable empty worksheet for the results
Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs
''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
If your file is tab delimited, you can use a schema.ini (http://msdn.microsoft.com/en-us/library/ms709353(VS.85).aspx). It must be in the same directory as you text file and need only contain two lines:
[Ordini BO new.txt]
Format=TabDelimited
Your connection string should read:
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Text;HDR=No;IMEX=1;FMT=Delimited"";"
As says Remou, check xour indexes, but also make sure your criteria are entered in a way that allows indexes to be used !
e.g : WHERE Format(myDate,"yyyy-mm") = "2011-09" does not allow the date index optimisation,
while WHERE myDate BETWEEN #09/01/2011# AND #09/30/2011# does allow index optimisation.
Edit:
If you have some kind of unique identifier in your text file and you translate that into a PK in your table design, you can then import the whole thing on a regular basis, and the duplicates will be discarded by the PK.
The import could be automated, even with a .vbs, you don't need Access to do it. Make that another question if you're stuck.
You could also ask the IT guys to delete the older records every month or so.

MS Access 2003 - Sparkline Graphs in Microsoft Access

Hey guys. Just wondering if anyone knows of a method to create sparkline graphs on a form in MS Access. The chart builder does not really work very well to create sparkline charts (graphs that small).
Just curious, thanks!
I don't think there is anything built in for Sparkline graphs in MS Access. You have to use a third party control and deploy it along with your app to all users or use MS Excel embedded control to show the graph.
There is a VBA-powered sparkline solution featured on the Access blog fairly recently: http://blogs.office.com/b/microsoft-access/archive/2011/02/10/power-tip-add-sparkline-like-graphs-to-access-reports.aspx
They have an .mdb as well as an .accdb sample file available, so I'm guessing it works across multiple versions.
I started with the VBA-powered sparkline but didn't like that it looked low-resolution and I couldn't use it on a continuous form (it only works on reports). The solution I came up with was to build the charts in Excel and save the chart images in a subfolder. Then it's easy to link the image on a report or continuous form. My charts update nightly, though the Excel chart building loop is really fast. The slow part is generating the data that the charts need, which may vary depending on what you are charting.
I created a template in Excel that had a chart with the look and resolution that I wanted. I wrote a VBA routine in Access to open an Excel sheet and loop through each record that I wanted to chart. The sheet is passed to this function (below), which loads a records of chart data and passes it into Excel, which automatically refreshes the 'SparkChart' object. It then saves the image to a subfolder. The Excel sheet stays open and is re-used with each loop. I didn't include the function with the loop.
Here's what my chart looks like in Excel:
Here is an example of the Sparklines shown in a continuous form:
Public Function fCreateSparklineChart(pDQ_ID As Long, pChartSheet As Object) As Boolean
' Pass in a Dashboard Query ID for data that has already compiled into the top-n
' temp table and the data will be copied to the passed pChartSheet in Excel. This
' will update a chart object, then the chart is saved as a .png file.
Dim strSQL As String
Dim strChartPath As String
Dim rs As DAO.Recordset
On Error GoTo ErrorHandler
' Get chart data from a table that has already been compiled with
' min and max values as percentages so the lowest value is 0
' and the highest value is 100.
strSQL = " SELECT DQ_ID, Trend_Value, " & _
" IIf(Trend_Value=0,0,Null) AS Min_Point, " & _
" IIf(Trend_Value=100,100,Null) AS Max_Point " & _
" FROM " & DASHBOARD_TMP_TABLE & _
" WHERE (DQ_ID=" & pDQ_ID & ") "
strSQL = strSQL & " ORDER BY RowNo "
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
If rs.RecordCount > 0 Then
pChartSheet.Range("A1").CurrentRegion.Clear
pChartSheet.Range("A1").CopyFromRecordset rs
pChartSheet.ChartObjects("SparkChart").Chart.SetSourceData pChartSheet.Range("rngData")
' Use a filename that includes the record ID.
strChartPath = CurrentProject.Path & "\Images\Sparkline_DQ_ID_" & Format(pDQ_ID, "0000") & ".png"
' Delete the file if it already exists.
DeleteFile strChartPath
' Save the Excel chart as a png file.
pChartSheet.ChartObjects("SparkChart").Chart.Export strChartPath, "png"
fCreateSparklineChart = True
End If
Exit_Function:
Exit Function
ErrorHandler:
fCreateSparklineChart = False
MsgBox "Error #" & err.Number & " - " & err.Description & vbCrLf & "in procedure fCreateSparklineChart of basSparkline"
GoTo Exit_Function
End Function
I've been thinking of creating a YouTube video explaining how I built this Data Quality Dashboard to chart data trends. Let me know if you are interested and I may be encouraged to do it.