I am trying to connect to Oracle using Excel VBA but when the code is run, Excel is closed and reopened at line MsgBox rs.recordCount
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim query As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo final1
query = "Select * from EMP where EMP_NO='998234'"
strcon = "Driver={Oracle in OraClient11g_home1};Dbq=smqa;Uid=MyUserID;Pwd=MyPassword;"
con.Open (strcon)
rs.CursorType = 1
rs.Open query, con
rs.MoveLast
MsgBox rs.recordCount
rs.Close
Set s = Nothing
con.Close
Set con = Nothing
final1:
MsgBox err.Number & " " & err.Description
End Sub
I am running this code on Windows 7 and trying to connect Oracle 11g. Do I need any additional setup to connect Oracle Db using Excel VBA or is something wrong in the code?
Related
I am trying to connect to Oracle DB for the first time using the ODBC Datasource Connector(named Demo_DSN) When I Step Into (F8) the code is giving Run-time error '3706': application-defined or object -defined error
Sub ConnectToOracle()
Dim cn As ADODB.Connection
Dim rs As ADODB.RecordSet
Dim mtxdata As Variant
Set cn = New ADODB.Connnection
Set rs = New ADODB.Recordset
cn.Open ( _
"User ID = XXGMDMADM" & _
";Password=xxgmdmadmdb" & _
";Data Source= Demo_DSN" &_
";Provider= OraOLEDB.Oracle")
'Cleanup in the end
Set rs = Nothing
Set cn = Nothing
End Sub
when i run the given original code the error in below line is shown "Run-time error 91"
con.Open "Provider=Microsoft.Jet.4.0;Data Source=C:\Documents and Settings\XPMUser\Desktop\New Folder\prac1.mdb; Persist Security Info = False"
original code
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub SUBMIT_Click()
con.Open "Provider=Microsoft.Jet.4.0;Data Source=C:\Documents and Settings\XPMUser\Desktop\New Folder\prac1.mdb; Persist Security Info = False"
rs.Open "select DBTB1 from prac1", con, adOpenDynamic, adLockPessimistic
rs.Fields("NUMBER").Value = Text1.Text
rs.Fields("NAME").Value = Text2.Text
rs.Fields("CITY").Value = Text3.Text
MsgBox "data saved!", vbInformation
rs.Update
End Sub
You are getting the Error 91 because you have not actually created the Connection object. Further, you will get the same error with the RecordSet. I have updated your code to allow it to work:
Private Sub SUBMIT_Click()
Set con = New ADODB.Connection
con.Open "Provider=Microsoft.Jet.4.0;Data Source=C:\Documents and Settings\XPMUser\Desktop\New Folder\prac1.mdb; Persist Security Info = False"
Set rs = New ADODB.Recordset
rs.Open "select DBTB1 from prac1", con, adOpenDynamic, adLockPessimistic
rs.AddNew
rs.fields("NUMBER").value = Text1.Text
rs.fields("NAME").value = Text2.Text
rs.fields("CITY").value = Text3.Text
rs.Update
MsgBox "data saved!", vbInformation
End Sub
Also, please note the addition of AddNew prior to updating the database.
When i check the result set. record count it returns -1 and while checking the recordset.EOF it returns true, thus the result set does not contain any value.
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL
Private Sub cmd_login_Click()
Dim pass As String
con.ConnectionString = "Provider=msdaora;Data Source=localhost;User Id=ams;Password=krishnan;"
con.Open
strSQL = "Select passwrd from ams.login_details where username = 'Admin'"
rs.Open strSQL, con
If Not (rs.EOF) Then
If rs("passwrd") = txt_pass.Text Then
MsgBox rs("passwrd")
End If
End If
rs.Close
con.Close
End Sub
I forget to commit the statements in Oracle Sql Developer that's why not data was fetched from the database, When i executed the commit statement, it's working fine.
I am developing winform application in vb6. I am using crystal report 4.6. I have created a crystal report which shows all data from a table (MS Access). And I unchecked save data with report and i saved the report. I just want to invoke it in application. So I included the component CrystalReportControl in my application. Now i want to set the records to be displayed in the report. The records are selected according to the user input to the text box.
Records are retrived from the database is done in following code.
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Command1_Click()
Set conn = New ADODB.Connection
conn.Open "provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path &"\faculty.mdb"
Set rs = New ADODB.Recordset
rs.Open "select * from facultydetails where eid=1234", conn, adOpenDynamic, adLockPessimistic
CrystalReport1.ReportFileName = App.Path & "\faculty.rpt"
Set CrystalReport1.DataSource = rs
CrystalReport1.Action = 1
End Sub
Gives an error for the line: Set CrystalReport1.DataSource = rs :
as Property is write-only.
Tell me how the records of the report can be dynamic? Plz help me...
Instead of
Set CrystalReport1.DataSource = rs
do
CrystalReport1.DataSource = rs
EDIT:
Take a look at the following example and see if that will help you:
'CrystalReport1 is the name of the dsr file
Dim Report As New CrystalReport1
Dim cdoRowset As CrystalDataObject.CrystalComObject
Dim varArray() As Variant
'Open ADO Connection
Set m_cnAdo = New ADODB.Connection
m_cnAdo.ConnectionString = "DRIVER={SQL Server};UID=[UserID];PWD=[Password]" _
& ";SERVER=[Server];DATABASE=[Database]"
m_cnAdo.Open
Dim rsAdo As ADODB.Recordset
Dim cmdAdo As ADODB.Command
'Using Embedded Query
Set cmdAdo = New ADODB.Command
Set rsAdo = New ADODB.Recordset
cmdAdo.ActiveConnection = m_cnAdo
cmdAdo.CommandText = "SELECT * FROM Table WHERE Param = " & lngParam1
cmdAdo.CommandType = adCmdText
Set rsAdo = cmdAdo.Execute
Report.Database.SetDataSource rsAdo, 3, 1
I am trying to connect to a SQL Server Compact Edition .sdf file with following connection string;
connMRC.ConnectionString = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=" & App.Path & "\Freeweigh.sdf;"
I get the following error everytime I try running an SQL command or opening a recordset:
Multiple-step OLE DB generated errors. Check each OLE DB status value, if available. No work was done.
I am using VB 6.0 and SQL Server Compact 3.5 SP2
Here's the code:
Public Sub opnConnectionC()
'Code for opening the ADO Connection
chkConn = connMRC.State
If chkConn = adStateClosed Then
connMRC.ConnectionString = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=" & App.Path & "\Freeweigh.sdf;"
connMRC.Open
End If
End Sub
Public Sub opnRecordsetC(rsOpen As Recordset)
'Code for opening the ADO Recordset
chkRs = rsOpen.State
If chkRs = adStateClosed Then
rsOpen.Source = strSQLC
rsOpen.CursorType = adOpenDynamic
rsOpen.LockType = adLockOptimistic
rsOpen.ActiveConnection = connMRC
rsOpen.Open
End If
End Sub
Private Sub tmrUpload_Timer()
Dim cmdUpload As New ADODB.Command
Dim rsFetch As New ADODB.Recordset
Call opnConnectionC
strSQLC = "SELECT Product FROM VehicleWeights"
Call opnRecordsetC(rsFetch)
rsFetch.MoveFirst
MsgBox (rsFetch.Fields("Product").Value)
Call clsConnectionC
End Sub
You can only open a forward only, read only recordset