Microsoft Access Attachment Recordset [duplicate] - vba

This question already has an answer here:
Access with breaking changes on Version 2206 (Recordset2.Fields returns DAO.Field3 instead of DAO.Field2) - Error 13
(1 answer)
Closed 7 months ago.
I have had a Microsoft Access application working for many years. However today the following code has stopped working.
Dim Recordset2 As DAO.Recordset2
Dim fieldAttachment As DAO.Field2
stringSQLText = "SELECT [" & stringTable & "].ID" & vbCrLf
stringSQLText = stringSQLText & " , [" & stringTable & "].[" & stringFieldName & "]" & vbCrLf
stringSQLText = stringSQLText & " FROM [" & stringTable & "]" & vbCrLf
stringSQLText = stringSQLText & " WHERE ((([" & stringTable & "].ID)=" & longID & "));"
Set Recordset2 = CurrentDatabase.OpenRecordset(stringSQLText, dbOpenSnapshot)
'Error occurs here
Set fieldAttachment = Recordset2(stringFieldName)
It reports the following error:
Error 13 Type mismatch
Could this be a result of an Office Update?
Has anybody else come across this problem?
My Access Version is Microsoft® Access® for Microsoft 365 MSO (Version 2206 Build 16.0.15330.20216) 64-bit

Changed the code to use Field3 instead of Field2 and now works.

Related

No value given for one or more required parameters error on one machine but not another

I have VBA code working in Excel on my machine but it is giving the above error on someone else's machine. What would be causing it to work for me but not for someone else?
'---Connecting to the Data Source---
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & wb.Path & "\" & wb.Name & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
'---Run the SQL SELECT Query---
sql = "SELECT " & _
"sum([" & cash_projections.Name & "$].[Projected Ending Balance]), " & _
"[" & cash_projections.Name & "$].[Account Name (Balance)], " & _
"[" & cash_projections.Name & "$].[Projected Date] " & _
"FROM " & _
"[" & cash_projections.Name & "$] " & _
"WHERE " & _
"[" & cash_projections.Name & "$].[Projected Ending Balance] > 100 " & _
"GROUP BY " & _
"[" & cash_projections.Name & "$].[Account Name (Balance)], " & _
"[" & cash_projections.Name & "$].[Projected Date] " & _
"ORDER BY " & _
"[" & cash_projections.Name & "$].[Account Name (Balance)], " & _
"[" & cash_projections.Name & "$].[Projected Date] "
Set rs = cn.Execute(sql)
The error shows up on the last line of this code. Again, it works just fine for me, but gives the error on another machine.
Please note that I am using the exact same workbook as is being used on the other computer. Here are the column headings
The line that is causing the error is the one that is executing your query. Because the code does not fail when you open the connection, it means it is finding the workbook you are querying just fine. My guess is that the workbook it finds has a different structure on the two machines. Because it says a parameter is missing a value, it means that something that is defined on your machine is missing on the other, it could be a table name or a column name. Basically, when you use that name on your machine it finds it and moves on. On the other machine, it can't find it so it assumes it must be parameter.

SQL code won't run when CASE function is inserted?

