Fastest way to move data from Oracle to MS Access via VBScript - ms-access

Normally, I would just loop through using Recordset.AddNew, but this is painfully slow when dealing with large recordsets -- is there a better way to do it? I was hoping there would be a way to simply write an insert statement to go from Oracle to Access, perhaps by using two ADO connects, but the only examples I can find are in VB.net (using OleDbCommand), which is sadly not an option. Is this simply a limitation of VBScript?
Thanks!

Your question tags include access-vba, so I'll suggest the DoCmd.TransferDatabase Method.
This example, near the bottom of the page, is to create an ODBC link to a SQL Server table. It assumes a DSN (data source name) named DataSource1. The link name in Access will be Authors, and the server source table is named dboAuthors.
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DSN=DataSource1;UID=User2;PWD=www;LANGUAGE=us_english;" _
& "DATABASE=pubs", acTable, "Authors", "dboAuthors"
You can adapt that for Oracle with an appropriate DSN. And if you want to import rather than link, substitute acImport for acLink.
And actually, you might not even need code for this. With a working DSN, you can import (or link) via the Access user interface.
Your title mentioned VBScript. Tell us if you actually must use VBScript instead of VBA.
After reading your comments, I think you would be better of with a different approach.
Create a database with an ODBC link to the Oracle table.
Then export subsets of that table data to your individual database files.
SELECT master_link.* INTO local_table IN 'c:\somefolder\db1.mdb'
FROM master_link
WHERE location = 'location1';
Then adjust the SQL for each target db file and data selection criterion. It should be easy to drive that from a simple VBA procedure.

Here are a few notes for VBScript using SQL Server, rather than Oracle, however, it should be possible to use something very similar.
Const CreatePath = "Z:\docs\"
Const Provider = "Microsoft.ACE.OLEDB.12.0;"
''The easiest way to get this line is from the connect property of
''a linked table
Const ServerInLine = "[ODBC;DRIVER=SQL Server;SERVER=server;Trusted_Connection=Yes;DATABASE=db]"
''Change this to the Oracle connection
Const ServerConnect = "Provider=sqloledb;Data Source=server;Initial Catalog=db;Integrated Security=SSPI;"
Dim cnSS ''As ADODB.Connection
Dim cnMSA ''As ADODB.Connection
Dim cat ''As New ADOX.Catalog
Set cat = CreateObject("ADOX.Catalog")
Set cnSS = CreateObject("ADODB.Connection")
Set cnMSA = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnSS.Open ServerConnect
''Only the locations that exist. Assuming a location table
''with a description, Location
sSQL = "SELECT DISTINCT a.LocationID, b.Location FROM ATable a "
sSQL = sSQL & "INNER JOIN Location b ON a.LocationID = b.ID"
rs.Open sSQL, cnSS
Do While Not rs.EOF
DBName = CreatePath & Trim(rs.Fields("Location")) & ".accdb"
scn = "Provider=" & Provider & ";Data Source=" & DBName
cat.Create scn
cnMSA.Open scn
sSQL = "SELECT * INTO " & Replace(Trim(rs.Fields("Location")), " ", "")
sSQL = sSQL & " FROM " & ServerInLine & ".ATable"
sSQL = sSQL & " WHERE LocationID=" & rs.Fields("LocationID")
cnMSA.Execute sSQL, recs
sMsg = sMsg & vbCrLf & DBName & " created " & recs
rs.MoveNext
cnMSA.Close
Loop
MsgBox sMsg

Related

Avoid new line seperators in mySQL query within VBA code

