Database connection VB6 to XAMPP - database

I'm totally new to vb6 and I wanna know why this code does not work. it has runtime 424 error. what do i have to do? whenever i try running it. I can't access the db it says empty. Here's the entire code.
Public cn As ADODB.Connection
Public rs As ADODB.Recordset
Private Sub Form_Load()
Set cn = New ADODB.Connection
cn.ConnectionString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=db_Inventory; User=root;Password=;Option=3;"
End Sub
Private Sub Cmd_Add_Click()
If Txt_Pname.Text = "" And Txt_Pprice.Text = "" Then
MsgBox ("Please enter details")
Else
rst.Open "select * from tbl_product", db, adOpenStatic, adLockOptimistic, adCmdText
rs.AddNew
rs("Prod_Name") = Txt_Pname.Text
rs("Prod_Price") = Txt_Pprice.Text
rs.Update
rs.Close
MsgBox ("Product Added!")
End If
End Sub

Related

ADO connection from Excel to SQL Server executes but returns no records

I have a spreadsheet from which I want to pass SQL script to my SQL server Database, both to retrieve records and run stored procedures.
Here's my code:
Sub ApendPickListData()
Dim SqlConn As New ADODB.Connection
Dim listID As Integer
Dim lists As New ADODB.Recordset
Dim SQLstr As String
SqlConn.ConnectionString = "Provider = 'SQLOLEDB';Server=MyServer\SQLEXPRESS;Database=MyDatabase;Uid=Username;PWD=Password;"
SqlConn.Open
'The following execution of a stored procedure works
SqlConn.Execute "Exec spListsInsertNew #Type = 'Picking', #Date ='" & Date & "'"
SQLstr = "SELECT ItemList.ItemNumber from ItemList"
'This method doesn't work
With lists
.ActiveConnection = SqlConn
.Source = SQLstr
.Open
Debug.Print .RecordCount
'prints -1 in the immediate window - no records
End With
'Neither does this method
Set lists = SqlConn.Execute(SQLstr)
Debug.Print lists.RecordCount
'prints -1 in the immediate window - no records
SqlConn.Close
End Sub
I feel like I'm missing something obvious. I've searched this site and others, found examples where this code should work. I've tested the select statement in SSMS and it works as expected.
Any help would be appreciated!
The code that worked was from Mark Balhoff's comment. Here it is:
Sub ApendPickListData()
Dim SqlConn As New ADODB.Connection
Dim listID As Integer
Dim lists As New ADODB.Recordset
Dim SQLstr As String
SqlConn.ConnectionString = "Provider = 'SQLOLEDB';Server=Myserver\SQLEXPRESS;Database=MyDB;Uid=Username;PWD=Password;"
SqlConn.Open
SQLstr = "select dbo.ItemList.ItemNumber from dbo.ItemList"
With lists
.ActiveConnection = SqlConn
.Source = SQLstr
.CursorLocation = adUseClient 'This was the key!
.Open
Debug.Print .RecordCount
End With
SqlConn.close
End Sub

VBA to connect Oracle DB

I'm using below code in VBA (macro) to connect Oracle DB.
I'm getting error as
Run-time error '3706':
Application-defined or object-defined error
Please help me any other setting to do
Sub ConnectToOracle()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mtxData As Variant
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open ( _
"User ID=cadcaguser" & _
";Password=cadcaguser11" & _
";Data Source=macroToDB" & _
";Provider=OraOLEDB.Oracle")
'Clean upin the end
Set cn = Nothing
Set rs = Nothing
End Sub

Error: "Command text was not set for the command object" when running a Stored Procedure from SQL on VBA

I'm trying to run a stored procedure on VBA using the following VBA code:
Please can someone advise: I get the error at "rs.Open".
Sub connection()
Dim Conn As ADODB.connection
Dim ADODBCmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim i As Integer
Dim constring As String
Dim location As String 'the server
Dim password As String
location = "10.103.98.18"
password = "password"
constring = "Provider=SQLOLEDB; Network Library=DBMSSOCN;Data Source=" & location & ";Command Timeout=0;Connection Timeout=0;Packet Size=4096; Initial Catalog=ElColibri; User ID=Analyst1; Password=password;"
Set Conn = New ADODB.connection
Conn.connectionString = constring
'On Error GoTo ConnectionError
Conn.Open
'loginstatus = False
'Exit Sub
'errorhandl0
'ConnectionError:
'MsgBox "Not possible to log in. Have you entered the correct password?"
'open recordset
Set ADODBCmd = New ADODB.Command
ADODBCmd.ActiveConnection = Conn
ADODBCmd.CommandTimeout = 1200
ADODBCmd.CommandText = ["ukrmc.dbo.FridayCommentary"]
ADODBCmd.CommandType = 4 'adCmdStoredProc
ADODBCmd.Execute
Set rs = New ADODB.Recordset
rs.ActiveConnection = Conn
rs.Open
Conn.Close
Set Conn = Nothing
Set ADODBCmd = Nothing
'Paste to spreadsheet
ThisWorkbook.Worksheets("macrotest").Range("a2").CopyFromRecordset
'Set rs = conn.Execute(Query)
rs.Close
Set rs = Nothing
End Sub
To me the code makes logical sense to me so I am not sure what the error means. Because to me, I have set text for the command object.
You are not connecting the recordset to your command. Assuming that your stored procedure issues a SELECT, change your code to
Set rs = ADODBCmd.Execute
thisWorkbook.Worksheets("macrotest").Range("a2").CopyFromRecordset rs
The Execute-Method will return a Recordset as result, no need to create one by yourself.
Or you should add the command object to the open operation
rs.Open ADODBCmd

