Does Excel VBA have a limit on the number of SQL Inserts it will write? - sql-server

I am trying to use VBA to insert rows into SQl Server. When I do a small test of about 5 records it works as expected and the new data is inserted. However when I run a full test (100,000 + records) the code runs but no records are inserted. If I copy the INSERT statements into SSMS and execute them all 100,000+ insert with no errors. Am I going over some sort of limit that VBA has? Below is the code I am currently working with.
Public Sub OpenConn()
Set cn = New ADODB.Connection
With cn
.ConnectionString = "Provider=SQLOLEDB;Data Source=USHDEVEVRSTDB\SDEVOLTP4;Initial
Catalog=DataManagement;Integrated Security=SSPI;": .Open
End With
End Sub
Public Sub DisposeConn()
cn.Close
Set cn = Nothing
End Sub
Public Sub WriteData()
With Application
.DisplayAlerts = True: .ScreenUpdating = False: .EnableEvents = False
End With
Dim strSQL As String
Dim wkbTarget As Workbook
Dim Comments As String
Dim Lastrow As Integer
Dim rng As Range
Dim row As Range
Set wkbTarget = ThisWorkbook
On Error GoTo Dispose
Call OpenConn
'SELECT THE RANGE ON THE SQLINSTERTSTRINGS SHEET TO SET AS THE COMMENTS AND INSERT THEM
Sheets("SQLInsertStrings").Activate
Lastrow = Range("A" & Rows.count).End(xlUp).row
Set rng = Range("A2:A" & Lastrow)
For Each row In rng.Rows
strSQL = row
Set cmd = New ADODB.Command
With cmd
Set .ActiveConnection = cn
.CommandText = strSQL
.CommandTimeout = 0
.Execute
End With
strSQL = ""
Next row
'CLOSE SUB
Dispose:
Call DisposeConn
With Application
.DisplayAlerts = True: .ScreenUpdating = True: .EnableEvents = True
MsgBox "Update Complete", vbInformation, "Confimation Message"
End With
End Sub

Related

How to format data inserted from SQL Server into Excel table using VBA?

