I'm trying to write VBA code to get SQL data into Excel. Everything works fine except the WHERE condition. I think the problem may be with quotation. This is my query:
Sub Engineering_Milestone()
Dim v_project As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
v_project = Worksheets("Parameters").Range("B1").Value
'cn.Open "Provider = x.1; Data Source=x; Initial Catalog=x; Integrated Security=x"
cn.Open "Provider = Sx; Data Source=x; Initial Catalog=x; Integrated Security=x"
Worksheets("Engineering_Milestone").Range("A2:G5000").ClearContents
sql = " SELECT A.ENGINEER_ID, B.[Description], B.BUDGET_APPROVED, A.MILESTONE, A.[DESCRIPTION], A.PCT_COMPLETE, A.SCHEDULE_DATE FROM X as A Inner Join X as B on A.ENGINEER_ID = B.ENGINEER_ID WHERE B.Project_ID = " & "'" & v_project & "'" and A.Project_ID = " & "'" & v_project & "'"
rs.Open sql, cn
Sheets("Engineering_Milestone").Cells(2, 1).CopyFromRecordset rs
rs.Close
cn.Close
End Sub
It works fine when the SQL query has one condition i.e ...where B.Project_ID = " & "'" & v_project & "'" (without second condition -> and A.Project_ID = " & "'" & v_project & "'").
I'm very new to this so would be grateful if anyone can help...Many thanks.
As I said never write SQL code by string concatenation, use parameters. After seeing your code it is now a little bit easier:
Sub Engineering_Milestone()
Dim v_project As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Dim cmd as ADODB.Command
Set cn = New ADODB.Connection
v_project = Worksheets("Parameters").Range("B1").Value
'cn.Open "Provider = x.1; Data Source=x; Initial Catalog=x; Integrated Security=x"
cn.Open "Provider = Sx; Data Source=x; Initial Catalog=x; Integrated Security=x"
Worksheets("Engineering_Milestone").Range("A2:G5000").ClearContents
sql = "SELECT A.ENGINEER_ID, B.[Description], B.BUDGET_APPROVED, " & _
" A.MILESTONE, A.[DESCRIPTION], A.PCT_COMPLETE, A.SCHEDULE_DATE" & _
" FROM X as A" & _
" Inner Join X as B " & _
" on A.ENGINEER_ID = B.ENGINEER_ID and B.Project_ID = A.Project_ID" & _
" WHERE B.Project_ID = ?"
set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandText = sql
cmd.Parameters.Append cmd.CreateParameter("#projectId", adVarchar)
cmd.Parameters("#projectId").Value = v_project
Set rs = cmd.Execute()
Sheets("Engineering_Milestone").Cells(2, 1).CopyFromRecordset rs
rs.Close
cn.Close
End Sub
NOTE: Your SQL is really vague. You are doing a self join just to create some kind of cartesian join? Probably in fact engineerId, projectId combinations are unique. If that is correct than you could simplify your SQL:
sql = "SELECT ENGINEER_ID, [Description], BUDGET_APPROVED, " & _
" MILESTONE, [DESCRIPTION], PCT_COMPLETE, SCHEDULE_DATE" & _
" FROM X" & _
" WHERE Project_ID = ?"
You only provided a half of one line of code so I'm can only guess that this is what you're trying for:
"where B.Project_ID = '"& v_project &"'& And A.Project_ID = ' & v_project "'"
Strings can be confusing when entering/exiting multiple types of quotes, but when you're troubleshooting a problem building a string, start be remove all the variables and just using a hard-coded SQL string.
Once that's working, start replacing the values with variables (and appropriate quotes) one at a time.
Consider SQL parameterization, the industry best practice when passing values into SQL queries -not just in VBA or your database but across all langauge interfaces to any databases. This process is more readable and maintainable as you no longer worry about quotes. Plus, code (SQL query) is separated from data (VBA variables).
Using ADO, parameters can be defined and set using the Command Object.
Dim v_project As String, sql As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
v_project = Worksheets("Parameters").Range("B1").Value
cn.Open "Provider = Sx; Data Source=x; Initial Catalog=x; Integrated Security=x"
' PREPARED STATEMENT WITH QMARK PLACEHOLDERS
sql = "SELECT A.ENGINEER_ID, B.[Description], B.BUDGET_APPROVED, A.MILESTONE," _
& " A.[DESCRIPTION], A.PCT_COMPLETE, A.SCHEDULE_DATE" _
& " FROM X AS A INNER JOIN X as B ON A.ENGINEER_ID = B.ENGINEER_ID" _
& " WHERE B.Project_ID = ? AND A.Project_ID = ?"
' COMMAND OBJECT
Set cmd = New ADODB.Connection
With cmd
.ActiveConnection = cn ' CONNECTION OBJECT
.CommandText = sql
.CommandType = adCmdText
' BINDING PARAMETERS
.Parameters.Append .CreateParameter("a_projid", adVarChar, adParamInput, , v_project)
.Parameters.Append .CreateParameter("b_projid", adVarChar, adParamInput, , v_project)
End With
' ASSIGN TO RECORDSET
Set rs = cmd.Execute
With Worksheets("Engineering_Milestone")
.Range("A2:G5000").ClearContents
.Cells(2, 1).CopyFromRecordset rs
End With
rs.Close: cn.Close
Set cmd = Nothing: Set rs = Nothing: Set cn = Nothing
Never write SQL code like that concatenating strings. Instead simply use parameters. ie: (say vProject is integer)
.. where B.Project_ID = ? And A.Project_ID = ?
cmd.Parameters.Append .CreateParameter("#projectId", adInteger, adParamInput, 0, vProject)
cmd.Parameters.Append .CreateParameter("#projectId", adInteger, adParamInput, 0, vProject)
Note: cmd is your ADODB.Command object that you use for your command.
Related
For the code listed below, it runs fine except for the first SQL query. I'm pulling address and state information from the workbook, and running a query on the information to find the count of how many times the address appears in the table. If I run the code and stop it before the query is sent to Access, I can pull the query command from the Immediate window, go to Access, and run the query no problem. However, if I just run the VBA program and have it send the query to Access, I keep getting 0 for the result. So long story short, the query will run in Access and provide the correct result, but when Excel VBA sends the query to Access, I keep getting zero for the result (and no error messages). Any help would be greatly appreciated.
Dim DatabaseFileName As String, connectionstring As String
connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DatabaseFileName & "; Persist Security Info=False;"
Dim conn As New ADODB.Connection
conn.Open connectionstring
Dim rs As New ADODB.Recordset, SQL As String
Dim ExecSQL As New ADODB.Command
With ThisWorkbook.Sheets(1)
For I = 2 To 1235
SQL = ""
If .Cells(I, 7) <> "" Then
SQL = "SELECT Count(VRSC_CUSTOMER_SITES.SITE_ID) AS GCOUNT into [GVRCount1] "
SQL = SQL & "FROM (VRSC_CUSTOMER_SITES) "
SQL = SQL & "WHERE ((VRSC_CUSTOMER_SITES.SITE_STREET Like " & Chr(34) & .Cells(I, 7) & Chr(34) & ") AND ((VRSC_CUSTOMER_SITES.SITE_ST)="
SQL = SQL & Chr(34) & .Cells(I, 5) & Chr(34) & ") AND ((VRSC_CUSTOMER_SITES.SITE_PHONE) Not Like ""999*""));"
rs.Open SQL, conn
SQL = "SELECT * FROM [GVRCount1]"
rs.Open SQL
.Cells(I, 8).CopyFromRecordset rs
End If
Next
End With
With ThisWorkbook.Sheets(2)
.Range("A1").CopyFromRecordset rs
End With
conn.Close
End Sub
Essentially, the issue is due to the LIKE operator. Whenever you run an Access query over an ODBC/OLEDB connection, the wildcard to use is the current ANSI version %. However, in Access GUI, the wildcard uses the older version, *. See MSDN docs discussing this wildcard usage.
To be compatible between Excel and Access (VBA or GUI), consider undocumented ALIKE operator to only use %. Additionally, use ADO parameterization using ADO command and avoid concatenation of values to SQL statement. Below replaces the first LIKE with = since no wildcard is used and the make-table action using INTO was removed. Also, New is removed from any Dim lines.
Dim DatabaseFileName As String, connectionstring As String, SQL As String
Dim conn As ADODB.Connection, rs As ADODB.Recordset, ExecSQL As ADODB.Command
Dim I As Long
connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" _
& DatabaseFileName & "; Persist Security Info=False;"
Set conn = New ADODB.Connection
conn.Open connectionstring
' PREPARED STATEMENT WITH ? PLACEHOLDERS
SQL = "SELECT COUNT(v.SITE_ID) AS GCOUNT " _
& "FROM VRSC_CUSTOMER_SITES v " _
& "WHERE v.SITE_STREET = ? " _
& " AND v.SITE_ST = ? " _
& " AND v.SITE_PHONE NOT ALIKE '999%';" _
For I = 2 To 1235
If ThisWorkbook.Sheets(1).Cells(I, 7) <> "" Then
Set ExecSQL = New ADODB.Command
With ExecSQL
.ActiveConnection = conn
.CommandText = SQL
.CommandType = adCmdText
' BIND PARAMETERS
.Parameters.Append .CreateParameter("street_param", adVarchar, adParamInput, 255, ThisWorkbook.Sheets(1).Cells(I, 7))
.Parameters.Append .CreateParameter("st_param", adVarchar, adParamInput, 255, ThisWorkbook.Sheets(1).Cells(I, 5))
' EXECUTE QUERY AND BIND INTO RECORDSET
Set rs = .Execute
End With
ThisWorkbook.Sheets(1).Cells(I, 8).CopyFromRecordset rs
End If
Next I
With ThisWorkbook.Sheets(2)
.Range("A1").CopyFromRecordset rs
End With
Debugger returns automation error when running the following :
Private Sub setDB()
Dim SQL As String
Dim Var As String
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
Var = Worksheets(3).Range("B2").Value
SQL = "UPDATE ps_product SET ean13='" & Var & "' WHERE id_product=12"
conn.Execute (SQL)
However when I assign a value to Var like this: Var=10, the code runs fine. Am I missing something here? I have searched on the internet for several days and I didn't find anything similar. Can some1 help or maybe send a link to a similar issue, please?
Consider parameterization, the preferred method to bind application layer values to executed SQL queries. ADO supports this approach with Command parameters. This avoids messy concatenation, quote punctation, and escape needs if string value contains special characters like single quotes.
Dim Sql As String, Var As String
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command ' NEW OBJECT TO INITIALIZE
Set conn = New ADODB.Connection
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
' PREPARED STATEMENT WITH PLACEHOLDER (NO QUOTES OR CONCATENATION)
Sql = "UPDATE ps_product SET ean13=? WHERE id_product=12"
' CONVERT TO NEEDED TYPE
Var = CStr(Worksheets(3).Range("B2").Value)
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = conn
.CommandText = Sql
.CommandType = adCmdText
' BIND PARAMS AND DEFINE TYPE AND LENGTH
.Parameters.Append .CreateParameter("prm", adVarChar, adParamInput, 255, Var)
' EXECUTE ACTION
.Execute
End cmd
The query is working when the desired cell is passed as a parameter. (Thx to #Parfait for the hint)
Some useful info which helped me figure this out can be found here:
VBA, ADO.Connection and query parameters
https://learn.microsoft.com/en-us/sql/ado/guide/data/creating-and-executing-a-simple-command?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/ado/guide/data/passing-parameters-to-a-named-command?view=sql-server-ver15
Working example:
Private Sub setDB()
Dim Cm As New ADODB.Command
Dim Rs As New ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Test = Worksheets(3).Range("B2").Value
CommandText = "UPDATE ps_product SET ean13=? WHERE id_product=12;"
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
Cm.CommandText = CommandText
Cm.CommandType = adCmdText
Cm.Name = "Var"
Set Cm.ActiveConnection = conn
conn.Var Test, Rs
End Sub
I am trying to assign variables value from recordset and insert values into an Access table. I also need to clear the table and insert new set of data before inserting. The recordset is from a stored procedure in SQL Server. Following does not seem to work:
Dim conn As ADODB.Connection, cmd As ADODB.Command, rst As
ADODB.Recordset
Dim Itm As String, JobNo As Integer, RevNo As Integer, DUStatus As Date, LDUStatus As Date, UTrigger As String
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider='sqloledb';Data Source=SERVER;Initial Catalog='Database';Integrated Security='SSPI';"
conn.Open
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = conn
.CommandText = "rg_ItemsQuerySP"
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter("#JobNo", adInteger, adParamInput, , TempJobNo)
.Parameters.Append .CreateParameter("#RevNo", adInteger, adParamInput, , TempRevNo)
End With
Set rst = cmd.Execute
If rst.EOF Then Exit Function
rst.MoveLast
rst.MoveFirst
With rst
Do While Not .EOF
Itm = rst.Fields("Item")
JobNo = rst.Fields("Job No")
RevNo = rst.Fields("Revision No")
DUStatus = rst.Fields("DateUpdatedStatus")
LDUStatus = rst.Fields("LastDateUpdatedStatus")
UTrigger = rst.Fields("UpdateTrigger")
DoCmd.RunSQL ("INSERT INTO ItemsQuerySP_Temp values " & Itm & ", " & JobNo & ", " & RevNo & ", " & DUStatus & ", " & LDUStatus & ", " & UTrigger & ";")
rst.MoveNext
Loop
End With
conn.Close
Set conn = Nothing
Possibly your issue is the lack of quotes around string variables which would raise SQL error. Since you use ADO parameters, continue to use parameters via QueryDef, avoiding string concatenation (i.e., ampersands) or punctuation (i.e., quotes):
SQL (save below as an MS Access saved query, adjust types as needed: Text, Long, Double, etc.)
PARAMETERS PrmItm Text, PrmJobNo Text, PrmRevNo Text,
PrmDUStatus Text, PrmLDUStatus Text, PrmUTrigger Text;
INSERT INTO ItemsQuerySP_Temp
VALUES(PrmItm, PrmJobNo, PrmRevNo,
PrmDUStatus, PrmLDUStatus, PrmUTrigger)
VBA (relevant section)
Dim qdef AS QueryDef
' ... same as above...
Set qdef = CurrentDb.QueryDefs("mySavedQuery")
With rst
Do While Not .EOF
' BIND PARAMETERS
qdef!PrmItm = rst.Fields("Item")
qdef!PrmJobNo = rst.Fields("Job No")
qdef!PrmRevNo = rst.Fields("Revision No")
qdef!PrmDUStatus= rst.Fields("DateUpdatedStatus")
qdef!PrmLDUStatus = rst.Fields("LastDateUpdatedStatus")
qdef!PrmUTrigger = rst.Fields("UpdateTrigger")
' EXECUTE ACTION
qdef.Execute dbFailOnError
.MoveNext
Loop
End With
Set qdef = Nothing
Disclaimer: I am new to VBA.
I hope to pass the value in the SQL query (30881570) through a field on my Excel sheet. I have tried a few different things.
Private Sub cmdImport_Click()
Call cmdClear_Click
Dim conn As New ADODB.Connection, cmd As New ADODB.Command, rs As New ADODB.Recordset
With conn
.ConnectionString = _
"Provider=SQLOLEDB; " & _
"Data Source=PRGTAPPDBSWC019; " & _
"Initial Catalog=DETEP;" & _
"Integrated Security=SSPI;"
.Open
End With
With cmd
.ActiveConnection = conn
.CommandText = "SELECT * FROM [dbo].[tbl_PMHeader] WHERE [PMHeader_PM_NUM] = '30881570'"
.CommandType = adCmdText
End With
Set rs.Source = cmd
rs.Open
'Need this to populate header row, starting at specified Range
For intColIndex = 0 To rs.Fields.Count - 1
Range("B1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
'This is where your data table will be copied to
ActiveSheet.Range("B2").CopyFromRecordset rs
Worksheets("Sheet1").Columns("B:BB").AutoFit
Worksheets("Sheet1").Range("A25").Formula = "=COUNTA(B:B)-1"
End Sub
It looks like you're already passing that value as criteria for the query's WHERE statement.
If you're asking how to replace that with a value from a worksheet, here's one way:
.CommandText = "SELECT * FROM [dbo].[tbl_PMHeader] " & _
"WHERE [PMHeader_PM_NUM] = '" & Sheets("mySheet").Range("A1") & "'"
...where your worksheet is named mySheet and the value is in cell A1.
This is the simplest method, potentially fine for internal use by trusted parties, but if the value has any ' single-quotes in it, you will get an error.
Worst-case scenario, this method leaves you open to SQL Injection attacks. Depends on your needs (and whether this this is just a school assignment), you may be better of using a parameter query.
See Also:
MSDN Blog : How and Why to Use Parameterized Queries
MSDN Blog : Everything About Using Parameters from Code
Private Sub cmdImport_Click()
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim sqlStr As String
With conn
.ConnectionString = _
"Provider=SQLOLEDB; " & _
"Data Source=PRGTAPPDBSWC019; " & _
"Initial Catalog=DETEP;" & _
"Integrated Security=SSPI;"
.Open
End With
orderno = Sheets("Sheet1").Range("A22")
strSql = "SELECT * FROM [dbo].[tbl_PMHeader] " & _
"WHERE [PMHeader_PM_NUM] = " & orderno
With cmd
.ActiveConnection = conn
.CommandText = strSql
.CommandType = adCmdText
End With
'Call cmdClear_Click
Set rs.Source = cmd
rs.Open
'Need this to populate header row, starting at specified Range
For intColIndex = 0 To rs.Fields.Count - 1
Range("B1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
'This is where your data table will be copied to
ActiveSheet.Range("B2").CopyFromRecordset rs
Worksheets("Sheet1").Columns("B:BB").AutoFit
Worksheets("Sheet1").Range("A25").Formula = "=COUNTA(B:B)-1"
End Sub
Im new at trying to construct queries out of vba. I am trying to figure out how to pass a variable inside the VBA syntax. Mind showing me where im dumb?
I tried this below but there's an automation error that pops up. Ive noticed from playing aroudn that automation errors come up when youve just got syntax wrong, so hopefully its something small?
Any help is greatly appreciated
Sub GetDataFromAccess()
Dim cmd As New ADODB.Command, rs As ADODB.Recordset
Dim recordNum As Integer
recordNum = 7
cmd.ActiveConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ashleysaurus\Desktop" & "\" & "xyzmanu3.accdb"
cmd.CommandType = adCmdText
cmd.CommandText = "SELECT * FROM Invoice WHERE OrderNumber <" & "'" & recordNum & "'" & "ORDER BY OrderNumber ASC"
Set rs = cmd.Execute
Sheet1.Range("A2").CopyFromRecordset rs
rs.Close
cmd.ActiveConnection.Close
Debug.Print "Done!"
End Sub
While learning to build VBA queries, consider parameterized queries and avoid any need of quotes! This is an industry best practice across all languages when passing values in dynamic SQL queries.
Sub GetDataFromAccess()
Dim cmd As New ADODB.Command, rs As ADODB.Recordset
Dim recordNum As Integer
recordNum = 7
With cmd
.ActiveConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=C:\Users\Ashleysaurus\Desktop" & "\" & "xyzmanu3.accdb"
.CommandType = adCmdText
.CommandText = "SELECT * FROM Invoice" _
& " WHERE OrderNumber < ? ORDER BY OrderNumber ASC"
End With
cmd.Parameters.Append cmd.CreateParameter("recordNumParam", adInteger, adParamInput, 10)
cmd.Parameters(0).Value = recordNum
Set rs = cmd.Execute
Sheet1.Range("A2").CopyFromRecordset rs
rs.Close
cmd.ActiveConnection.Close
Debug.Print "Done!"
End Sub
Assuming OrderNumber is a number, do not use quotes.
Also make sure you have a space before Order By:
cmd.CommandText = "SELECT * FROM Invoice WHERE OrderNumber <" & recordNum & " ORDER BY OrderNumber ASC"