Check the permission of user when calling `ADODB.connection.open` - sql

I was stucked for many week on the following :
I need to delete and insert to SQL database using VBA Excel.
The code used to connect to database is :
sConnString= "string for the connexion to the database"
conn.Open sConnString, "username", "password"
Debug.Print conn.State
Here the answer is 1, which according to the Microsoft documentation, means that the connexion is opened.
Then I try to execute an SQL query, using the following code :
varSQL = "DELETE FROM mytable WHERE specificColumn = '" & specificVariable & "'"
Set Command1 = New ADODB.Command
With Command1
.ActiveConnection = conn
.CommandType = adCmdText
End With
With Command1
.CommandText = varSQL
.Execute NbRecordsAffected
End With
This returns access denied for the DELETE query
I want to know if the user with username has the permission to do the query, so I can be sure that the error comes form another thing
Thank you in advance

Consider capturing the more informative error via the ADO error collections using VBA error handling where you may receive if using an SQL Server database:
The DELETE permission was denied on the object 'mytable', database 'mydatabase' schema dbo
Sub Run_SQL()
On Error GoTo ErrorHandle
Dim conn As ADODB.Connection, Command1 AS ADODB.Command
'...your full code...
ExitHandle:
' RELEASE RESOURCES
Set Command1 = Nothing: Set conn = Nothing
Exit Sub
ErrorHandle:
' RAISE EVERY CONNECTION ERROR
Dim myError As ADODB.Error
For Each myError In conn.Errors
Msgbox myError.Number & " - " & myError.Description, "RUNTIME ERROR", vbCritical
Next myerror
Resume Exit_Handle
End Sub

Related

VBA SQL: Runtime Error 3704, how can I fix this?