I'm using the code below to import tables from SQL Server into Excel.
So the old data be removed and new be inserted. I need to insert table to related data. I find something called ListObject but I didn't know how to apply it. Is there any way to insert table after the data are inserted?
Option Explicit
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Sub Connect_To_SQLServer(ByVal Server_Name As String, ByVal Database_Name As String, ByVal SQL_Statement As String)
Dim strConn As String
Dim wsReport As Worksheet
Dim col As Integer
Dim tbl As ListObject
strConn = "Provider=SQLOLEDB;"
strConn = strConn & "Server=" & Server_Name & ";"
strConn = strConn & "Database=" & Database_Name & ";"
strConn = strConn & "Trusted_Connection=yes;"
Set conn = New ADODB.Connection
With conn
.Open ConnectionString:=strConn
.CursorLocation = adUseClient
End With
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = conn
.Open Source:=SQL_Statement
End With
'// here i selected the sheet where the data will be written
Set wsReport = Sheets("RDWH")
With wsReport
'// here I clear the old data and insert new set
wsReport.Select
Selection.Clear
For col = 0 To rst.Fields.Count - 1
.Cells(1, col + 1).Value = rst.Fields(col).Name
Next col
.Range("A2").CopyFromRecordset rst
End With
Set wsReport = Nothing
Call Close_Connections
End Sub
Private Sub Close_Connections()
If rst.State <> 0 Then rst.Close
If conn.State <> 0 Then conn.Close
'// Release Memory
Set rst = Nothing
Set conn = Nothing
End Sub
Sub Run_Report()
Dim Server_Name As String
Server_Name = "NL-1012716\SQLEXPRESS"
Call Connect_To_SQLServer(Server_Name, "project", "SELECT * FROM [2_RDWH_CAST]")
End Sub
It is easier if the sheet name matches the table name and just delete any sheet with that name. Then recreate the sheet with a new table. This example shows how multiple tables can be downloaded in one operation with each query creating a new sheet.
-- Update ; Added sheet name and position parameters to sub Run_Report.
Option Explicit
Sub Run_Reports()
Dim conn As ADODB.Connection
Const SERVER_NAME As String = "NL-1012716\SQLEXPRESS"
Const DB_NAME = "test"
Dim sTable As String
sTable = ThisWorkbook.Sheets("Control").Range("H6").Value
' connect to DB, run reports and disconnect
Set conn = Connect_To_SQLServer(SERVER_NAME, DB_NAME)
Call Run_Report(conn, sTable, "Sheet3", "A3")
conn.Close
Set conn = Nothing
End Sub
Sub Run_Report(ByRef conn As ADODB.Connection, ByVal TABLE_NAME As String, _
SHEET_NAME As String, START_CELL As String)
If Len(TABLE_NAME) = 0 Then
MsgBox "TABLE_NAME missing", vbCritical, "ERROR"
Exit Sub
End If
Dim rst As ADODB.Recordset
Dim wb As Workbook, ws As Worksheet, i As Integer
Dim wsReport As Worksheet, tblResult As ListObject
' query
Dim SQL As String
SQL = "SELECT * FROM [" & TABLE_NAME & "]"
' execute query
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = conn
.Open Source:=SQL
End With
' output
Set wb = ThisWorkbook
' clear sheet
Set wsReport = wb.Sheets(SHEET_NAME)
wsReport.Cells.Clear
With wsReport.Range(START_CELL)
' write headers
For i = 0 To rst.Fields.Count - 1
.Offset(0, i).Value = rst.Fields.Item(i).Name
Next
' write data
.Offset(1, 0).CopyFromRecordset rst
' create table
Set tblResult = wsReport.ListObjects.Add(xlSrcRange, .CurrentRegion, , xlYes)
' add table name
.Offset(-1, 0) = TABLE_NAME
End With
MsgBox "Rows written = " & rst.RecordCount, vbInformation, TABLE_NAME
rst.Close
Set rst = Nothing
End Sub
Function Connect_To_SQLServer(SERVER_NAME As String, DB_NAME As String) As ADODB.Connection
Dim strConn As String
strConn = "Provider=SQLOLEDB;" & _
"Server=" & SERVER_NAME & ";" & _
"Database=" & DB_NAME & ";" & _
"Trusted_Connection=yes;"
Set Connect_To_SQLServer = New ADODB.Connection
With Connect_To_SQLServer
.Open ConnectionString:=strConn
.CursorLocation = adUseClient
End With
End Function

VBA Getting data from SQL Server

