VBA to export MS Access table into Oracle table - vba

I am trying to insert ms access table into Oracle table using the below code. I get an error "table or view does not exist" at cn.Execute strSql line. I think the code is unable to identify the access table name. Any help is greatly appreciated. Thanks!
Function test()
Dim cn As New ADODB.Connection
Dim rs As DAO.Recordset
Dim strSql As String
Dim db As DAO.Database
Dim ssqlFields As String
Dim i As Integer
Dim ssql As String
Set db = CurrentDb
Set cn = New ADODB.Connection
Set rs = db.OpenRecordset("SELECT * FROM T_556_557_ORCL", dbOpenDynaset)
cn.Open ( _
"User ID = XXXX" & _
";Password=YYYY" & _
";Data Source=ABCD" & _
";Provider=OraOLEDB.Oracle")
cn.CommandTimeout = 300
strSql = "INSERT INTO TDMTRDOP.TEST SELECT Ent_Ex_Summ,QTY FROM T_556_557_ORCL"
cn.Execute strSql
Set rs = Nothing
Set cn = Nothing
End Function

Related

Insert table records from Local MS Access Database to Oracle table

I am trying to Insert records from a local Access database to an Oracle table. Same fields in both tables.
I don't want to loop through the lines if I don't have to. I have to code to connect to the oracle database. Just not sure of the code to select the local Access table called "RET_PARTS" and insert that data to the Oracle table called RET2_PARTS_ORACLE"
Sub subFastUpload()
`enter code here`On Error GoTo Err_subFastUpload
`enter code here`Dim intlength As Integer
`enter code here`intlength = 8
`enter code here`Dim strServer As String
strServer = "MARS"
Const maxItems = 500
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim objRst As New ADODB.Recordset
Dim objCon As New ADODB.Connection
Dim objCmd As New ADODB.Command
Dim strTable As String
Dim intCount As Integer
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("Ret2_Parts")
Dim mars_pass As String
'mars_pass = InputBox("Enter MaRS Password")
mars_pass = "XXXXXXX"
intCount = 1
If strServer = "MARS" Then
strTable = Environ("UserName") & ".RET2_PARTS_ORACLE"
objCon.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=MARSNEW.123;User ID=" & Environ("UserName") & ";Password=" & mars_pass & ";"
strTable = Environ("UserName") & ".RET2_PARTS_ORACLE"
End If
objCon.Open
objCmd.ActiveConnection = objCon
objCmd.CommandType = adCmdText
objCmd.Properties("PLSQLRSet") = False
While Not rst.EOF
If intCount = 1 Then
strSQL = "Select * from RET2_Parts"
Debug.Print strSQL

Read SQL Server query for ADODB.Connection from external file or from variable

I have this Excel VBA code:
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim query As String
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=xxxx.xxx.xxxx.xxx,xxxx;" & _
"Initial Catalog=mydb;" & _
"Trusted_Connection=yes;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute("")
ActiveSheet.Range("A2").CopyFromRecordset rs
...
End Sub
all works fine.
The issue is in this section: Set rs = conn.Execute("")
My select statement query is just too big, split it into continuation lines is not practical.
Is there a way of reading the query text from a file, or from a variable?
Thank you very much
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim query As String
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=xxxx.xxx.xxxx.xxx,xxxx;" & _
"Initial Catalog=mydb;" & _
"Trusted_Connection=yes;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
'read SQL from file
query = GetContent("C:\Stuff\myQuery.txt")
Set rs = conn.Execute(query)
ActiveSheet.Range("A2").CopyFromRecordset rs
...
End Sub
'read all file content
Function GetContent(f As String) As String
GetContent = CreateObject("scripting.filesystemobject"). _
opentextfile(f, 1).readall()
End Function
Alternatively, store your SQL in a worksheet cell and read it from there.
You don't have to use continuation lines, you can append the strings and append a vbCrLf character. e.g.
Dim sSQL as string
sSQL = "SELECT" & vbCrLf
sSQL = sSQL & "AField" & vbCrLf
sSQL = sSQL & ",BField" & vbCrLf
sSQL = sSQL & ",(SELECT XField FROM AnotherTable ..........) as XField" & vbCrlf
'and so on and so on
Set rs = conn.Execute(sSQL)
You can build very long SQL statements this way.

Update Error on VBA .Execute on ADODB Recordset

I am trying to update a SQL DB table through the input from my textbox. The issue I am having can be seen with the generic VBA Sub seen below:
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim strSql As String
Dim strADOCon As String
strADOConTechSub = "PROVIDER=SQLOLEDB;Data Source=______;Initial Catalog=______;User ID = _______; Password = ______;Trusted_Connection=Yes"
conn.Open strADOCon
strSql = "SELECT this " & _
"FROM there " & _
"WHERE that <> ''"
Set rst = conn.Execute(strSql)
rst!this.Value = Me.Textbox1
conn.Close
I am receiving the error message;
Run-Time error '3251':
Current Recordset does not support updating. This may be a limitation
of the provider, or of the selected locktype.
Any help would be greatly appreciated.

If InputBox equals Value1 then connect to Database1

I'm trying to create a VBA code that would connect to a specific database based on the user's input. E.g. if a user enters DB1 into the prompt, the code will run the following query: SELECT * FROM MyServerName.DB1.dbo.Table
Here is what I've got so far:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim strSQL As String
Dim DB As Variant
DB = InputBox("Please enter the Database Name.")
sConnString = "Provider=SQLOLEDB;Data Source=MyServerName;" & _
"Integrated Security=SSPI;"
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Unsuccessful attempt to use the Else/If statement:
if '" & DB & "' = "DB1" then
conn.Open sConnString
Set rs = conn.Execute("SELECT * FROM MyServerName.DB1.dbo.Table ;")
elseif '" & DB & "' = "DB2" then
conn.Open sConnString
Set rs = conn.Execute("SELECT * FROM MyServerName.DB2.dbo.Table ;")
else
MsgBox "No records returned. Enter the correct Database Name.", vbCritical
End If
If Not rs.EOF Then
Sheets("Sheet1").Range("A2").CopyFromRecordset rs
rs.Close
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
The connection itself is working just fine when I use the select statement without Else/If. Does anyone know how to fix this? Thanks so much!
You could just use the DB variable to decide which database to use
Set rs = conn.Execute("SELECT * FROM [MyServerName].[" & DB & "].dbo.table ;")

Copying ADO recordset into excel worksheet

I'm trying to open a CSV file and query it and return the results into column A of the second worksheet of "ThisWorkbook".
I'm not getting any errors so I do not see why it is not copying the record set into excel.
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim currentDataFilePath As String
Dim currentDataFileName As String
Dim nextRow As Integer
currentDataFilePath = "C:\Users\M\folder\"
currentDataFileName = "csv-file"
con.Open "Provider=Microsoft.JET.OLEDB.4.0;" & _
"Data Source=" & currentDataFilePath & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited;IMEX=1"""
'rs.ActiveConnection = con
rs.Open "SELECT Name FROM [" & currentDataFileName & ".csv] WHERE Datatype ='TYPE3'",
con
ThisWorkbook.Worksheets("Sheet2").Range("A:A").CopyFromRecordset rs
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
End Sub
You might refer to the CopyFromRecordset() method.
Based on your code above, after the rs.Open command you would add something like this:
ActiveWorksheet.Range("A1").CopyFromRecordset rs
See more here: http://msdn.microsoft.com/en-us/library/office/ff839240%28v=office.15%29.aspx