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
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
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
I can't import values values from SQL Server to Excel. Can someone please help:
Dim oConn As ADODB.Connection
Private Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "Provider=SQLOLEDB; " & _
"Data Source=ServerName; " & _
"Initial Catalog=MyDB;" & _
"Trusted_Connection=yes;"
End Sub
Public Sub ExportDataToDB()
Dim rs As ADODB.Recordset
ConnectDB
Set rs = New ADODB.Recordset
Dim strSql As String
strSql = "select t.col1, t.col2 from Table t"
rs.Open strSql, oConn
Sheet1.Range("A2").CopyFromRecordset rs
CloseDBConn
End Sub
Private Sub CloseDBConn()
oConn.Close
End Sub
Connection is successful. No errors. Query is not empty, but nothing is returned into sheet.
In cases where you would like to just read a set of records from the database, such as writing current state of the records to the worksheet you should specify CursorType property of the ADODB.RecordSet as adOpenStatic
Here is the link to Microsoft documention of it: https://learn.microsoft.com/en-us/sql/ado/guide/data/types-of-cursors-ado
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 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