I am having trouble when getting data from SQL Server. Here is the code.
Private Sub Form_Load()
Dim blsCritical As Boolean
'----------------------------------------------
ListBox.AddItem "Initializing..."
'----------------------------------------------
Me.Repaint
'----------------------------------------------
ListBox.AddItem "Welcome"
'----------------------------------------------
Me.Repaint
'----------------------------------------------
ListBox.AddItem "Examining your access rights..."
Call ConnectSQLServer
'----------------------------------------------
Me.Repaint
ListBox.AddItem strSQL
'----------------------------------------------
ListBox.AddItem "Opening database connection..."
'----------------------------------------------
Me.Repaint
End Sub
Sub ConnectSQLServer()
Dim cmd As ADODB.Command
Dim conn As ADODB.Connection
Dim strConn As String
Dim par As ADODB.Parameter
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
objMyConn.ConnectionString = "DRIVER=SQL Server;SERVER=CHU-AS-0004;DATABASE=RTC_LaplaceD_DEV;Trusted_Connection=Yes;"
objMyConn.Open
strSQL = "SELECT [currentVersion], [standardVersion] FROM [dbo].[Version]"
If currentVersion = "" Then
MsgBox ("No currentVersion value")
ElseIf Not IsNull(currentVersion) Then
If currentVersion < standardVersion Then
MsgBox ("Upgrade is needed")
ElseIf currentVersion = standardVersion Then
MsgBox ("PASS")
Else
End If
Else
End If
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
End Sub
I have data in SQL Server:
but I cannot get data from SQL Server. When I execute it, 'No CurrentVersion value' message pops up. I don't see any mistakes in my code. Could you help me to solve this problem?(It would be great if you can share your fixed code...)
Just whipped this up for you to show you where you went wrong... untested...
strSQL = "SELECT [currentVersion], [standardVersion] FROM [dbo].[Version]"
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
while objMyRecordset.EOF = false
currentVersion = objMyRecordset!currentVersion
objMyRecordset.MoveNext
wend
If currentVersion = "" Then
MsgBox ("No currentVersion value")
ElseIf Not IsNull(currentVersion) Then
If currentVersion < standardVersion Then
MsgBox ("Upgrade is needed")
ElseIf currentVersion = standardVersion Then
MsgBox ("PASS")
Else
End If
Else
End If
Something like this should do the job.
Sub GetDataFromADO()
'Declare variables'
Set objMyconn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
Dim rc As Long
'Open Connection'
objMyconn.ConnectionString = "Provider=SQLOLEDB;Data Source=SAXAM\SQLEXPRESS;Initial Catalog=AdventureWorks2012; Integrated Security=SSPI;"
objMyconn.Open
'Set and Excecute SQL Command'
Set objMyCmd.ActiveConnection = objMyconn
objMyCmd.CommandText = "select * from [Person].[BusinessEntity] "
objMyCmd.CommandType = adCmdText
objMyCmd.Execute
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyconn
objMyRecordset.Open objMyCmd
'Copy Data to Excel'
'ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)
Application.ActiveCell.CopyFromRecordset (objMyRecordset)
rc = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(rc + 1, 1).Select
'Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Value
objMyconn.Close
End Sub
Here's one more idea. Let's say you had a bunch of Select statements in a bunch of cells from A1, down to whatever, you can add sheets dynamically, and import some sample data on to each sheet, to get a sense of the data structures of multiple tables.
Assuming the following in A1:A3.
SELECT TOP 1000 * FROM [YOUR_DB].[dbo].[YOUR_TABLE1]
SELECT TOP 1000 * FROM [YOUR_DB].[dbo].[YOUR_TABLE2]
SELECT TOP 1000 * FROM [YOUR_DB].[dbo].[YOUR_TABLE3]
Run the script below.
Sub Download_From_Multiple_Tables()
'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
Dim rCell As Range
Dim rRng As Range
Dim sht As Worksheet
Dim LastRow As Long
Set cnn = New ADODB.Connection
'For a trusted Connection, where your user ID has permissions on the SQL Server:
cnn.Open ConnectionString:="Provider=SQLOLEDB.1;" & _
"Data Source=" & "YOUR_SERVER_NAME" & ";Initial Catalog=" & "YOUR_DB_NAME" & _
";TRUSTED_CONNECTION=YES"
'Opens connection to the database
'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
cnn.CommandTimeout = 900
Set sht = ThisWorkbook.Worksheets("Sheet1")
'Ctrl + Shift + End
LRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Set rRng = Sheet1.Range("A1:A" & LRow)
i = 2
For Each rCell In rRng.Cells
LPosition = InStrRev(rCell.Value, "[dbo]") + 5
' Name the newly added worksheet, based on the cell value
Name = Mid(rCell.Value, LPosition + 1, 99)
' Remove [] characters, as these are not permitted in tab names
Name = Replace(Name, "[", "")
Name = Replace(Name, "]", "")
SheetName = Left(Name, 31)
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = SheetName
Worksheets(SheetName).Activate
StrQuery = rCell.Value
'Performs the actual query
rst.Open StrQuery, cnn
'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
' Dump field names to the worksheet
For intFieldIndex = 0 To rst.Fields.Count - 1
ActiveSheet.Cells(1, intFieldIndex + 1).Value = rst.Fields(intFieldIndex).Name
Next intFieldIndex
' Dump the records to the worksheet
ActiveSheet.Cells(2, 1).CopyFromRecordset rst
' Sheets(i).Range("A1").CopyFromRecordset rst
i = i + 1
rst.Close
Next rCell
End Sub

