SQL Query Run Against Excel Workbook Returns Truncated Text Field - sql

I'm running a SQL SELECT query through an ADO connection to an Excel 2007 workbook with the following code (using a custom version of VBScript)
dim ado, rs
set ado = CreateObject("ADODB.Connection")
ado.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=workbook.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"
ado.open()
set rs = ado.execute("SELECT * FROM [sheet1$]")
which is straighforward. The problem is that any cell that has text longer than 255 characters is truncated; is there any way around this? Is there a property in the connection string that will support this or is it an option I need to change in the excel document itself? I have tried MSSQL's CAST() function but this just causes an error when executed.
Any help would be greatly appreciated.

I think you're running into a variant of a long-standing limitation in Excel's data access provider. See http://support.microsoft.com/default.aspx?scid=kb;EN-US;189897 for an example or google for thousands more.

Instead of trying to use CAST(), have you tried to use the CONVERT() function?

Related

Using the Month function as criteria in an Access SQL query

I'm trying to extract data from an Access 2016 database using SQL and VBA. Below is the code I'm trying to use and every time I run it, I get a "No value given for one or more parameters". I've also shown what I see in the immediate window.
vsql = "SELECT [ResDate],[ResNanme],[ResStart],[ResEnd] FROM [TrainingRoom] where Month([ResDate]) = " & MonNo
Set RecSet1 = Connection.Execute(vsql, dbrows, adCmdText)
Immediate Window:
SELECT [ResDate],[ResNanme],[ResStart],[ResEnd] FROM [TrainingRoom] where(Month([ResDate])) = 11
I don't see anything wrong but I'm sure this is user error. The "MonNo" variable is declared as an integer.
Any suggestions would be greatly appreciated. Thanks for the help.....
I've never used Execute method to open ADO recordset, only Open.
Assuming Connection is a declared and set variable - but it is a reserved word and really should not use as a variable. If code is behind same db pulling data from, could just use CurrentDb with DAO recordset variable and OpenRecordset method. Example that accepts default parameters for optional arguments:
Dim RecSet1 As DAO.Recordset
Set RecSet1 = CurrentDb.OpenRecordset(vsql)

Editing & Running an SQL Query in an Access database from Excel VBA