I am trying to run a SQL Query through VBA. (I'm new to VBA)
P.s I have searched so much online but no solution has helped just yet.
I have copied the code from another excel (which works flawlessly) but my SQL Query involves temp tables and the other doesn't (this used to work on previous files that had temp tables). For some reason it just fails and I get the following error on the following line:
Error: 3704 Operation is not allowed when the object is closed'
' Check we have data for OrderIDs.
If Not rsfswdata.EOF Then
My full VBA code;
Dim conn As ADODB.Connection
Dim rsfswdata As ADODB.Recordset
Dim sConnString As String
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=server001;" & _
"Initial Catalog=Dev;" & _
"Integrated Security=SSPI;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'To wait till the query finishes without generating error
conn.ConnectionTimeout = 0
'To wait till the query finishes without generating error
conn.CommandTimeout = 0
' Open the connection and execute.
conn.Open sConnString
Set rsfswdata = conn.Execute(Sheets("SQL Query").Range("A1").Value)
' Check we have data for OrderIDs.
If Not rsfswdata.EOF Then
' Transfer result.
Sheets("test").Cells(3, 2).CopyFromRecordset rsfswdata
' Close the recordset
rsfswdata.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rsfswdata = Nothing
End Sub

VBS Script to Execute SQL statement?

To open, I have never written a vbs script. I have written many SQL scripts, views, developed databases. I have written plenty of VBA in Access applications.
For this, I am just trying to set up a SQL script as a VBS script, so the users don't have to go into SSMS to run it. They can just double-click the VBS script, specify the server and database when prompted, and the quick script will run for them.
This is what I have gotten so far, but I keep getting Microsoft VBScript compilation errors. The latest one is line 3 char 17, which is on a Dim statement. Just wanted to see if anyone can tell if I am missing something fundamental to this script, that is preventing it from compiling or processing correctly.
This is the very short script:
Dim conn
Set conn = createobject("Adodb.Connection")
Dim sConnString As String
Dim SqlStatement As String
sSourceServer = InputBox ("Enter the name of the SQL Server","Enter SQL Server Name","")
If Len(sSourceServer) = 0 Then
MsgBox "No SQL Server was specified.", , "Unable to Continue"
Exit Sub
End if
sSourceDB = InputBox ("Enter the name of the Law SQL Database","Enter Law SQL DB Name","")
If Len(sSourceDB) = 0 Then
MsgBox "No SQL DB was specified.", , "Unable to Continue"
Exit Sub
End if
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=" & sSourceServer & "; Initial Catalog=" & sSourceDB & "; Integrated Security=SSPI;"
MsgBox sConnString
' Open the connection and execute.
conn.Open sConnString
conn.CommandTimeout = 900
SqlStatement = "UPDATE [tablename] " & _
"SET UUID = CASE WHEN CHARINDEX('.',[Filename]) > 1 THEN LEFT(CAST([Filename] AS VARCHAR),CHARINDEX('.',[Filename])-1) ELSE [Filename] END " & _
"WHERE [Filename] IS NOT NULL"
conn.Execute(SqlStatement)
conn.Close
Set rs = Nothing
SqlStatement = vbNullString
MsgBox "All Done! Go Check your results!"
If anyone can help, I'd greatly appreciate it.
Thank you
nevermind. I kept troubleshooting and finally got it to work. For those that this might help, unlike VBA, it's easier not to declare variables as a type. just Dim them and move on. see below:
Dim conn
Set conn = createobject("Adodb.Connection")
Dim sConnString
Dim SqlStatement
StartScript
Sub StartScript()
sSourceServer = InputBox ("Enter the name of the SQL Server","Enter SQL Server Name","")
If Len(sSourceServer) = 0 Then
MsgBox "No SQL Server was specified.", , "Unable to Continue"
Exit Sub
End if
sSourceDB = InputBox ("Enter the name of the Law SQL Database","Enter Law SQL DB Name","")
If Len(sSourceDB) = 0 Then
MsgBox "No SQL DB was specified.", , "Unable to Continue"
Exit Sub
End if
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=" & sSourceServer & "; Initial Catalog=" & sSourceDB & "; Integrated Security=SSPI;"
' Open the connection and execute.
conn.Open sConnString
conn.CommandTimeout = 900
SqlStatement = "UPDATE [tablename] " & _
"SET UUID = CASE WHEN CHARINDEX('.',[Filename]) > 1 THEN LEFT(CAST([Filename] AS VARCHAR),CHARINDEX('.',[Filename])-1) ELSE [Filename] END " & _
"WHERE [Filename] IS NOT NULL"
conn.Execute(SqlStatement)
conn.Close
Set rs = Nothing
SqlStatement = vbNullString
End Sub
MsgBox "All Done! Go Check your results!"
Remember, for those looking to use this as a basis for a script - I'm not doing any checks, so if you don't know your data, this is a dangerous thing to run.
Know your data, backup your data, and if you can, add in some checks, to make sure anything that isn't a select statement, is checked and re-checked before it is run.

Executing a stored procedure on pervasive server

I have a stored procedure on my Pervasive Server called EGC_Expl_BOM_TT and I can execute it with "CALL EGC_Expl_BOM_TT('B-8579-K')" in my SQL query window within Excel, however it will perform the function and then through me an error forcing me to back out of the query window. I found the below VBA code which is designed to execute a stored procedure. I need help adapting it to me specific need. My stored procedure has only one variable input, which I will put in sheet 1 cell A1.
This is my connection string from my Excel query window. I need help formatting it in the VBA code:
Provider=MSDASQL.1;Persist Security Info=True;Extended Properties="DSN=global_EGC;ServerName=fah2.1583;UID=UserIDName;PWD=password;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;DBQ=GLOBALEGC;ClientVersion=11.31.017.000;CodePageConvert=1252;PvClientEncoding=CP1252;PvServerEncoding=CP1252;AutoDoubleQuote=0;"
Function Sproc()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Dim ConnectionString As String
Dim StrSproc As String
Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=MSDASQL.1;DSN=global_EGC;ServerName=fah2.1583;UID=Myusername;PWD=mypassword;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;DBQ=GLOBALEGC;ClientVersion=11.31.017.000;CodePageConvert=1252;PvClientEncoding=CP1252;PvServerEncoding=CP1252;AutoDoubleQuote=0;"
'Opens connection to the database
On Error GoTo SQL_ConnectionError
cnn.Open ConnectionString
On Error GoTo 0
'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 rst = New ADODB.Connection
StrSproc = "set nocount on; "
StrSproc = "CALL EGC_Expl_BOM_TT" + Cells(1, 1)
rst.ActiveConnection = cnn
On Error GoTo SQL_StatementError
rst.Open StrSproc
On Error GoTo 0
If Not rst.EOF And Not rst.BOF Then
Sproc = IIf(IsNull(rst.Fields(0).Value), "(BLANK)", rst.Fields(0).Value)
End If
Exit Function
SQL_ConnectionError:
MsgBox "Error connecting to the server / database. Please check the connection string."
Exit Function
SQL_StatementError:
MsgBox "Error with the SQL syntax. Please check StrSproc."
Debug.Print StrSproc
Exit Function
SQL_ConnectionError:
Msgbox "Error connecting to the server / database. Please check the connection string."
Exit Function
SQL_StatementError:
Msgbox "Error with the SQL syntax. Please check StrSproc."
Debug.Print StrSproc
Exit Function
End Function
The connection string would be something like:
cnn.ConnectionString = "Provider=MSDASQL.1;DSN=global_EGC;ServerName=fah2.1583;UID=UserIDName;PWD=password;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;DBQ=GLOBALEGC;ClientVersion=11.31.017.000;CodePageConvert=1252;PvClientEncoding=CP1252;PvServerEncoding=CP1252;AutoDoubleQuote=0;"
Your statement would be something like:
StrSproc = "EXEC EGC_Expl_BOM_TT" + Cells(1,1)

Error in connecting to Access database from Excel macro

I have to create excel macro which fetches from access database and update appropriate columns in excel worksheet.
I have never done VBA programming before, i am just able to write a code to connect to the database which is giving me error saying "Error in From clause".
Sub Button2_Click()
Dim conn As New Connection
Dim rs As New Recordset
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Users\Xprts8\Documents\shipping.accdb;" & _
"User Id=admin;Password="
conn.Open (strcon)
qry = "SELECT * FROM shipping-table"
rs.Open qry, conn, adOpenKeyset
rs.Close
conn.Close
End Sub
can anybody help me with the following problem
shipping-table is not a valid table name in an SQL statement (because of the hyphen) unless its escaped thusly:
SELECT * FROM [shipping-table]

VBA Error handling on ADODB Connection.Open

I have an ADODB connection in VBA for connecting to an SQLServer database. I want to catch the error that is raised when connection.Open is called and the given database is unreachable.
My code looks like this:
Public Function Connect() As Boolean
On Error GoTo DBError
Dim dbServer As String
Dim dbName As String
Dim dbUser As String
Dim dbPwd As String
dbServer = DatabaseSettings.dbServer
dbName = DatabaseSettings.dbName
dbUser = DatabaseSettings.dbUser
dbPwd = DatabaseSettings.dbPwd
Dim connectionString As String
connectionString = "Server=" & dbServer & ";Database=" & dbName & ";User Id=" & dbUser & ";Password=" & dbPwd
Set conn = New ADODB.Connection
conn.Provider = "sqloledb"
With conn
.ConnectionTimeout = 2
.CursorLocation = adUseClient
.Open connectionString
.CommandTimeout = 0
End With
Connect = True
Exit Function
DBError:
Connect = False
End Function
My problem is that when i try to run this code with an incorrect connectionString an error is raised and shown in a MsgBox and not caught by the "On Error GoTo DBError".
Is there something wrong in my error handling code or do i need to find another way of catching this error?
Thank you for your help. Any suggestions are welcome.
Not sure if this is it, but in the VBE window make sure the Tools...Options...General...Error Trapping option is set to "Break on Unhandled Errors". If it were set to "Break on All Errors" this may bypass your handlers.
Try to use the below, worked here:
With conn
.ConnectionTimeout = 2
.CursorLocation = adUseClient
.ConnectionString = connectionString
.Open
.CommandTimeout = 0
End With