VBA - Call a module to brings a string and complete Query

quite new to VBA. I tried to fix this problem by my own but any of the open threats seems to fit in what I need.
Context:
I have this Macro that brings info from a DDBB and copies it in a new Workbook. I would like to organize different queries in different modules than the main one and call them on demand.
Problem:
I have set my query in a new module as a string, but I get ByRef or Method or data member not found all the time:
Main Sub
Sub Consulta_Sql_ERP()
'Declare variables
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
Dim ws2 As Workbook
Dim iCols As Integer
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB.1;
Data Source=(...);
Initial Catalog=(...);
User ID=(...);
Password=(...);
Persist Security Info=True;"
objMyConn.Open
'Set and Excecute SQL Command'
strSQL = Module4.Querys(Query1)
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
'Open a NewWorkbook
Call NewBook
'Copy Data to the new book
Set ws2 = ActiveWorkbook
ws2.Worksheets("Sheet1").Activate
'Copy headers
For iCols = 0 To objMyRecordset.Fields.Count - 1
Worksheets("Sheet1").Cells(1, iCols + 1).Value = objMyRecordset.Fields(iCols).Name
Next
ActiveSheet.Range("A2").CopyFromRecordset (objMyRecordset)
objMyConn.Close
'Close and save
Call carpetaventas
'ws.SaveAs Savechanges:=True, Filename:="" & Format(Date, "yyyymmdd")
'ws2.Close Savechanges:=True, Filename:="" & Format(Date, "yyyymmdd"),
'RouteWorkbook:="C:\Ventas"
End Sub
The module in which I have the String of my query is "Module4"
Sub in which I have my Query:
Sub Queries(Query1 As String)
Set Query1 = "Select * from table1"
End Sub
It works if I directly introduce the Query after "strSQL" but not if I "call" the Sub on Module4. Any ideas?
Thanks a lot in advance.
strSQL = Module4.Query1()
Function Query1() As String
Query1 = "Select * from table1"
End Sub

How do I write to SQL table using excel VBA

I have a SQL table called Audit. There are two fields in this table called UN and CN. My server name is analive and DB is DW_ALL. I am trying to capture in excel the username and computer name that accesses/opens my workbook or sheet then write that audit information to my SQL table.
Sub UpdateTable()
Dim cnn As ADODB.Connection
Dim uSQL As String
Dim strText As String
Dim strDate As Date
strText = ActiveSheet.Range("b4").Value
''strDate = Format(ActiveSheet.Range("c4").Value, "dd/mm/yyyy")''
Set cnn = New Connection
cnnstr = "Provider=SQLOLEDB; " & _
"Data Source=icl-analive; " & _
"Initial Catalog=DW_ALL;" & _
"User ID=ccataldo;" & _
"Trusted_Connection=Yes;"
cnn.Open cnnstr
''uSQL = "INSERT INTO tbl_ExcelUpdate (CellText,CellDate) VALUES ('" & strText & "', " & strDate & ")"''
''uSQL = "INSERT INTO Audit (UN,CN) VALUES (MsgBox Environ("username"), MsgBox Environ("username""''
uSQL = INSERT INTO Audit (UN,CN) VALUES ('MsgBox Environ("username") ', 'MsgBox Environ("username"'))
Debug.Print uSQL
cnn.Execute uSQL
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub
Connection strings can be tricky things. I rely heavily on ConnectionStrings.com to refresh my memory.
Trusted_Connection and User ID are mutually exclusive. Use trusted connection when you want to log onto SQL Server using your Windows account. Username and password are for logging in with a SQL account.
Assuming you want to use your Windows login; try this:
Provider=SQLNCLI11;Server=analive;Database=DW_ALL;Trusted_Connection=yes;
Here is a sample script that writes to an AccessDB. The SQL Should be similar as well as the needed vba statements. I hope it helps
Also it uses DAO and not Addob connection type.
Private Sub thisbetheshitmane()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim tb As DAO.TableDef
Dim vAr As String
Dim i As Integer
Dim y As Integer
Dim InCombined As Boolean
Dim InOpen As Boolean
Dim vbSql As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Dim StartTime As Double
Dim SecondsElapsed As Double
StartTime = Timer
Set db = DBEngine.OpenDatabase("C:\Users\dzcoats\Documents\Microsoft.accdb")
For Each tb In db.TableDefs
If Len(tb.Connect) > 0 Then
tb.RefreshLink
End If
Next tb
Set rst = db.OpenRecordset("SELECT DISTINCT [Table_Name].Defect FROM [Table_Name] WHERE [Table_Name].Defect IS NOT NULL;")
Dim QResult() As Variant
QResult = rst.GetRows(rst.RecordCount)
For a = LBound(QResult, 2) To UBound(QResult, 2)
vAr = QResult(0, a)
Next a
For y = LBound(QResult, 2) To UBound(QResult, 2)
If vAr <> "Defect" And vAr <> vbNullString And vAr <> "" Then
If InCombined = True And InOpen = True Then
vbSql = "UPDATE [Table_Name] SET [Table_Name].Status ='Bad Defect Number' WHERE ((([Table_Name].Defect)='" & vAr & "'));"
db.Execute vbSql
End If
If InCombined = False And InOpen = True Then
vbSql = "UPDATE [Table_Name] SET [Table_Name].Status ='Completed' WHERE ((([Table_Name].Defect)='" & vAr & "'));"
db.Execute vbSql
End If
End If
Next y
rst.Close
Set rs = Nothing
db.Close
Set db = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub

