I am trying to combine multiple audit tables, and then filter the results into an excel sheet. The Union All and parameters make the query in excess of 1200 characters. It appears the string is truncated when running this. What recommendations can anyone make. I have no control over the database structure and am only reading foxpro free tables.
I am permitted table creation but can write into the excel sheet connecting to the datasource
An update to this is that I was able to extend the query string beyond 1800 characters and get data back. So I conclude I had syntax errors and the apparent truncation I mentioned was a failure in my string development in the scripting.
I posted an example of my connection code and that answer has disappeared, so I am not sure how to designate this a closed issue. There is NOT an apparent string length limit and that was my initial concern. Thanks for the contributions.
Can you not create a recordset with a superset of the desired data, then run a second query against that to do the final filtering?
Alternatively, and I'm not in any way familiar with FoxPro or your database privileges, could you create a stored procedure in the database and then just pass parameters to it?
Edit to add: I presume you're already giving your tables short names? (e.g. ...FROM Extremely_Long_Table_Name a WHERE...) If not, this could probably save you a pile of characters.
If it's a Union Query, as your question suggests, then you could break it up into each sub-query and run them one after the other.
I can hardly believe that the limit on your query string would be so small though. Where is your query being truncated? By VBA? by FoxPro? Can you copy/paste your generated query directly into a database client and see if it runs correctly?
After rewriting the query a number of ways, I have gotten the string length for the query beyond 1800 characters successfully. I have to conclude that my previous error was syntax although the vba error didn't give many clues.
The query string is built using controls on an excel sheet and accumulate in SQLstr. Apparent truncation was an error in my scripting the string creation. Once that was resolved then this:
With ActiveSheet.QueryTables.Add(Connection:=Array( _
"ODBC;DSN=myDB;Description=myDB;DATABASE=myDB;Trusted_Connection=YES"), Destination:=Range("A2"))
.CommandText = SQLstr
.Name = "Query from myDB"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Thanks for the help because the responses indicating that it seemed unlikely to be a string length limit made me look at syntax instead.
I am still curious if there are query string size limits, if anyone knows.
Check for syntaxtical errors, I was getting similar errors, but it transpired to be syntax-error, there were some white-spaces missing where I concatenated my (2000+ chars long) query-string. Ensure that you have space before/after each concatenation-string, e.g.
qryStr = "SELECT name, tel, email" & _
" FROM MyTable;"
Note the space preceding the FROM clause (within double quotes). HTH. Eddie
Related
I'm attempting to extract a table of historical stock prices to bring into a an Excel worksheet. I'm currently using Excel for Mac so activex / com objects are not supported, which is why I have had to go this route. If I use a different URL it extracts the table information just fine, and visiting the URL directly in browser works fine, but if I try to extract historical price information Excel throws a VB error 1004.
HistoryURL = "URL:https://finance.yahoo.com/quote/AAPL/history?period1=1577836800&period2=1609459200&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true"
With .QueryTables.Add(Connection:=HistoryURL, Destination:=.Range("Z1"))
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = False
End With
What am I missing here?
Couldn't find a suitable way to do this with VBA so I enabled the Beta Channel updates for Excel which allowed me to start using the native StockHistory function.
I've done a bit of work with finnancial data/stock price data and I found the following API a lot easier to work with than yahoo's finance data. You can get historical stock price data, so feel free to check it out and see if it might work for your application. Finnancial API
I'm trying to import an Excel to Access DB, and some customer numbers cannot be imported due to Type Conversion Failure. It is 12 entries out of 66 thousand. Normally the customer number is a number, but these 12 are strings like ABCDEFT001. I tried setting the field of the table to Long Text or Short text, they are still not imported (only to ImportError table). Do you know what else I can try?
Thank you very much in advance!
P.S. I'm trying to import using DoCmd.TransferSpreadsheet
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Inv level", "Path/to/file.xlsb", True, "Sheetname!"
This strategy links to the excel file, instead of directly importing it, and then selects data out of it and casts any fields that need it to the correct datatype.
Sub ImportFromExcel()
Dim pathToFile As String
Dim targetTableName As String
Dim sql As String
pathToFile = "C:\Path\To\File.xlsx"
targetTableName = "ImportResults"
'//create the link
DoCmd.TransferSpreadsheet acLink, _
acSpreadsheetTypeExcel12, _
targetTableName, _
pathToFile, _
True '//This part only if the excel file has field headers
'//import
sql = "SELECT Field1Name, CStr(CustNumber) AS CustNumber, Field3Name INTO NewImportTable FROM " & targetTableName
CurrentDb.Execute sql
'//remove the link
DoCmd.DeleteObject acTable, targetTableName
End Sub
***Two pitfalls of this code to be aware of:
1) You should delete any table with the name "NewImportTable" before running this code, or you'll get a "Table Already Exists" error.
2) If any errors happen in this Sub before you remove the link, you'll have an issue the next time it runs, as it will create a link called "ImportResults1" since "ImportResults" would still exist. The really scary thing is that no errors would be thrown here. It would create "ImportResults1" and then run your sql on "ImportResults"!!
Here is part of the code, where I believe problem is...
URL = "http://www.radiowavemonitor.com/Subscriber/fi_stations_r100.asp?ID=" & JAZZLINK & "&IDF=7"
Set QT = WS.QueryTables.Add( _
Connection:="URL;" & URL, _
Destination:=WS.Range("A1"))
With QT
.RefreshOnFileOpen = True
.Name = "LINK"
.FieldNames = True
.WebSelectionType = xlAllTables
.Refresh BackgroundQuery:=False
End With
When I try line by line with F8 everything is fine and dandy, but when I run entire code it just start to run infinitely (I let it run for 2,3 hours than break it - and no, it doesnt show me where it breaks as I have to force close it!). I think it have problem when it refresh query and it doesnt wait for connection to load and continue performing rest of code and it is what make him go to infinite loop (when I run line by line, everything is OK). So, unless something else is problem, I would like to know how to set query to wait on page to load entirely (Im not too good at this), also, how to set some error checking on link if it is broken or empty to skip on next?
If i loop through 10 documents and fill an excel sheet with data from that documents then sometimes the export is slow and sometimes it is fast.
How is this possible, this is how I export data:
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Visible = True
Call objExcel.Application.Workbooks.Open(CorDos.CorBestandsnaam)
Set xlSheet = objExcel.Application.ActiveWorkbook.Activesheet
and then it fills the cells..
The documents are in a database which is on another server, this server also sometimes has some issues with I/O could that also be the problem?
This is NOT a problem of Lotus Notes but a typical excel- automation problem. Just search for "excel vba slow", and you will find tons of articles how to make this fast again. The fastest way to write something to excel is: create a 2 dimensional array in LotusScript and assign the document values to that array. THEN write the whole array at once. This looks like this:
...
Set dc = db.UnprocessedDocuments
...
Redim varArray( dc.Count - 1, NumberOfFields ) as String
Set doc = dc.getFirstDocument()
While not doc is Nothing
varArray(i , 0 ) = doc.GetitemValue( "FirstField" )(0)
varArray(i , 1 ) = doc.GetitemValue( "SecondField" )(0)
varArray(i , 2 ) = doc.GetitemValue( "ThirdField" )(0)
....
i = i + 1
Set doc = dc.GetNextDocument(doc)
Wend
...
xlSheet.Range( xlSheet.Cells(1,1), xlSheet.Cells(dc.Count,NumberOfFields) ) = varArray
This code is not tested and partly taken from a response to this excel- vba- question, but should show you the way to go.
I like #Torsten-Link's answer, but it might not be the bottleneck you're experiencing. You might want to consider a pattern where the building of the excel file, or at least collating the data to be inserted into it, is done on the actual machine that has the data and then passes the information on in one terse communication. Much of the issues involved with speed are related to local processing and sifting of remote data.
Also, you really should set Visible to false, then make sure it turns true even if it errors. Having that Visible property to True really slows things down.
I'm building a DB for school activities in the afternoon. I'm trying to create a search option through a form that controls a pivot table in which the user can filter on the class type and/or the age group and or the school year.
I wrote this code in VBA and it's not working. When i tried to write a code to filter on only one of the above (such as class type) it worked but when I expanded it to 3 filter options it isn't working. It fails when no value is inserted in one of the options.
search_class.Hide
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").ClearAllFilters
If IsNull(Range("type_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").CurrentPage = "(All)"
Else: ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").CurrentPage = Range("type_search").Value
End If
type_box = "pick a class type"
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").ClearAllFilters
If IsNull(Range("target_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").CurrentPage = "(All)"
Else: ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").CurrentPage = Range("target_search").Value
End If
target_box = "pick a group age"
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").ClearAllFilters
If IsNull(Range("year_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").CurrentPage = "(All)"
Else:
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").CurrentPage = Range("year_search").Value
End If
year_search_box = "pick a school year"
ActiveSheet.PivotTables("PivotSearchClass").PivotCache.Refresh
Does anyone know ehat the problem is and how to fix it?
I think it will work if you change your tests to either:
If IsEmpty(Range("type_search").Value)
or
If Range("type_search").Value = ""
IsNull is used to test whether a variant variable contains a null value, which won't be true with either a blank or filled cell.
I don't have Excel on this computer and pivot tables are my weakness but I'll try to help since you seem alone on this. First make sure your script is indeed running the "True" case of your if statement and then I would edit the value of CurrentPage to see if your issue is there (i.e., change the "(all)" value to something else). Where does the error lie and what is the error description?