Find all database names on a Netezza server - excel

I want to find out the names of all DB instances on a Netezza DB server. I am using the following code:
Set objConn = CreateObject("ADODB.Connection")
Set objRec = CreateObject("ADODB.Recordset")
objConn.open "Driver={NetezzaSQL};servername=xx.xx.xx.xx;port=5480;database=_v_database;username=pankaj;password=xxx_333"
sqlQuery="select database from _v_database"
objRec.open sqlQuery, objConn
value = objRec.fields.item(0)
msgbox Value
I am able to connect to individual DBs and able to fetch data. However, when I try to run the above code I am getting "Database _V_DATABASE does not exist" error. Can anyone help me in finding all the instances on a particular server - doesn't have to be strictly in VBA.

Found the solution - I used database=SYSTEM instead of database=_v_database in the connection string.
Set objConn = CreateObject("ADODB.Connection")
Set objRec = CreateObject("ADODB.Recordset")
objConn.open "Driver={NetezzaSQL};servername=xx.xx.xx.xx;port=5480;database=SYSTEM;username=pankaj;password=xxx_333"
sqlQuery="select database from _v_database"
objRec.open sqlQuery, objConn
value = objRec.fields.item(0)
msgbox Value

Related

Performance issue with access to excel vba

I need to get data from Access to excel vba, I use ADODB.
My problem is that although the database is relatively small and query results 30-40 records only, the process gets stuck either with the ".open" or with "copyfromrecordset" line and takes 40-50 secs to display the records.
This is my code.
I made some tests with different cursor types and locktypes with no result. The query is working executed directly from access and I have no issue when the connection points locally to my PC. I am on office 365. I referenced the activex data objects 2.8 library.
Sub loadTestDisplay2()
Dim myConnectiom As ADODB.Connection
Dim myRS As ADODB.Recordset
Const conStringNet As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\ourserver-f04\COE-Shared\Data Tool\Access\adsLoadTest.accdb; Persist Security Info=False;"
Dim sql As String
sql = "SELECT * FROM tblLoad where user is Null"
Set myConnection = New ADODB.Connection
Set myRS = New ADODB.Recordset
myConnection.ConnectionString = conStringNet
myConnection.Open
With myRS
.ActiveConnection = conStringNet
.Source = sql
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Sheets.Add
Range("A2").CopyFromRecordset myRS
myRS.Close
myConnection.Close
End Sub

Updating closed Access database with Excel VBA

I have a macro in Excel to delete tables in Access.
Is there is a way to do this without opening the Access database?
Set AppAcc = Nothing
Set AppAcc = New Access.Application
AppAcc.Visible = True
AppAcc.OpenCurrentDatabase "C:\MyStuff\MyDataBase.mdb"
AppAcc.DoCmd.SetWarnings False
With AppAcc
.DoCmd.OpenQuery "Delete_Query1"
.DoCmd.OpenQuery "Delete_Query2"
.DoCmd.OpenQuery "Delete_Query3"
End With
AppAcc.DoCmd.SetWarnings True
AppAcc.Quit acQuitSaveNone
Set AppAcc = Nothing
Absolutely, there is a way. Since MS Access is a database it can connect via a backend like all major RDBMS's (Oracle, SQL Server, Postgres, etc.). Below demonstrates ODBC/OLEDB connections using ADO. Then, further below is an alternative version with DAO which you are currently running but can do so without Access.Application and DoCmd calls.
Both should even work without having MS Access GUI (the MS Office app) installed. Each use late binding. For early binding, add VBA references Microsoft ActiveX Data Objects #.# Library or Microsoft DAO #.# Object Library, then modify Dim and Set statements.
ADO
Dim conn As Object
Set conn = CreateObject("ADODB.Connection")
' WITH ODBC DRIVER
conn.Open "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=C:\MyStuff\MyDataBase.mdb;"
' WITH ODBC DSN
'conn.Open "DSN=MS Access Database;DBQ=C:\MyStuff\MyDataBase.mdb;"
' WITH OLEDB PROVIDER
'conn.Open "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyStuff\MyDataBase.mdb;"
' THREE WAYS TO CALL ACTION QUERIES
conn.Execute "{CALL Delete_Query1}"
conn.Execute "EXEC Delete_Query2"
conn.Execute "Delete_Query3"
Set conn = Nothing
DAO
Dim conn As Object, db As Object
Set conn = CreateObject("DAO.DBEngine.120")
Set db = conn.OpenDatabase("C:\MyStuff\MyDataBase.mdb")
db.Execute "Delete_Query1"
db.Execute "Delete_Query2"
db.Execute "Delete_Query3"
Set db = Nothing
Set conn = Nothing

Excel VBA Executing Access Query that Links to Oracle

