Is it possible to combine an Oracle table and Access table in one recordset with ADO?
I can do it with two Access table but not able to do with one Oracle and one Access.
any help would be appreciated.
EDIT
Working code with two access
Sub twoDB()
Dim con As ADODB.Connection
Dim strDB1 As String
Dim strDB2 As String
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
strDB1 = ".......\daotest.accdb"
strDB2 = ".......\daotest2.accdb"
mysql = "select * from [" & strDB1 & "].şubeler inner join [" & strDB2 & "].şb on (şb.şb=şubeler.SubeKodu)"
With con
.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB2 'oledb, odbc için: con.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" & strDB 'odbc
.Mode = adModeWrite
.Open
End With
Set rs = New ADODB.Recordset
rs.Open mysql, con, adOpenDynamic, adLockReadOnly
ActiveCell.CopyFromRecordset rs
End Sub
Non-Working code with one access and one oracle
Sub twoDBOneOracleOneAccess()
Dim con As ADODB.Connection
Dim strDB2 As String
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
strDB2 = "....\daotest2.accdb"
mysql = "select MUSTERI_ID,MUSTERI_BAGLI_OLDUGU_SUBE_ID from OracleTable a inner join [" & strDB2 & "].şb on (şb.şb=a.MUSTERI_BAGLI_OLDUGU_SUBE_ID) where A.MESLEK_KODU=2041"
With con
.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source........;"
.Open
End With
Set rs = New ADODB.Recordset
rs.Open mysql, con, adOpenKeyset, adLockOptimistic
ActiveCell.CopyFromRecordset rs
End Sub
Related
Hi I am trying to write a macro that takes the user input from an excel form and adds it to a access table. Using the following code:
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim wsh As Excel.Application
Set cnn = "db.accdb.connection"
Set rst = New ADODB.Recordset
rst.Open "table", cnn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
With rst
.AddNew
.Fields("column1").Value = textboxvar
.Update
End With
with textboxvar previously defined. But it wont work and I don't know why.
after searching for a long time. The top voted post in this thread already answers the question:
Using Excel VBA to export data to MS Access table
This just has to be updated to work with Access 2016:
Public Sub UploadExcel()
Set cn = CreateObject("ADODB.Connection")
dbPath = 'type your database path in here
dbWb = Application.ActiveWorkbook.FullName
dbWs = Application.ActiveSheet.Name
scn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
dsh = "[" & Application.ActiveSheet.Name & "$]"
cn.Open scn
ssql = "INSERT INTO TableName ([Field1], [Field2], [Field3]) "
ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh
cn.Execute ssql
End Sub
Example that declares, sets, opens connection:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; DataSource='C:\path\databasename.accdb'"
rs.Open "SELECT * FROM MyTable", cn, adOpenKeyset, adLockOptimistic
I have tried every iteration I can find online, but I am still returning -1^ for my recordset count instead of the actual count. I tried multiple combinations of the CursorType, LockType, & CursorLocation. Here is my code.
Sub test()
Dim FullQry As String
Dim qry1 As String
Dim qry2 As String
Dim qry3 As String
Dim qry4 As String
'DECLARE VARIABLES FOR CONNECTION (HOW THE QUERY CONNECTS TO TERADATA)
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
'DECLARE VARIABLES FOR RECORDSET (THE RESULTS OF THE SQL QUERY)
Dim rs As ADODB.Recordset
Set rs = CreateObject("ADODB.Recordset")
'DECLARE VARIABLES FOR COMMAND (I THINK THIS MAKES TERADATA RUN THE QUERY AFTER A CONNECTION IS ESTABLISHED)
Dim cmdSQLData As ADODB.Command
Set cmdSQLData = New ADODB.Command
'Connect to Teradata
cn.Open "Data Source = MOSAIC_PROD; Database= prod_flight_ops_combined_vw; Persist Security info=True; User ID=758673; Password=PSPL444eae???; Session Mode=System Default;"
Set cmdSQLData.ActiveConnection = cn
rs.CursorType = adOpenStatic
rs.LockType = adLockReadOnly
rs.CursorLocation = adUseClient
'Define Qry
qry1 = "SELECT AIRLINE, FLT_NUM, SKD_ORIG, SKD_DEST, ACT_ORIG, ACT_DEST, SKD_TAIL, ACT_TAIL, SKD_SUBFLEET, ACT_SUBFLEET, SKD_OUT_GMT, SKD_IN_GMT, ACT_OUT_GMT, ACT_ON_GMT, ACT_IN_GMT, ACT_OUT_DATE_GMT, ACT_IN_DATE_GMT, ACT_OFF_GMT, "
qry2 = "SKD_OUT_DATE_GMT , SKD_IN_DATE_GMT, SKD_BLK, ACT_BLK, SKD_AIR, ACT_AIR, SKD_TXOT, ACT_TXOT, SKD_TXIN, ACT_TXIN, SKD_OFF, ACT_OFF, SKD_ON, ACT_ON, SKD_TURN, ACT_TURN, AVAIL_TURN, MOGT, OP_STATUS, OP_STATUS_DESC, SUB_DIVERT_DESC , DELAY_MSG "
qry3 = "FROM prod_flight_ops_combined_vw.OPS_FLIGHT_LEG "
qry4 = "WHERE act_out_date_gmt > current_date - 45 and act_in_date_gmt < current_date - 1 and Airline = 'AA';"
FullQry = qry1 & qry2 & qry3 & qry4
cmdSQLData.CommandText = FullQry
cmdSQLData.CommandType = adCmdText
cmdSQLData.CommandTimeout = 0
Set rs = cmdSQLData.Execute()
x = rs.RecordCount
Set rs = Nothing
Set cmdSQLData = Nothing
cn.Close
End Sub
Thanks for the assistance.
Sub test()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Open "Data Source=MOSAIC_PROD"
Dim rs As ADODB.Recordset
Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient
rs.Open "SELECT * from FROM prod_flight_ops_combined_vw.OPS_FLIGHT_LEG", cn
MsgBox (rs.RecordCount)
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
I am trying to connect DB2 to Excel using ado connection string.Could you please let me know whats wrong with the code
Also it's a local setup on my desktop and can anyone tell me how to find hostname and userid.
Sub Import_data_from_db2()
Dim rs As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim sConnString As String
Set rs = New ADODB.Recordset
Set cnn = New ADODB.Connection
' create the connection
sConnString = "Provider=IBMDADB2;Database=Sample;" & _
"Hostname=localhost;Protocol=TCPIP;" & _
"Port=50000;Uid=db2admin;Pwd=xx;"
'Open connection
cnn.Open sConnString
strQry = "SELECT * FROM ORDERS"
With rs
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open strQry, cnn
End With
Sheets(1).Range("A1").CopyFromRecordset rs
End Sub
I am getting error 3705 after adding the line
[MTSU_Data].[Tool no]=" CInt(WsInput.Range("J" & c).value) & ";"
The highlighted line where the error occurs is at
.ActiveConnection = conn
Full code
Dim conn As ADODB.Connection
Dim Accdata As ADODB.Recordset
Dim Accfield As ADODB.Field
Dim wsQueryR As Worksheet, wsFinal As Worksheet
Set wsFinal = Worksheets("Final")
Set conn = New ADODB.Connection
Set Accdata = New ADODB.Recordset
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\wongki7\Desktop\MTSU Db.accdb;"
conn.Open
'G:\BusUnits\MTSU\MTSU\Mold Tool Set Up\MTSU Reports\Shift Reports\MTSU Db.accdb;"
' On Error GoTo CloseConnection
For c = 2 To WsInput.Range("J" & Rows.Count).End(xlUp).Row
With Accdata
.ActiveConnection = conn
.Source = "SELECT * FROM [MTSU_Data] Where [MTSU_Data].[Date]>= #" _
& Format(CDate(WsInput.Range("A2").value), "mm/dd/yyyy") & " # AND [MTSU_Data].[Date]<= #" _
& Format(CDate(WsInput.Range("A3").value), "mm/dd/yyyy") & " # AND [MTSU_Data].[LT] = " _
& CInt(WsInput.Range("M2").value) & " AND [MTSU_Data].[Tool no]=" _
& CInt(WsInput.Range("J" & c).value) & ";"
.LockType = adLockReadOnly
'.CursorType = adOpenForwardOnly
.Open
End With
Next
Worksheets("Result").Select
Sheets("Result").Range("a2").CopyFromRecordset Accdata
Accdata.Close
conn.Close
Any help is appreciated. Thanks and have a good day ahead.
Can you not try to move the line
.ActiveConnection = conn
To outside the loop. Obviously fully qualifying it..
Accdata.ActiveConnection = conn
That looks to me that you are trying to make the connection for each iteration and I think it is only necessary once... maybe.
To best fit your code, Id try something like:
Sub SomeRoutine()
Dim conn As ADODB.Connection
Dim Accdata As ADODB.Recordset
Dim Accfield As ADODB.Field
Dim wsQueryR As Worksheet, wsFinal As Worksheet
Dim c As Long
Set wsFinal = Worksheets("Final")
Set conn = New ADODB.Connection
Set Accdata = New ADODB.Recordset
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\wongki7\Desktop\MTSU Db.accdb;"
conn.Open
Accdata.ActiveConnection = conn
Accdata.LockType = adLockReadOnly
For c = 2 To WsInput.Range("J" & Rows.Count).End(xlUp).Row
Call Accdata.Open("SELECT * FROM BLah Blah")
Sheets("Result").Range("a2").CopyFromRecordset Accdata
Accdata.Close
Next
conn.Close
End Sub
You may be able to look at the way you build the SQL request to build a query to get the data in one go and and do a single paste to Excel with the CopyFromRecordset.... but that's a different issue.
A mistake I've made many times before. When assigning objects, you need to use Set. Change the line to
Set .ActiveConnection = conn
.Source and .LockType are scalar properties, so they don't need Set, but .Activeconnection does.
I need to get my table up in the powerpivot model down to the excel worksheet.
So far I have tried to use a Recordset but I cant get an ActiveConnection to the powerpivot table. Is it possible? Or is there an other better way to do this?
I use the following code:
Dim name As ADODB.Recordset
Set name = New ADODB.Recordset
With name
.ActiveConnection = ConnectionName
.Source = "TableName"
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
But with this piece of code I get an error at .ActiveConnection. (Run-time error 3001, it complains about non-allowed connection interval)
This is an example of how to read the records from a named range (assuming 'TableData' is named range).
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=Excel 8.0;"
.Open
End With
rs.Open "SELECT * FROM [TableName]", cn
Dim r
For Each r In rs.GetRows
'Do whatever you want per record
Debug.Print r
Next r
rs.Close
cn.Close