I am trying to fit a CASE function into some SQL code and can't get it to run. Everything has worked perfectly up until the CASE function, and whenever that is inserted, I get a debug error. I was assisted in making this program so I'm hoping it's just a simple error that I'm ignorant of. This is pulling select content from 2 large excel tables based on meeting the criteria. The CASE function is determining whether a value is > or < 12000 and pumping out content based on which content matches that criteria. THANKS!!
See code below:
Dim fPath As String
Dim oConn As New ADODB.Connection
Dim oRS As New ADODB.Recordset
Dim sPath As String, rng1 As Range, rng2 As Range, SQL As String
fPath = ThisWorkbook.FullName 'workbook must be saved somewhere...
'connect to the workbook on disk
oConn.Open "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & _
"DBQ=" & fPath & ";"
'select some records which match between lists (zipccode and subdivision)
' and with the entered criteria (price, SQFT)
SQL = " select n.*, o.* from [OldList$] o, [NewList$] n " & _
" where n.zipcode = o.zipcode and left(n.Subdivision, 6) = left(o.Subdivision, 6)" & _
" and (n.listprice - o.listprice) between " & shtCriteria.Range("C4").Value & " and " & shtCriteria.Range("D4").Value & _
" and (n.sqft - o.sqft) between " & shtCriteria.Range("C6").Value & " and " & shtCriteria.Range("D6").Value & _
" and CASE " & _
" WHEN n.lot > 12000 " & _
" THEN o.lot between n.lot + n.lot* " & shtCriteria.Range("C8").Value & " and n.lot + n.lot* " & shtCriteria.Range("D8").Value & " " & _
" ELSE (n.lot - o.lot) between " & shtCriteria.Range("C9").Value & " and " & shtCriteria.Range("D9").Value & _
" END "
Set oRS = oConn.Execute(SQL) 'run the query
CASE returns different results of the same type depending on conditions. It’s not for executing alternate code blocks as you have attempted.
Remove CASE and use OR and AND.
You have used it like this:
WHERE …
AND CASE
WHEN n.lot > 12000
THEN <condition 1>
ELSE <condition 2>
END
Change it to:
WHERE …
AND (
(n.lot > 12000 AND <condition 1>)
OR
(n.lot <= 12000 AND <condition 2>)
)

Access VBA Docmd.OpenQuery Can Run an update query but Currentdb.Excecute Cannot

I created a saved update query as below, which has control values and IIf function.
UPDATE SYS_AAAA_AAAH
SET SYS_AAAA_AAAH.AAK = AAA & " " & AAB & IIf(IsNull(AAC),"","(" & AAC & ")") & IIf(IsNull(AAF),""," not null") & " comment '" & AAH & "',"
WHERE (((SYS_AAAA_AAAH.AAO)=[forms]![frmAdmiTabl]![CombSAAO]));
DoCmd.OpenQuery can run it while Currentdb.Execute gives an error message 'too few parameters'. I created another saved update query without input from control or function and Currentdb.Execute worked. I don't want to see the warning message from Docmd.OpenQuery and I dont want to mess around by turning on and off the warning. Anyway of getting Currentdb.Execute work on this?
When you want to update a column of some certain records with another column's value and IIf function, it is better to use DAO.recordset edit and update
Dim Rs_AAAH As DAO.Recordset
Set Rs_AAAH = CurrentDb.OpenRecordset("select * from Table where AAO='" & Me.CombSAAO.Value & "'", dbOpenDynaset)
Rs_AAAH.MoveFirst
Do Until Rs_AAAH.EOF
With Rs_AAAH
.Edit
.Fields("AAK").Value = Rs_AAAH.Fields("AAA") & " " & Rs_AAAH.Fields("AAB") & IIf(IsNull(Rs_AAAH.Fields("AAC")), "", "(" & Rs_AAAH.Fields("AAC") & ")") & IIf(IsNull(Rs_AAAH.Fields("AAF")), "", " not null") & " comment '" & Rs_AAAH.Fields("AAH") & "',"
.Update
End With
Rs_AAAH.MoveNext
Loop
Rs_AAAH.Close
Set Rs_AAAH = Nothing

SQL command terminates before reaching end of large csv file