I have the below VBA to extract data from the database:
Sub Get_Data_from_DWH ()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim dateVar As Date
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=XX.XXX.XXX.XX; DATABASE=bi; UID=testuser; PWD=test; OPTION=3"
conn.Open
strSQL = " SELECT" & _
" product, brand, sales_channel," & _
" country, sales_manager, sales_date, return_date, " & _
" process_type, sale_quantity, return_quantity " & _
" FROM bi.sales" & _
" WHERE sales_date BETWEEN '2020-01-01' AND '2020-06-30' " & _
" AND country IN ('DE', 'US', 'NL') " & _
" ORDER BY FIELD (brand, 'brand_A', 'brand_B', 'brand_C');"
Set rs = New ADODB.Recordset
rs.Open strSQL, conn, adOpenStatic
Sheet1.Range("A1").CopyFromRecordset rs
rs.Close
conn.Close
End Sub
This VBA extracts the data based on the SQL without any problem.
However, in my original file the VBA is much bigger and therefore I have to use a lot of new line separators " and " & _ .
This makes the handling of the SQL within the VBA very difficult since the structure is quite confusing.
Therefore I am wondering if there is an alternative that allows you to enter the SQL without the new line separators.
Something like this:
strSQL = " SELECT
product, brand, sales_channel,
country, sales_manager, sales_date, return_date,
process_type, sale_quantity, return_quantity
FROM bi.sales
WHERE sales_date BETWEEN '2020-01-01' AND '2020-06-30'
AND country IN ('DE', 'US', 'NL')
ORDER BY FIELD (brand, 'brand_A', 'brand_B', 'brand_C'); "
Do you have any idea if this is possible?
Definitive answer - It's not possible in VBA, there's no way for the IDE to allow line breaks in a string.
But here's some suggestions:
If you can't create views on the database and just use those, maybe you could directly link to the data source and do queries that way? So you don't have to use VBA to write queries - Excel has great in-built connectivity and you get a MySQL library, just google "connect to mysql database in excel"
If you really want your queries defined as text somewhere else, one thing that might help to neaten your code is to load your strings from another place where you can keep your sql queries in a format you prefer.
You could load them from a VBA module, which would still have split strings but you can call them like StringSQL = Module1.GetQueryOne and it helps keep the execution code and the SQL strings separate
You could create a method to pull queries from worksheet cells, or maybe keep them in textboxes on a worksheet, then you can use
SqlString = ThisWorkbook.Sheets("Sheet1").Shapes("SqlQuery1").OLEFormat.Object.Text
or even keep them in a text file that accompanies your Excel workbook and load them from there.

Calling a UDF (Sql server) from VB Access "Undefined function <function name> in expression"

I am trying to call a udf (SQL server) from Vb code in access. Connection to DB was successful and I am able to run queries on SQL server tables. However, when I try to call the UDF, it throws me an error saying undefined function.
Please see the code below:
Private Sub cmd_Login_Click()
' some code here
Set db = CurrentDb()
sSQL = "SELECT UserID FROM TBL_User_Login WHERE UserName = '" & cbo_User & "' AND Status = 0"
Set recset = db.OpenRecordset(sSQL)
recset.Close
Set rectset = Nothing
sSQL = "SELECT fn_validate_user(" & gb_UserId & ",'" & Hash(Me.txt_Password + cbo_User) & "') AS PasswordValid"
Set recset = db.OpenRecordset(sSQL) ' this is where i get error for undefined function fn_validate_user
PasswordValid = recset("PasswordValid")
Can someone see if I am missing something here.
When you run a standard query in Access it is first processed by the Access Database Engine, even if that query refers to ODBC linked tables. Access can recognize Access user-defined functions (created with VBA) but it is not aware of SQL Server user-defined functions.
In order to use a SQL Server user-defined function you need to use a pass-through query. As the name suggests, it bypasses the Access Database Engine and sends the query directly to the remote database (via ODBC). The VBA code to do that would look something like this:
Dim db As DAO.Database, qdf As DAO.QueryDef, recset As DAO.Recordset
Dim sSQL As String, PasswordValid As Boolean
Set db = CurrentDb
sSQL = "SELECT fn_validate_user(" & gb_UserId & ",'" & Hash(Me.txt_Password + cbo_User) & "') AS PasswordValid"
Set qdf = db.CreateQueryDef("")
' get .Connect property from existing ODBC linked table
qdf.Connect = db.TableDefs("TBL_User_Login").Connect
qdf.ReturnsRecords = True
qdf.SQL = sSQL
Set recset = qdf.OpenRecordset(dbOpenSnapshot)
PasswordValid = recset.Fields("PasswordValid").Value
recset.Close
Set recset = Nothing
Set qdf = Nothing

