why this lin not works in ms access ADo recordset? - where-clause

In ADO recordset:
rssv.Open "select * from Q_ReportIsplate1 where ImeBanke Like Banka = '[Forms]![Formom]![Bankom]'", cnn, adOpenDynamic, adLockPessimistic
There is some error!
Help!
I have try with this way and with variable.
Not works!

Related

Excel VBA Run-time error -2147217904 (80040e10)

I have looked at several posts about this error but I am still puzzled about my particular situation. Here is my code that is causing the error:
League = Sheets("GameList").Cells(X, GLeagueCol).Value
SQLStr = "SELECT LeagueID FROM tblLeagues WHERE League = tblLeagues.LTAB"
LRec.Open SQLStr, SptConn, adOpenKeyset, adLockOptimistic
With LRec
.ActiveConnection = SptConn
.Source = SQLStr
.LockType = adLockOptimistic
.CursorType = adOpenDynamic
.Open
End With
If I write the code using a table as the source as below it works fine but then I have to go through the recordset until I find a match.
LRec.Open "tblLeagues", SptConn, adOpenKeyset, adLockOptimistic

How to hold a long calculated string in recordset just by sql query?

I need fetch data from an MS Access database using a SQL query in a VB program.
The result is stored in a recordset, one of the fields is f100, it's calculated by connecting multiple strings.
If the length of f100's value is less than 255, the result is correct, but if it's longer than 255, the result is wrong: f100 just remains 255 characters in length and subsequent characters disappear.
My SQL statement is like this:
Select iif(f1=1,'123','')+iif(f2=1,'456','')+...... as f100 from Table1
My program is like this:
Dim Conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strConn$, strSQL$
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\try.mdb;Persist Security Info=False"
Conn.CursorLocation = adUseClient
Conn.Open strConn
strSQL="Select iif(f1=1,'123','')+iif(f2=1,'456','')+...... as f100 from Table1"
With rst
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.CursorLocation = adUseClient
.Open strSQL, Conn
End With
'here rst.fields("f100") remains a max of 255 char
'debug.print rst.fields("f100")
rst.Close
Conn.Close
Is there any solution that hold a long calculated string in recordset just by SQL query?

ADODB recordset recordcount always returns -1

