How to Add Data From MS Access 2016 Query to SQL Server Table - sql

Is it possible to transfer Data directly using a query from an Access table to an SQL table ?
I have in mind something like that:
INSERT INTO tableTSQL
SELECT *
FROM tableAccess2016
WHERE condition;

You can use something similar to this:
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Set cnn = New ADODB.Connection
cnn.Open "ODBC;APP=Microsoft Access;DRIVER={SQL Server};SERVER=SQLDEV03;DATABASE=Database_Test;NETWORK=DBMSSOCN;TRUSTED_CONNECTION=Yes;"
Set cmd = New ADODB.Command
cmd.CommandTimeout = 180
cmd.CommandText = strSQL
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdText
cmd.Execute
I am using similar code every day for tables I do not want to link to Access.

Related

SQL in Excel: Cannot call declared variable

I am trying to breakdown my SQL string in Excel VBA, storing my variables in the first part of the SQL, and calling them in the second part. It seems that declared variables is not my friend here?
First part(Declaring my variable)
Dim rst As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim sSQL As String
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.LockType = adLockReadOnly
Set cnn = New ADODB.Connection
cnn.CommandTimeout = GCL_dbTimeout
cnn.Open "XXX"
rst.ActiveConnection = cnn
sSQL = "DECLARE #Id AS INT = 1234 ; SELECT #Id AS [Id] INTO #cte_TEMP;"
cnn.Execute (sSQL)
Second part(Calling my variable)
Using a temp table works:
rst.Open "SELECT * FROM #cte_TEMP;"
But calling the variable doesn't:
rst.Open "SELECT #Id;"
In-memory #variables are only in-scope during the execution of a single command; once rst.Open returns, the variable is gone and no longer exists.
By storing it into a #temp table, you are persisting the value to physical disk storage, which leaves it available to other subsequent commands.
Don't forget to DROP TABLE #cte_TEMP; at one point! :)
Note: #temp tables are only accessible from the user that created it. If you need to access it from a different connection string, you need to use ##temp tables instead.

How to pass SQL query from Excel/vba which contains another query as string