MS Access Passthrough Query Update

I am trying to make an Update to a Passthrough query using MS Access to an ODBC server that I have no control over. The reason I have to use a Passthrough is that the records I am accessing have more than 255 fields (I would use a linked table if I could).
I've been using this resource to get the data using Passthrough (http://www.techonthenet.com/access/tutorials/passthrough/basics09.php)
The query is simply: SELECT FullName, PointNumber FROM DNP3.CDNP3AnalogIn
The ODBC Connect Str is: ODBC;DSN=SCX6_DB;LOCATION=Main;UID=admin;PWD=password;LOCALTIME=False;
Now inside an Access Database I have a table (SCADA DB Tags) with same name for the Fields (FullName, PointNumber), and I want to update the fields inside the ODBC Database using an Update Passthrough query, but I am unsure how to do this.
I saved the previous Query as DNP3_CDNP3AnalogIn Query, and tried to make a new Query:
UPDATE [DNP3_CDNP3AnalogIn Query] INNER JOIN [SCADA DB Tags] ON
[DNP3_CDNP3AnalogInQuery].FullName = [SCADA DB Tags].FullName
SET [DNP3_CDNP3AnalogIn Query].[PointNumber] = [SCADA DB Tags].[PointNumber];
But I get an error from Access: Operation must use an updateable query.
I know there is someway to do this but I can't seem to find an example (I might not be googling the correct phrase). Microsoft page (http://technet.microsoft.com/en-us/library/bb188204%28v=sql.90%29.aspx) says: There is, however, one important limitation: the results returned by SQL pass-through queries are always read-only. If you want to enable users to perform updates based on the data retrieved, you must write code to handle this. Unfortunately it doesn't give an example to do it!
Can anyone give me a solution, I can use VBA if required? I can also give more background if required. Unfortunately I'm not an expert in Access, I'm just trying to come up with an automated solution that could save me some time.
When they said that "If you want to enable users to perform updates based on the data retrieved [from a pass-through query], you must write code to handle this" they probably meant something like this:
Option Compare Database
Option Explicit
Public Sub UpdateSqlServer()
Dim cdb As DAO.Database, rst As DAO.Recordset
Dim con As Object ' ADODB.Connection
Dim cmd As Object ' ADODB.Command
Const adParamInput = 1
Const adInteger = 3
Const adVarWChar = 202
Set cdb = CurrentDb
Set rst = cdb.OpenRecordset( _
"SELECT " & _
"[SCADA DB Tags].FullName, " & _
"[SCADA DB Tags].PointNumber " & _
"FROM " & _
"[DNP3_CDNP3AnalogIn Query] " & _
"INNER JOIN " & _
"[SCADA DB Tags] " & _
"ON [DNP3_CDNP3AnalogIn Query].FullName = [SCADA DB Tags].FullName", _
dbOpenSnapshot)
Set con = CreateObject("ADODB.Connection")
con.Open "DSN=SCX6_DB;"
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandText = _
"UPDATE DNP3.CDNP3AnalogIn SET " & _
"PointNumber=? " & _
"WHERE FullName=?"
cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput) ' PointNumber
cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255) ' FullName
cmd.Prepared = True
Do Until rst.EOF
cmd.Parameters(0).Value = rst!PointNumber
cmd.Parameters(1).Value = rst!FullName
cmd.Execute
rst.MoveNext
Loop
Set cmd = Nothing
con.Close
Set con = Nothing
rst.Close
Set rst = Nothing
Set cdb = Nothing
End Sub
Notes:
The code uses your existing ODBC DNS.
It uses a Prepared Statement to perform the updates, increasing efficiency and protecting against failures related to SQL Injection.
The source Recordset performs an INNER JOIN on the pass-through query to ensure that the code only tries to update rows on the server that actually exist on the server.
Are you saying that [DNP3_CDNP3AnalogIn Query] is server side based and that table [SCADA DB Tags] is local based? In that case you cannot use a pass-through query.
However since the tables ARE in different locations pass-though cannot touch BOTH at the same time.
You can however execute "single" server side (pass-though) in a loop. If you setup a pass-though query and SAVE it, then this code will work:
Dim qdfPass As DAO.QueryDef
Dim rstLocal As DAO.Recordset
Dim strSQL As String
Dim strSQL2 As String
Set qdfPass = CurrentDb.QueryDefs("MyPass")
strSQL = "UPDATE [DNP3_CDNP3AnalogIn Query] " & _
"SET [DNP3_CDNP3AnalogIn Query].[PointNumber] = 'xxxx' " & _
"WHERE [DNP3_CDNP3AnalogInQuery].FullName = 'zzzz' "
Set rstLocal = CurrentDb.OpenRecordset("[SCADA DB Tags]")
Do While rstLocal.EOF = False
strSQL2 = Replace(strSQL, "xxxx", rstLocal!PointNumber)
strSQL2 = Replace(strSQL2, "zzzz", rstLocal!FullName)
qdfPass.SQL = strSQL2
qdfPass.Execute
rstLocal.MoveNext
Loop
rstLocal.Close