Let me start of by saying I am new to both this site as well as VBA so please bear with me. Thank you in advance for your help.
I have a VBA function that runs an existing query from Access. Some of the tables being queried are stored in an Oracle database that require a user specific password access. Right now, a sub that I wrote to automate a report calls this function 7 times and requires the user to input their Oracle password each time the function is called (it also stops the sub and gives an error message if they type in the password incorrectly which I see as a likely event if they need to do it 7 times). The code works but I would like to find a way to have the code ask for the password once and be done with it. All of the solutions I have found involve connecting to and querying Oracle directly which requires very complicated SQL coding that I am by no means capable of writing.
I am also having an issue where the columns show up in the excel sheet in a different order than they do in Access for some of the queries. This seems to be consistent so it isn't to big of a problem but I would like to know how to prevent this to prevent any future issues.
Here is the code for the function I am currently using. Any insight would be greatly appreciated!
Option Explicit
'Single Argument "qryName" as string. Runs access qry and copys recordset to active sheet.
Function AccessQueryPull(qryName As String)
'Declare variables
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
'open the connection to the access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=filePath.accdb;"
'format the command to run the query
Dim cmd As New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = qryName
cmd.ActiveConnection = cn
'execute the query
Set rs = New ADODB.Recordset
Set rs = cmd.Execute()
'copy data to excel sheet
ActiveSheet.Cells(2, 1).CopyFromRecordset rs
'Cleanup
rs.Close
Set rs = Nothing
The reason you are prompted each time for Oracle credentials is that you create a fresh, new connection to MS Access each time the function is called.
Simply persist the MS Access connection by connecting once in the Sub that calls function and pass connection object as parameter. In this way, any connection error is caught in the parent routine. As for column order simply declare the columns in the needed order in SQL statement.
Sub RunQueries()
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
'open the connection to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=filePath.accdb;"
Call AccessQueryPull(cn, "SELECT Col1, Col2, Col3 FROM Qry1") ' AND OTHER 6 CALLS
cn.Close
Set cn = Nothing
End Sub
Function AccessQueryPull(accConn As ADODB.Connection, qryName As String)
'format the command to run the query
Dim cmd As New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = qryName
cmd.ActiveConnection = accConn
'execute the query
Set rs = New ADODB.Recordset
Set rs = cmd.Execute()
'copy data to excel sheet
ActiveSheet.Cells(2, 1).CopyFromRecordset rs
'Cleanup
rs.Close
Set rs = Nothing: Set cmd = Nothing
End Function

ADODB connection to .mdb file from excel 2013

I hope someone can help.
I've developed an excel package that updates a .mdb access database through the connection string "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"
The database is shared.
I have a Mitel telephony system that checks the database every 10 seconds or so to check for a new entry.
The database is updated with
Dim q As New ADODB.Command
Dim cn As New ADODB.Connection
Dim SQL As String
SQL = "INSERT INTO tbl1LinkAuths (DateTimeAdded, DateEntered, AddedBy, REG, OrderNo,AccountNumber, CentreNumber, EmailAddress, Callback, "
SQL = SQL & "MakeText, "...............
cn.Open cnDB
q.ActiveConnection = cn
q.CommandText = SQL
'Excecute the above SQL to insert the new job record
q.Execute
Set rs = Nothing
Set cn = Nothing
Dim db As Access.Application
Set db = New Access.Application
db.Application.Visible = False
db.OpenCurrentDatabase "\\kffcis02\VWM Share\TelephonyDB.mdb", False
db.CloseCurrentDatabase
The INSERT statement updates the database fine, but I find I have to open and close the database to get it to update in time.
This package is used heavily by around 5 people at a time, making about 2 entries per minute.
It comes up with the error "file already in use", especially when using excel 2013, a lot of the time. I think this is because I have to open/close the database every time I update.
Does anybody know of a different way I can get the database to update quicker?
I've got the actual database setting to update ADODB every second and the database is shared.
I'm now desperate, as this package has went live. I didn't have any problems during testing because there wasn't as many people using it and none of them were on office 2013.
Wrong driver: Assuming a reference to activex data objects...
dim conn as adodb.connection 'module level variable
const DBNAME = "your name here"
const DBLOC = "Your dir here"
Sub UpdateDb()
dim sql as string
openconnectionroutine
sql = "INSERT INTO tbl1LinkAuths (DateTimeAdded, DateEntered, AddedBy, "
'etc
'if you want to check it worked : otherwise ditch numrecs
dim numrecs as long
conn.execute sql, numrecs
msgbox "You added " & numrecs & " records",vbokonly,"Done"
end sub
sub Openconnectionroutine()
if conn is nothing then set conn = new adodb.connection
if conn.connectionstring = "" then
conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=" & DBNAME & ";" & _
"DefaultDir=" & DBLOC & ";" & _
"Uid=Admin;Pwd=;"
end if
if conn.state = adstateopen then
else
conn.Open
end if
End sub

Get specific data(query) from Oracle into excel

I want to load specific data(query) from Oracle database into excel. I am able to achieve it through external connections, (Mircosoft Data Access - OLE DB Provider for Oracle), but all of the table is loaded. This was by hit and try. I am not aware what OLE DB is.
Is it possible to load specific data using that method.
How can I load the same from VBA, I have read many sources, blogs but none are lucid and comprehensive. Can somebody please explain for a newbie. Or refer to me some book/source.
This function will connect to an Oracle Database using ADODB. Make sure to include Microsoft ActiveX Data Objects 2.8 as a reference. You can configure the connectingstring to suit your needs if there are admin privileges.
It will store your database into a variant.
Function ConToDataBase(DBPath As String) As Variant
Dim Con As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim SQL As String
SQL = "SELECT * FROM TableName"
Set Con = New ADODB.Connection
With Con
.ConnectionString = "Provider=OraOLEDB.Oracle;Data source=" & DBPath & ";UserID=;Password:=;"
.Open
End With
Set Rs = New ADODB.Recordset
Rs.Open SQL, Con
Dim Var As Variant
Var = Rs.GetRows
ConToDataBase = Var
Set Rs = Nothing
Con.Close
End Function

Resources