VBA SQL Server Select

I was wondering if you could please check my code in VBA (Excel) to retrieve data from a SQL Server database, and insert it in the sheet.
It returns an error according the picture attached.
Sub ConnectionTest()
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Dim constr As String
constr = "Provider=sqloledb;Data source=USO-YEGANEH\SQL2008;Initial Catalgo=USO_Final;User Id=sa;Password=123"
Dim conRS As ADODB.Recordset
Set conRS = New ADODB.Recordset
conn.Open constr
With conRS
.ActiveConnection = conn
.Open "Select * from LatLong_Amar"
Sheet1.Range("A1").CopyFromRecordset conRS
.Close
End With
End Sub
I found out what is the problem, it seems full path of the table, despite the fact that database is defined in the Connection String, should be entered. So, the line "Select * from LatLong_Amar" should be changed to [USO_Final].[dbo].[Latlong_Amar]
Sub ConnectionTest()
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Dim constr As String
constr = "Provider=sqloledb;Data source=USO-YEGANEH\SQL2008;Initial Catalgo=USO_Final;User Id=sa;Password=123"
Dim conRS As ADODB.Recordset
Set conRS = New ADODB.Recordset
conn.Open constr
With conRS
.ActiveConnection = conn
.Open "Select * from [USO_Final].[dbo].[Latlong_Amar]"
Sheet1.Range("A1").CopyFromRecordset conRS
.Close
End With
End Sub
You can do it this way.
Sub ADOExcelSQLServer()
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "YOUR_SERVER_NAME" ' Enter your server name here
Database_Name = "AdventureWorksLT2012" ' Enter your database name here
User_ID = "" ' enter your user ID here
Password = "" ' Enter your password here
SQLStr = "SELECT * FROM [SalesLT].[Customer]" ' Enter your SQL here
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("sheet1").Range("a1:z500") ' Enter your sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
Here is another option for you to muse over.
Sub TestMacro()
' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=NORTHWIND.MDF;"
'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"
'Now open the connection.
cnPubs.Open strConn
' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
With rsPubs
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "SELECT * FROM Categories"
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A1").CopyFromRecordset rsPubs
' Tidy up
.Close
End With
cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing
End Sub

Cannot connect to sql server with VBA (Run-time error (80040e14))

I am trying to connect to my Sql database and get one simple recordset out of it running this code:
Sub ConnectToSQL()
Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
With cnn
.ConnectionString = "File Name=C:\inetpub\tc\TEST.udl"
.Open
End With
With cmd
.ActiveConnection = cnn
.CommandType = xlCmdSql
.CommandText = "SELECT * FROM TEST"
End With
rs.Open cmd.Execute, cnn
If Not rs.EOF Then
Sheets(1).Range("A1").CopyFromRecordset rs
rs.Close
Else
MsgBox "No records returned", vbCritical
End If
cn.Close
Set cn = Nothing
Set rs = Nothing
End Sub
As a result I get the run-time error message (80040e14) What could be wrong here?
Thanks,
Do you want to try following? Please do not forget to add Tools> Refernces if necessary.
Sub ConnectToSQL()
Dim cnn As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim ConnectionString As String
Set cnn = New ADODB.Connection
ConnectionString = "File Name=C:\inetpub\tc\TEST.udl"
cnn.Open ConnectionString
cnn.CommandTimeout = 900
strquery = "SELECT * FROM TEST;"
'MsgBox strquery
rs.Open strquery, cnn
If Not rs.EOF Then
Sheets(1).Range("A1").CopyFromRecordset rs
rs.Close
Else
MsgBox "No records returned", vbCritical
End If
cnn.Close
Set cnn = Nothing
Set rs = Nothing
End Sub

Resources