I have a large csv file with lots of data that I need to be able to analysis (~6M rows). I want to connect to the file and run SQL command against it to return only the data I'm interested in analysing. The VBA I'm writing is in Excel 2010.
Everything works fine when the number of rows in the csv file is < 4432669. When the csv file has more rows than this, the command seem to terminate at that point in the file and just returns what ever it has found up to that point. No Error is thrown (CN.Errors), I first though it might be that the command timedout but when I increase this it made no difference. I also checked with different csv files just incase that row contained corrupted data, but no luck. Recordset maxrecords is set to 0 (No limit).
I've tried using Microsoft.Jet.OLEDB.4.0; and driver={Microsoft Text Driver (*.txt; *.csv)}; in the connectionstring, both behave the same as described above.
Here is test code I'm using,
Dim CN As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim Err As ADODB.Error
providerstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\cygwin\home\MarkM\csvimport\filtertest4\;" & _
"Extended Properties=" & Chr(34) & "text;HDR=Yes;FMT=Delimited" & Chr(34) & ";"
CN.ConnectionString = providerstr
CN.Mode = adModeRead
CN.CommandTimeout = 900
CN.Open
RS.Open "SELECT exCode FROM 5M_MBP1R04.csv", CN, adOpenStatic, adLockReadOnly
RS.MoveLast
MsgBox "Number of rows = " & RS.RecordCount
For Each Err In CN.Errors
strError = "Error #" & Err.Number & vbCr & _
" " & Err.Description & vbCr & _
" (Source: " & Err.Source & ")" & vbCr & _
" (SQL State: " & Err.SqlState & ")" & vbCr & _
" (NativeError: " & Err.NativeError & ")" & vbCr
If Err.HelpFile = "" Then
strError = strError & " No Help file available"
Else
strError = strError & _
" (HelpFile: " & Err.HelpFile & ")" & vbCr & _
" (HelpContext: " & Err.HelpContext & ")" & _
vbCr & vbCr
End If
Debug.Print strError
Next
Really appreciate any help as I'm completely stuck now.
BR's Mark.
Perhaps you are exceeding a memory constraint due to the CursorType. Try changing it to adOpenForwardOnly
Here is the MSDN page describing Cursor Types.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681771(v=vs.85).aspx

Why do brackets in SQL around field name not work in Access VBA ADO?

In MS Access QBE if I paste the following SQL, it works correctly and I get 2 records back-
SELECT [tmp_binning].[bn_faibash] FROM [tmp_binning] WHERE key2='0210043-HOU-STOR' ORDER BY [tmp_binning].[bn_faibash];
But if I programmatically run the same query in VBA from an ADO object I get (incorrectly) no records. If I change the SQL to remove brackets around the field name, it does correctly return the 2 records in VBA ADO.
SELECT [tmp_binning].bn_faibash FROM [tmp_binning] WHERE key2='0210043-HOU-STOR' ORDER BY [tmp_binning].bn_faibash;
I've been unsuccessful googling to figure why this happens on my own, can anyone tell me why?
Thanks.
First, the brackets aren't required, either in the in Access UI or via ADO. Simply omit them in all environments and the problem should go away. (If it is the Access QBE thing that is adding the brackets then consider another tool or hand crafting your SQL code!)
Second, even with the brackets I can't reproduce the error using your SQL code e.g.
Sub gjskdjs()
On Error Resume Next
Kill Environ$("temp") & "\DropMe.mdb"
On Error GoTo 0
Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat
.Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
Environ$("temp") & "\DropMe.mdb"
With .ActiveConnection
Dim Sql As String
Sql = _
"CREATE TABLE tmp_binning" & vbCr & "(" & vbCr & " bn_faibash VARCHAR(255)," & _
" " & vbCr & " key2 VARCHAR(255)" & vbCr & ");"
.Execute Sql
Sql = _
"INSERT INTO tmp_binning (bn_faibash, key2)" & _
" VALUES ('002', '0210043-HOU-STOR');"
.Execute Sql
Sql = _
"INSERT INTO tmp_binning (bn_faibash, key2)" & _
" VALUES ('001', '0210043-HOU-STOR');"
.Execute Sql
Sql = _
"SELECT [tmp_binning].bn_faibash " & vbCr & " FROM" & _
" [tmp_binning] " & vbCr & " WHERE key2 = '0210043-HOU-STOR'" & _
" " & vbCr & " ORDER " & vbCr & " BY [tmp_binning].bn_faibash;"
Dim rs
Set rs = .Execute(Sql)
MsgBox rs.GetString
End With
Set .ActiveConnection = Nothing
End With
End Sub
Consider posting your schema as SQL DDL with sample data.