Print recordset if value exists in array

New to vb and am struggling with returning data from my recordset if variable equals a value in my array.
I think what I've done so far is correct but having trouble with the final bit. I need to script something to say "if value from range equals a value in my array then print the recordset".
I hope someone can help. I'm also new to vb so any suggestions on how to improve my code would be great. Thanks in advance!! Brian
Sub FindCardOrdersv2()
' Initialize variables.
Dim cn As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim provStr As String
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim rsFilter As Range
Dim i As Integer
Dim rng As Variant
Dim payid(1 To 10) As String
Dim tw As ThisWorkbook
Workbooks("cleanse.xlsm").Activate
Worksheets("Sheet1").Activate
' Create new instances
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
' sql query
sql = "SELECT TOP 100 t.tri_transactionidcode," _
& "SUBSTRING(t.tri_reference, 1, 9) AS merchantref," _
& "t.tri_additionalreferencenumber, t.CreatedOn, t.tri_amount, ISNULL(t.tri_paymenttransactiontypeidName, 'Online')" _
& " FROM dbo.tri_onlinepayment t INNER JOIN dbo.tri_transaction tr ON tr.tri_onlinepaymentid = t.tri_onlinepaymentId" _
& " WHERE t.tri_transactionresult = 9"
' Specify the OLE DB provider.
cn.Provider = "sqloledb"
' Specify connection string on Open method.
cn.Open "Data Source=IFL-SQL11;Database=IFL_MSCRM;Trusted_Connection=yes;Integrated Security=SSPI"
' Assign active connection to recordset
Set rs.ActiveConnection = cn
'intMaxCol = rs.Fields.Count
' Define cursors and open sql
With rs
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open sql
End With
For i = 1 To 3
payid(i) = rs.Fields.Item(0)
Debug.Print rs(0)
Debug.Print rs(1)
Debug.Print rs(3)
rs.MoveNext
Next i
'rsFilter = Range("A1:A10")
For Each rsFilter In Range("A1:A10").Cells
If rsFilter.Value = payid Then
Debug.Print rs(1)
rs.MoveNext
End If
Next
'If rs.RecordCount > 0 Then
' With Worksheets("Sheet1")
' .Range("B1:B2").CopyFromRecordset rs
' End With
'End If
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
Add an extra for loop inside to compare every value in the range to the array values
For Each rsFilter In Range("A1:A10").Cells
For i=1 To 3
If rsFilter.Value = payid(i) Then
Debug.Print rs(1)
rs.MoveNext
End If
Next i
Next

Resources