I'm using Excel/vba to send query to SQL Server. The query is something like:
using database
declare #queryString as nvarchar(max)
create table #tempTable (col1 int, col2 int, col3 int)
insert into #tempTable
select top 10 col1, col2, col3
from sometable
set #queryString='select * from #tempTable where col1>10'
exec(#queryString);
If I run this query in SQL Server Management Studio, I get the desired results, but when I run it from Excel/vba, I get an error
Operation is not allowed when the object is closed.
I tried to pass it to SQL Server Management Studio like this (where queryExample is my query):
Dim rs As New ADODB.Recordset
Set Connection = New ADODB.Connection
Connection.ConnectionString = "... connection string ..."
Connection.Open
With rs
.ActiveConnection=Connection
.Open queryExample
End With
and like this
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Set Connection = New ADODB.Connection
cmd.CommandText = queryExample
cmd.CommandType = adCmdText
cmd.ActiveConnection = Connection
Set rsSR = cmd.Execute
but nothing is working, and I always get the same error. I think that problem is in exec() function, because if I pass query like this:
select top 10 col1, col2, col3 from sometable
I don't get any error and recordset is populated with data.
What am I doing wrong? Any help would be appreciated.
Thanks.
EDIT:
I made a mistake when copying vba code for the last example to pass the query to SQL Server Management Studio, the actual code was:
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Set Connection = New ADODB.Connection
Connection.ConnectionString = "... connection string ..."
Connection.Open
cmd.CommandText = queryExample
cmd.CommandType = adCmdText
cmd.ActiveConnection = Connection
Set rsSR = cmd.Execute
Conection was opened in both cases - value of .State of Connection object was 1.

How do I import data from a Teradata table to MS access in an automated way?

I am new to MS Access. I have been trying to import data from a Teradata table to MS Access database. I could establish the connection between the two using VBA. However, I am not being able to write the contents to the access database.
For Excel, we generally use objects like sheets and range to populate the values. What are Access counterparts of these objects?
Given below is the code that I have been using:
Sub TBEN_PR_DSM_SEAS()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim cmdSQLData As ADODB.Command
Set cmdSQLData = New ADODB.Command
Dim query As String
cn.Open "DRIVER={Teradata}; DBCNAME=ABC2; Persist Security Info=True; User ID= ******; Password=******; Session Mode=ANSI;"
Set cmdSQLData.ActiveConnection = cn
query = "SELECT * FROM PRODBBYCIADHOCWRK.TBEN_PR_DSM_SEAS;"
cmdSQLData.CommandText = query
cmdSQLData.CommandType = adCmdText
cmdSQLData.CommandTimeout = 0
Set rs = cmdSQLData.Execute()
End Sub
Can anyone please help me out with the rest of the part? I am using Access 2007-2010.
Thanks and regards,
Nirvik
MS Access is an interesting piece of software as it can serve as both a RDMS database and GUI console to a database. By default, it connects to the Jet/ACE SQL Engine (Windows .dll files) which would compare to SQLite another file-level RDMS. However, with MSAccess.exe Office program, this default can be switched or supplemented with any other ODBC/OLEDB compliant database including the server-level RDMS (Oracle, SQL Server, MySQL, Sybase, even Teradata) using linked tables. And in connecting to external backends it would compare to MySQL's phpmyadmin, SQL Server's Management Studio, PostgreSQL's pgAdmin, and other consoles.
Therefore, consider creating a linked table to Teradata using DoCmd.TransferDatabase where changes reflect on both ends without constant import and export of table data:
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DRIVER={Teradata}; DBCNAME=ABC2; Persist Security Info=True; User ID= ******;" _
& "Password=******; Session Mode=ANSI;", acTable, "TBEN_PR_DSM_SEAS", "NewAccessTable"
And for a static, local copy (which would add redundancy to your application needs) you can run an append or make-table query in Query Window or VBA's DoCmd.RunSQL or CurrentDb.Execute to a local Access table using above linked table.
INSERT INTO NewAccessTable SELECT * FROM [TBEN_PR_DSM_SEAS]
SELECT * INTO NewAccessTable FROM [TBEN_PR_DSM_SEAS]
Insert the data in the recordset into the Access table, using new recordset, Connection and Command objects.
Sub TBEN_PR_DSM_SEAS()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim cmdSQLData As ADODB.Command
Set cmdSQLData = New ADODB.Command
Dim query As String
cn.Open "DRIVER={Teradata}; DBCNAME=ABC2; Persist Security Info=True; User ID= ******; Password=******; Session Mode=ANSI;"
Set cmdSQLData.ActiveConnection = cn
query = "SELECT * FROM PRODBBYCIADHOCWRK.TBEN_PR_DSM_SEAS;"
cmdSQLData.CommandText = query
cmdSQLData.CommandType = adCmdText
cmdSQLData.CommandTimeout = 0
Set rs = cmdSQLData.Execute()
'Up to here is your code.
'Asuming you have a table in Access with identical number of fields, and field names:
dim dRst as dao.Recordset, fld as Variant
set dRst = CurrentDb.("AccessTable")
Do While Not Rs.EOF
dRst.AddNew
For Each fld in dRst.Fields
dRst.Fields(fld.Name) = rs.Fields(fld.Name)
Next
'Update an entire record:
dRst.Update
Rs.MoveNext: Loop
End Sub
Tables are Sheets and Queries select Ranges.
Fields are Columns and Records are Rows.
'Loop through Records
Do Until rs.EOF
'rs.Fields(0) is the first field returned from the Teradata query
'executed in your initial question.
Debug.Print rs.Fields(0)
rs.MoveNext
Loop
'Append to Table
'Have to create Table1 in Access database.
'Table1
'Field Name: Column1
'Data Type: Text
If rs.BOF = False Then rs.MoveFirst 'BOF = Beginning of file
DoCmd.SetWarnings False
Do Until rs.EOF 'EOF = End of file
DoCmd.RunSQL ("INSERT INTO Table1 (Column1) SELECT '" & rs.Fields(0) & "'")
rs.MoveNext
Loop
DoCmd.SetWarnings True
cn.Close
Set cn = Nothing

Reading from hidden Oracle Tables with VBA ADODB

I'm trying to read from different Tables on a Oracle Database using VBA and Excel.
Usually when read the tables I run something like:
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
Dim query As String: query = "SELECT * FROM OBJ_NAME"
rs.Open query, con
However, this does not work for all the tables. Using SQL Developer i usually run at the beginning:
exec session#.open_session();
After which I can read any table. Is there a way to run this command with an ADODB.Recordset in the beginning too? Just replacing the SQL Query with the command did not work.
Or is there different way to read this 'hidden' Tables?
Try something like that:
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
Dim query As String: query = "begin session#.open_session; end;"
With rs
.ActiveConnection = con
.Open query, con
End With
Set connectToDB = con
This should work using VBA. Let me if it worked :)

Get last UserID from SqlServer to textbox control

I have a table customers where each cust has UserID as "A000" now I need to get the last entered ID from the database and display it in my textbox.
Can anyone suggest me how do I do this?
As I have seen many articles describing about
SELECT ##IDENTITY
SELECT SCOPE_IDENTITY()
SELECT IDENT_CURRENT('TableName')
but unable to know where to use it correctly.
And here is how I'm doing it :
Dim strConnection As String = "Data Source=.\SqlExpress;Initial Catalog=Subscription;Integrated Security=True"
'Establish SQL Connection
Dim con As New SqlConnection(strConnection)
'Open database connection to connect to SQL Server
con.Open()
'Data table is used to bind the resultant data
Dim dtusers As New DataTable()
'Create a new data adapter based on the specified query.
Dim da As New SqlDataAdapter("SELECT MAX(UserID) FROM Customers", con)
Dim cmd As New SqlCommandBuilder(da)
da.Fill(dtusers)
con.Close()
Use ExecuteScalar :
Dim comm as new SqlCommand
comm.CommandText = "SELECT MAX(UserID) FROM Customers"
comm.Connection = con
Dim MaxUserID as object = comm.ExecuteScalar()
Use the ExecuteScalar method to retrieve a single value (for example,
an aggregate value) from a database
Side Note : ExecuteScalar() may return a null reference (Nothing in VB.NET) if the result of the command is empty like when there are no records in the table or there is condition that doesn't produce any records. Make sure you check that before assigning the value to your TextBox.