VB.NET can't use EOF and BOF with ADODB recordset - vb.net

In VB.net , EOF and BOD not working with ADODB recordset, even I added the right reference
DIM rsReqs as New ADODB.recordset
DIM cn as ADODB.connection
after open connection, with SQL Query I tryed to do this
rsReqs.open(StrSQL,cn)
Do until rsreqs.EOF
....
....
Thank's for your help

You can't use that libraries on vb.net, find the libraries based on your database server. Find more about how to connect your database (like mysql or access) with vb.net by googling.

Related

Populate results from SQL Server using Recordset in MS Access using VBA code

I am not sure how I can put the results from Record Set into a query or result pane in MS Access using VBA code. The results in the recordset are from SQL Server, so I want to display the results in MS Access. I need to do it this way, is that possible? I would think I need to do something where are x's are.
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConnString As String
strConnString = "Provider=SQLNCLI11;Server=SRV;Database=Staging;Trusted_Connection=yes;"
Set conn = New ADODB.Connection
conn.Open strConnString
Set rs = conn.Execute("Select * from MSAccess_APP_ComplianceDashBoard ")
XXXXXXXXXXXXXXXXXX
rs.Close
Assuming a continues form,or a multiple items form?
In the forms on-load event just go:
me.ReocrdSource = "Select * from MSAccess_APP_ComplianceDashBoard"
It is of course assumed that you have a linked table to SQL server with the above name.
In fact, you can set the forms record source in design mode and as a result you need zero lines of code.
So, setup a linked table - you not need to write any code.
Create a pass-through query, put your connection string there, and do whatever you your needs over this query.

Link PostgreSQL to MS Access using ADO library via ODBC driver (DSN-less)

Hello :) I've searched Stackoverflow and Google for a lot of hours and would like to have a clear answer to my problem, because I got a little bit confused. I want to link a table from my PostgreSQL database to my MS Access frontend using VBA. Therefore I'm using the ADO library and installed the 32 bit ODBC driver for PostgreSQL. It works great if I use a connection string with a defined DSN, code looks like this:
Set conn = New ADODB.Connection
Dim strConnect As String
strConnect = "DSN=PostgreSQL35W;Database=...;UID=...;PWD=..."
conn.Open
But the thing is that I dont wan't to use a connection string with DSN, because I dont want to mess around with these DSNs, esspecially not in a distributed environment. Therefore I would like to use a DSN-less connection string. But it seems like that the ADO library doesn't support DSN-less connection strings while using the ODBC driver for PostgreSQL. Is that true?
Here's the code I used for the DSN-less connection:
Set conn = New ADODB.Connection
Dim strConnect As String
strConnect = "ODBC;Driver={PostgreSQL Unicode};Server=...;Port=5432;Database=...;UID=...;PWD=..;TABLE=...
I know there are a lot of threads about that topic, but I couldn't get a clear answer for it. So thank you very much in advance!

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%');

Connect to a linked table

Connect to a linked table with code.
I have some linked tables from a SQL-server; they are linked with an ODBC connection. The password is not saved with the connection. When I am double clicking on the table in Access table-view I get a prompt for username and password. After entering the password I can view the data in the table.
My problem is when I try to access the table with code before having opened it in this way. What I try to do is to use ADODB to open a recordset with data from the linked table, like:
Dim rst as new ADODB.Recordset
Dim sql as string
Sql = “SELECT * FROM LinkedTable”
rst.Open sql, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
Running this code without having access the table before will generate this error: Error# -2147467259, ODBC: connection to dns-name failed.
So, my question is, are there any way to connect to the database with code that can be run when the database is opened? This would also help the users as they would not have to remember a password to the SQL-server.
It seems that you are mixing 2 technologies that might not work together, ie linked tables through ODBC and ADODB recordsets. Have you tried to open DAO recordsets on your linked tables?
Dim rst as DAO.Recordset
Dim sql as string
Sql = “SELECT * FROM LinkedTable”
set rst = currentDb.openRecordset(sql,<your parameters>)
You could of course use ADODB recordsets through 2 ADODB connections, one to your access file, the other one to your SQL server:
Dim rsSQL as ADODB.recordset, _
rsACCESS as ADODB.recordset, _
connectionSQL as ADODB.connection, _
connectionACCESS as ADODB.connection
set connectionSQL = New ADODB.connection
set connectionACCESS = New ADODB.connection
connectionSQL.properties(...) = enumerate your SQL parameters
connectionACCESS.properties(...) = enumerate your ACCESS parameters (use currentproject.accessConnection if your access tables are local tables only)
set rsSQl = New ADODB.recordset
set rsACCESS = New ADODB.recordset
rsSQL.open "SELECT * FROM ...", connectionSQL, <other parameters>
rsACCESS.open "SELECT * FROM ...", connectionACCESS, <other parameters>
Linking ADO recordsets to forms and comboboxes in Access is possible. But, when creating forms, this technology has to be mainly managed through VBA code (you will have to write 'on open' events such as set me.recorset = ...), while the standard "linked tables" technology can be easily used through the user-friendly 'form-design' interface.
You can use a connection string in your code, it is easy enough, seeing you are already using ADO: http://www.connectionstrings.com/
You will need to find out which version of SQL Server you are linking to.

Access remote sql server using VB.NET

I've found a few examples of using vb.net to access an sql database, so far none of them have worked . They all involve using DataReaders. Maybe its the fact that the sql db is not on the same machine as the application.
I was just wondering if anyone had a more comprehensive example of using VB.NET to access a remote sql server.
Thanks!
EDIT:
I've received a few helpful comments an replies already. So far my connection string looks like:
"server=sqlblah.myhost.com;uid=myuser;pwd=pass;database=testdb"
Probably also good to mention their is no editing of the tables a this point, just reading.
Check out the SQLClient class.
One convienent way to access a SQL database is to fill a DataSet object with query data with a DataAdapter object.
Dim sSQL As String = "SELECT * FROM ???"
Dim conn As New SqlClient.SqlConnection("connection string")
Dim da As New SqlClient.DataAdapter(sSQL, conn)
Dim ds As New DataSet
da.Fill(ds, "TABLE NAME")
You can then access the "TABLE NAME" table in the DataSet object.
The "connection string" is obviously your SQL connection string.
Use the sSQL string to query as necessary.
Quick side note - a helpful tool for creating connection strings:-
Open up a text document (notepad, wordpad etc) and save a blank document with the extension ".UDL".
This will give you a "Data link Properties" mini app.
Open up the app and change the provider in the "Provider" to whichever provider you need (in this case OLE DB Provider for SQL Server).
You then need to build up the connection in the connection tab.
Once you have chosen the criteria (ServerName (the drop down list will show you all visible Servers), Security permissions,Database (this drop down list will be populated based on the server chosen)) you can test your connection (to make sure you have permissions etc).
Click ok to close the App, rename the file to have a ".txt" extension and re-open in a text editor, hey presto, one built up connection string (as below).
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=YOURDBNAME;Data Source=YOURSERVERNAME