I am trying to write a Excel VBA macro that runs a query based upon a variable in the spreadsheet. The existing data is half in a large data base (MS Access) on the network.
For some reason when my code runs, nothing is pasted back to my spreadsheet. Can you see why this may be the case:
Sub test()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\\network\data07\version6.mdb;"
cnn.Open ConnectionString
cnn.CommandTimeout = 900
StrQuery = "SELECT dbo_vwData_SelectAll.BusinessDate, , dbo_vwData_SelectAll.Flowdate, dbo_vwData_SelectAll.Bucket FROM dbo_vwData_SelectAll WHERE (((dbo_vwData_SelectAll.Line)=""1.1.1.4"") AND ((dbo_vwData_SelectAll.ReferenceID) Like ""rent*"");"
rst.Open StrQuery, cnn
Sheets("Sheet2").Range("A2").CopyFromRecordset rst
End Sub
The above is a test - I will be changing "rent*" for other variables based on strings in the spreadsheet.
Thanks in advance.
The misappropriated LIKE operator is a known issue in the MS Access SQL dialect. When using MSAccess.exe GUI program, LIKE uses the asterisk wildcard, *. However, using MS Access via ODBC as a backend database as you are with Excel, LIKE uses the ANSI wildcard, %. See MS Office Support docs on Access wildcard character reference.
Therefore, consider the following adjustment which includes use of single quotes:
SELECT dbo_vwData_SelectAll.BusinessDate,
dbo_vwData_SelectAll.Flowdate, dbo_vwData_SelectAll.Bucket
FROM dbo_vwData_SelectAll
WHERE (((dbo_vwData_SelectAll.Line)='1.1.1.4')
AND ((dbo_vwData_SelectAll.ReferenceID) LIKE 'rent%');
To be consistent in either frontend or backend interfaces, consider the ALIKE operator:
AND ((dbo_vwData_SelectAll.ReferenceID) ALIKE 'rent%');

Use DBEngine with Run-time Access

i try to connect my xls with access database. Below code work greate when i have installed full access program on my machine. Problem is when i try tu use it on machine what have only installed Run-time version of access.
I have use this references:
Visual Basic For Applications
Microsoft Excel 14.0 Object Library
OLE Automation
Microsoft Office 14.0 Object Library
Microsoft Forms 2.0 Object Library
When i try to run below code i get error: ActiveX component can't create object or return reference to this object (Error 429)
Sub mcGetPromoFromDB()
Application.ScreenUpdating = False
Dim daoDB As DAO.Database
Dim daoQueryDef As DAO.QueryDef
Dim daoRcd As DAO.Recordset
'Error on line below
Set daoDB = Application.DBEngine.OpenDatabase("K:\DR04\Groups\Functional\DC_Magazyn\Sekcja_Kontroli_Magazynu\Layout\dbDDPiZ.accdb")
Set daoRcd = daoDB.OpenRecordset("kwPromoIDX", dbOpenDynaset)
Dim tempTab() As Variant
For Each Article In collecArticle
daoRcd.FindNext "IDX = " & Article.Index
Article.PromoName = daoRcd.Fields(1).Value
Article.PromoEnd = "T" & Format(daoRcd.Fields(2).Value, "ww", vbMonday, vbUseSystem)
Next
Application.ScreenUpdating = True
End Sub
Is IDX an indexed field?
Is kwPromoIDX optimized for this specific purpose? I mean does it only contain the fields required for this update, or are you pulling extra useless fields? Perhaps something like "SELECT IDX, [Field1Name], [Field2Name] FROM kwPromoIDX" would be more efficient.
Since you are only reading the table records, and don't seem to need to actually edit them instead of dbOpenDynaset, use dbOpenSnapshot.
Just throwing out an idea here, you'd have to test to see if it made any difference, but perhaps you could try to reverse your logic. Loop through the recordset 1 by 1 and locate the IDX within your worksheet.
Another thing I've done in the past is use .CopyFromRecordset and copied the entire recordset into a temporary worksheet and done the juggling back and forth entirely within Excel, to eliminate the back and forth.
Lastly, another approach can be to quickly loop through the entire recordset and populate an array, collection, ... and then work with it instead of Access. This way the data is all virtual and you reduce the back and forth with Access.
You'll need to do some testing to see what works best in your situation.

Updating Excel sheet cell (the cell is a dropdown list)

Basically I have a template excel sheet that I copy to another directory.
I open the connection to said new sheet using:
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & excel_copy & ";Extended Properties=""Excel 8.0;HDR=No;"""
Set rsHoja = cn.OpenSchema(20)
hoja=rsHoja("Table_Name")
rsHoja.close
Set rsHoja = nothing
Set rsHoja = Server.CreateObject("ADODB.Recordset")
From This point on I've been successfully doing updates such as:
sql = " UPDATE [Hoja1$B2:B2] SET F1='"&VALUE&"'"
rsHoja.Open sql, cn
The problem comes when I'm trying to do an update on a cell that takes it's value from a List(that is in the same sheet)
I keep getting an error:
Microsoft Access Database Engine error '80040e10'
Any idea how to assign the value to the cell from the List? I've tried the list's name[cell of the value] and other methods but I'm pretty much trying to figure it out blindly now.
I've also looked everywhere with no luck.
Update 1:
I forgot to mention my file doesn't have headers if that's any help.
As you are using Microsoft.ACE.OLEDB.12.0 database engine for connection,
Possibilities:
1) Make sure you are using Excel 12.0 in Extended Properties if you are using .xlsx file as Data source
2) Make sure you are using Excel 8.0 in Extended Properties if you are using .xls file as Data source
Check this URL for more info on this:
https://www.connectionstrings.com/excel/
So i finally found the problem...It had nohing to do do with my SQL or the cell having a list,the problem was that the cell was marked(in the excel sheet) as a DATE but the List was a String so whenever I tried to write my string on it gave the type error...changing the cell on the excel to string/general fixed the issue,the cell gets the value correctly.

Current Recordset does not support bookmarks

I have this ASP classic code that will return a set of records (recordset):
adoRs.Open "EXEC SP_SelectFromTable", adocn, 1
Its obviously from a Stored Procedure source. Now I use a AbsolutePage property for pagination function but it cause an error:
Error Type: ADODB.Recordset (0x800A0CB3) Current Recordset does not
support bookmarks. This may be a limitation of the provider or of the
selected cursortype.
But when I changed it to a simple select statement like below. It work just fine.
adoRs.Open "SELECT * FROM tblSample", adocn, 1
Any concept I'm missing?
When I first started working with ADO in ASP I ad the same problem. Most of the easy to find documentation mentions setting the cursor type of the recordset object. But on our servers, I actually have to set it on my connection object to get it to work (never really figured out why).
So on my applications I set it on my connection object like this:
adocn.CursorLocation = adUseClient
Then I can set my recordset as:
adoRs.CursorType = adOpenStatic
Once the recordset is opened the datatype of Bookmark will be changed to either Double or Integer according to the recordcount. Just assign the record number in the variable of that type then it will work fine...
Note:-To know type of bookmark use typename(rs.bookmark) after recordset is opened