I am trying to retrieve data to excel form a database in MS access. However the recordcount property for recordset always return -1 though for other purposes the code is working fine.
The code I am using is as follows :
`Sub datarecordset()
Dim cn As adodb.Connection
Dim oRs As adodb.Recordset
Set cn = CreateObject("ADODB.Connection")
DBPath = "C:\[databse path]" & "\[database name].accdb"
dbWs = "[excel sheet name]"
scn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath
dsh = "[" & "[excel sheet name]" & "$]"
cn.Open scn
Dim sSQL As String
Dim F As Integer
sSQL = "Select 'W',a.[Subledger],NULL,sum(a.[Amount]) from GL_Table a where a.[Opex_Group] = 10003 and year(a.[G/L Date]) = " & Year(Sheets("Repairs").Cells(1, 4)) & " and month(a.[G/L Date]) = " & Month(Sheets("Repairs").Cells(1, 4))
sSQL = sSQL & " group by " & "a.[Subledger],(year(a.[G/L Date])),(month(a.[G/L Date]))"
Set oRs = cn.Execute(sSQL)
Debug.Print oRs.RecordCount
oRs.Close
....... further code to print to excel here
cn.Close
End Sub`
The code will fetch data in recordset and write in excel. But since the recordset property is not returning the recordcount so can't print values of various fields in recordset to different cells of excel worksheet.
I searched on google and understood that I need to declare the recordset type and for that I have to use connection.open in place of connection.execute. But I am trying to change the code then it gives the error object variable or With variable not defined.
Any quick help will be welcome. Thanks.
The link by #BitAccesser provides a valid solution. Quick how-to-implement in your situation:
Instead of Set oRs = cn.Execute(sSQL)
Set oRS = CreateObject("ADODB.Recordset")
oRS.CursorLocation = adUseClient
oRS.Open sSQL, cn
ADO's recordcount property returns -1 when ADO cannot determine the number of records or if the provider or cursor type does not support RecordCount. That last one is true for this case.
To avoid this, there are several solutions. The most simple one is to use a client-side cursor, which I just demonstrated, but more alternative solutions are provided in the links by #BitAccesser
You may also specify the CursorType as the third argument to open the RecordSet as follows, which is optional
The first two lines, leaving blank or selecting adOpenDynamic, do not give the record count.
The remaining ones work OK.
1-RS.Open SqlStr, Conn
2-RS.Open SqlStr, Conn, adOpenDynamic
(Erik's solution)
- 3-RS.CursorLocation = adUseClient
Other Options also work fine, please note 4- and 6- which do not require a seperate line
- 4-RS.Open SqlStr, Conn, adOpenKeyset
- 5-RS.Open SqlStr, Conn, adOpenKeyset AND RS.CursorLocation = adUseClient
- 6-RS.Open SqlStr, Conn, adOpenStatic AND RS.CursorLocation = adUseClient
- 7-RS.Open SqlStr, Conn, adOpenStatic
BR, Çağlar
You can still use the Execute method but you need to set the correct cursor type. The recordset is created automatically with cursor type adOpenForwardOnly. This results in oRs.RecordCount = -1. adOpenKeySet is the correct cursor type to correctly show oRs.RecordCount.
Note: The LockType is irrelevant in this case.
Set oRs = cn.Execute(sSQL)
oRs.Close
oRs.CursorType = adOpenKeyset
oRs.Open
Debug.Print oRs.RecordCount
Closing the recordset, changing the cursor type and reopening the recordset worked fine for me (Access 2016 on Windows 7).

Unable to query SQL view

I'm working on an old classic ASP site and have run into a problem. I can query database tables but not views.
This query on a table returns the correct number of rows:
Set rs = Server.CreateObject("ADODB.Recordset")
sqlQuery = "SELECT * FROM tblContent;"
rs.Open sqlQuery, conn, 1, 2
response.write "q1 cnt = " & rs.RecordCount
The same query on a view returns -1:
Set rs = Server.CreateObject("ADODB.Recordset")
sqlQuery = "SELECT * FROM myView;"
rs.Open sqlQuery, conn, 1, 2
response.write "q2 cnt = " & rs.RecordCount
Both queries return the correct number of rows if run from SQL Server Management Studio but only one works when called from a .asp web page. The same seems to apply for all tables and views in the DB (I've tested half a dozen of so of each).
Can anyone offer any clues as to what is going on here?
OK, here's my comment as an answer
This is a workaround, it doesn't explain why the recordcount property doesn't work with views, but you can use an SQL query to count your rows, eg
Set rs = Server.CreateObject("ADODB.Recordset")
sqlQuery = "SELECT COUNT(*) as myrecordcount FROM myView;"
rs.Open sqlQuery, conn, 1, 2
response.write "q2 cnt = " & rs("myrecordcount")
You are requesting editable recordset which may fail on views. Try to open default forward-only read-only recordset
rs.Open sqlQuery, conn, adOpenForwardOnly, adLockReadOnly
or
rs.Open sqlQuery, conn, 0, 1

VBScript: Syntax Error in FROM clause

This is my first visit here.
I need help with connection to access db from vbscript, I will appreciate any clues!
This is my code:
connStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Gregory\Documents\db1.accdb"
set conn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.Recordset")
conn.Open connStr
rs.CursorType = adOpenStatic
rs.Open "SELECT username FROM User", conn
I should also add that there is just one table in the database, called "User" and it's got three fields: username, password and realName.
I believe User is a reserved word in SQL. Try using [ ] around the table name:
select username from [User]