How do I delete rows in a table from one database to the other in MS Access

I have two databases, a "Master" source (held locally) and a "Copy" database (to be distributed). Neither will be able to see each other once distributed (either locally or across a network), so we can't perform queries across the databases after distribution. I need to remove some content from the Copy database before it is distributed, so I decided to create a VBA script to produce the Copy databases for distribution.
There are lookups in the tables, so I have decided to hold a template database (which was copied from the master source, and then stripped the tables out of them), then am dropping the tables and have recreated them in an appropriate order.
I'm now needing to remove some of the data, and I'm struggling.
DeviceTable:
AutoNumber(ID)
Text(DeviceName)
Integer(ClusterID)
Text(Distribution)
ClusterTable:
AutoNumber(ID)
Text(ClusterName)
VirtualSystemTable:
AutoNumber(ID)
Text(VirtualSystemName)
Integer(ClusterID)
Integer(DeviceID)
InterfaceTable:
AutoNumber(ID)
Integer(VirtualSystemID)
Integer(ClusterID)
Integer(DeviceID)
Text(Description)
I need to remove entries from DeviceTable, ClusterTable, VirtualSystemTable and InterfaceTable for anything which is not marked as Distribution: "Public"
Normally I would do (in psudocode):
arrDEV = SQL("SELECT ID, ClusterID FROM DeviceTable WHERE Distribution<>"Public"")
Then, for each response, I would
arrVSYS = SQL("SELECT ID FROM VirtualSystemTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID")
SQL("DELETE FROM InterfaceTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID OR VirtualSystemID=$arrVSYS.ID")
SQL("DELETE FROM VirtualSystemTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID")
SQL("DELETE FROM ClusterTable WHERE ID=$arrDEV.ClusterID")
My issue is that I can't work out how to perform these queries across the database link. I amusing ADODB. I normally code in PHP, so this is a bit of a struggle for me!
You have the right idea but VBA is quite a bit different from PHP. You can't use variables inside strings with first exiting the string, concatenating the variable, and then starting the string again.
Where you would typically use an array called arrDev or arrVSys, we use a DAO Recordset or ADO Recordset object inside MS Access.
Updated answer to reflect the information you gave:
Dim db As DAO.Database
Dim sSQL as String
Dim arrDEV As DAO.Recordset, Dim arrVSYS as DAO.Recordset
Set db = OpenDatabase("C:\SomeDatabase.accdb")
sSQL = "SELECT ID, ClusterID FROM DeviceTable WHERE Distribution <> 'Public'"
Set arrDEV = db.Open(sSQL)
If Not (arrDEV.EOF and arrDEV.BOF) Then
arrDEV.movefirst
Do Until arrDEV.eof = True
sSQL = "SELECT ID FROM VirtualSystemTable WHERE DeviceID = " & arrDEV("ID") & _
" OR ClusterID = " & arrDEV("ClusterID")
Set arrVSYS = CurrentDb.Open(sSQl)
sSQL = "DELETE FROM InterfaceTable WHERE DeviceID = " & arrDEV("ID") & _
" OR ClusterID = " & arrDEV("ClusterID") & " OR VirtualSystemID = " & arrVSYS("ID")
CurrentDb.Execute sSQl, dbFailOnError
sSQL = "DELETE FROM VirtualSystemTable WHERE DeviceID = " & arrDEV("ID") & " OR ClusterID = " & arrDEV("ClusterID")
CurrentDb.Execute sSQl, dbFailOnError
sSQL = "DELETE FROM ClusterTable WHERE ID = " & arrDEV("ClusterID")
CurrentDb.Execute sSQl, dbFailOnError
arrVSYS.Close
arrDEV.MoveNext
Loop
End If
'Cleanup
Set arrVSYS = Nothing
arrDEV.Close
Set arrDEV = Nothing
db.close
Set db = Nothing
The code is untested and could have some errors. The only thing I didn't plan for was having more than one record in arrVSYS. If that recordset will have multiple records then you'll need yet another loop.
As you can see, you don't need to use ADO to access another Access database. But if the "outside" database is something other than Access then yes, you would need to use ADO.

Why am I getting Data provider or other service returned an E_FAIL status? SQL Native Client

I'm switching our ASP code to use SQL Native Client so that we can connect to a mirrored database with a failover partner, since you can only supply the failover partner parameters in SQL Native Client. When I run a proc that returns an nvarchar(max) column with Driver={SQL Server} everything works fine. When I run procs that return small colums with Driver={SQL Server Native Client 10.0} that works fine. It's only when I try to run a proc that returns an nvarchar(max) column while using Driver={SQL Server Native Client 10.0}; that I get the error. The error happens as soon as we hit
rs.Open cmdTemplate
So I'm not even referring to the column. Setting the conn string like this:
if bUseSQLNative then
connString = "Driver={SQL Server Native Client 10.0}; Network=DBMSSOCN; server=" & rs("SERVER_NAME") & "," & rs("PORT_NUM") & ";database=" & rs("DATABASE_NAME")
connString = connString & ";uid=" & rs("USER_NAME") & ";pwd=" & UnProtectValueEx(ConnSaltForDBPwd(), rs("CONNECTION_NAME"), rs("PASSWORD"))
else
connString = "Driver={SQL Server}; Network=DBMSSOCN; server=" & rs("SERVER_NAME") & "," & rs("PORT_NUM") & ";database=" & rs("DATABASE_NAME")
connString = connString & ";uid=" & rs("USER_NAME") & ";password=" & UnProtectValueEx(ConnSaltForDBPwd(), rs("CONNECTION_NAME"), rs("PASSWORD"))
end if
connString = connString & ";"
And opening like this:
set rs = server.CreateObject("ADODB.RecordSet")
rs.CursorLocation = 3
rs.CursorType = 3
rs.CacheSize = 50
on error resume next
rs.Open cmdTemplate
The error is:
Microsoft Cursor Engine (0x800A0001)
Data provider or other service returned an E_FAIL status.
In my case, the data to be saved (string) was larger than the specified nvarchr().
Increasing the field size solved the problem
I found it. I had to use
connString = "Provider=SQLNCLI10; DataTypeCompatibility=80;...
The DataTypeCompatibility makes the nvarchar max etc map back to formats ado can handle. And for some reason that parameter doesn't take effect with Driver={SQL Server Native Client 10.0};, only with Provider=SQLNCLI10
Use the reference MSADO 6.1 Library and then construct a data environment to connect to the database and establish the recordset to be used like this:
mydata = is a data environment with the connection to the database
getItemRec = is the query or command inside the mydata
myRecSet = is a Recordset Variable.
Do the code like this:
myData.Commands("getItemRec").CommandText = "Select * from myTable"
myData.getItemRec
Set myRecSet = myData.rsgetItemRec
With myRecSet
If .RecordCount <> 0 Then .MoveNext
Do While .EOF = False
....
....
.movenext
Loop
end With
Hope this will Help.