I need to place 3 Parameters into a Select query from Excel VBA to SQL - One of which can just be replaced using a variable. But this query is a query of another query, and the parameters are held within those two other queries. If running this query in Access I'm just prompted by all three to manually type them in - "Start", "End", and "AdvisorName".
Running the code will prompt the "No value given for one or more required parameters" - However, only 1 Parameter is in this query, the other 2 parameters are held within the other two queries inside this query - "Q_SoloFocus_Advisor_QuestionsYes" and "Q_SoloFocus_Advisor_QuestionsNo".
The three parameters are called "Start" (Start Date range), "End" (End Date range), and "AdvisorName" (held within this query).
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim Command As New ADODB.Command
Dim strSQL As String
Set cnn = New ADODB.Connection
cnn.Open ConnectionString:=Cnct
Set rst = New ADODB.Recordset
strSQL = "SELECT Q_SoloFocus_Advisor_QuestionsAll.Advisor,
Q_SoloFocus_Advisor_QuestionsAll.KeyID,
Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text,
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer AS [All],
Q_SoloFocus_Advisor_QuestionsNo.CountOfAnswer AS [No],
Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer
AS Yes," & _"Format(Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer/
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer,'0.0%')
AS Result" & _"
FROM (Q_SoloFocus_Advisor_QuestionsAll
LEFT JOIN Q_SoloFocus_Advisor_QuestionsNo
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsNo.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsNo.KeyID)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsNo.Advisor))
LEFT JOIN Q_SoloFocus_Advisor_QuestionsYes
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsYes.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsYes.Advisor)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsYes.KeyID)" & _
" WHERE (((Q_SoloFocus_Advisor_QuestionsAll.Advisor)=[AdvisorName]));"
'----------------------------------------------------------------------------------
'----------------------------------------------------------------------------------
rst.Open strSQL, cnn, adOpenStatic
rst.MoveFirst
The SQL for the other two queries are similar, one has a where "No" and the other a where not "No" where the parameters are required are:
SELECT tbl_Surveys.Advisor, tbl_QuestionRef.KeyID, tbl_QuestionRef.CallSkill,
tbl_QuestionRef.CallReason, tbl_QuestionRef.MainQ_Text
tbl_QuestionRef.SubQ_Text,
Count(tbl_SurveyAnswers.Answer) AS CountOfAnswer
FROM tbl_QuestionRef
INNER JOIN (tbl_SurveyAnswers
INNER JOIN tbl_Surveys
ON tbl_SurveyAnswers.SurveyLink = tbl_Surveys.ID)
ON tbl_QuestionRef.KeyID = tbl_SurveyAnswers.QuestionRef
WHERE (((tbl_SurveyAnswers.Answer)<>"No"
And (tbl_SurveyAnswers.Answer)<>"N/A"
And (tbl_SurveyAnswers.Answer)<>"0")
AND ((tbl_Surveys.CallDate)>=[Start]
And (tbl_Surveys.CallDate)<=[End]))
GROUP BY tbl_Surveys.Advisor, tbl_QuestionRef.KeyID, tbl_QuestionRef.CallSkill,
tbl_QuestionRef.CallReason,
tbl_QuestionRef.MainQ_Text, tbl_QuestionRef.SubQ_Text;
As you can see, the "Start" and "End" parameters are in this second SQL query... Any ideas how I can put these two parameters into the first SQL function? I can't put the Start and End into the "All" query, as it'll knock out the "Count" part...
Assuming the other two queries are saved and named, instead of calling named queries, incorporate the SQL from the two queries into the main query as derived tables. By doing so, the query will prompt for all parameters. See the JOIN clauses for some clarification.
strSQL = "SELECT Q_SoloFocus_Advisor_QuestionsAll.Advisor, "
Q_SoloFocus_Advisor_QuestionsAll.KeyID,
Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text,
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer AS [All],
Q_SoloFocus_Advisor_QuestionsNo.CountOfAnswer AS [No],
Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer
AS Yes," & _"Format(Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer/
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer,'0.0%')
AS Result" & _"
FROM (Q_SoloFocus_Advisor_QuestionsAll
LEFT JOIN (YOUR FIRST SAVE QUERY SQL) AS Q_SoloFocus_Advisor_QuestionsNo
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsNo.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsNo.KeyID)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsNo.Advisor))
LEFT JOIN (YOUR SECOND QUWERY SQL) AS Q_SoloFocus_Advisor_QuestionsYes
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsYes.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsYes.Advisor)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsYes.KeyID)" & _
" WHERE (((Q_SoloFocus_Advisor_QuestionsAll.Advisor)=[AdvisorName]));"
Related
I have a form with a button that calls and filters a couple of union queries with about 40 SELECT queries total in between them. It then displays the data in a report. Each SELECT query in the Union query collects records from multiple unique tables in the database. I recently had to add a couple more SELECT queries into the union query to grab records from new tables which is when I got the runtime error. It was opening the report fine before I added these SELECT queries so im under the assumption is there are too many SELECT queries in the UNION query. To resolve this issue, do I simply not use a UNION query and find an alternative way to combine records? or is it something in the VBA code that needs adjustment?
Here is my code
Private Sub Command189_Click()
DoCmd.SetWarnings False
DoCmd.Close acReport, "Operator Daily Review"
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("Productivity_WeeklyFinal")
Set qdf2 = db.QueryDefs("qFiller_Names")
strSQL = "SELECT Info_ME_Employees.ID, gs_1_week_finalUnion.SampleID,
gs_1_week_finalUnion.Operator, Format$([TestDate],'m/dd/yyyy') AS Test_Date,
gs_1_week_finalUnion.Test FROM Info_ME_Employees INNER JOIN gs_1_week_finalUnion ON
Info_ME_Employees.Full_Name = gs_1_week_finalUnion.Operator" & _
" WHERE Info_ME_Employees.ID IN (4,5,6,7)AND gs_1_week_finalUnion.TestDate Between (Date()-7-
Weekday(Date(),2)) And (Date()-Weekday(Date(),2)-1) " & _
" ORDER BY gs_1_week_finalUnion.Operator"
strSQL2 = "SELECT Info_ME_Employees.ID, Info_ME_Employees.Full_Name FROM Info_ME_Employees" & _
" WHERE Info_ME_Employees.ID IN (4,5,6,7)"
qdf.SQL = strSQL
qdf2.SQL = strSQL2
DoCmd.OpenReport "Operator Daily Review", acViewReport
Set db = Nothing
Set qdf = Nothing
End Sub
I think that there is a limit of tables that can be included in a UNION query - possibly 32. Therefore your options are:
Create several UNION queries, and then UNION them all together as the final step;
Insert the data into a temp table using each individual part of the union query.
Additionally, there may be some way that your database could be re-designed, as it is quite unusual to have to have some many unions needed.
Regards,
Actually, the statement for this "error" is incorrect!
“Cannot open any more databases.” What microsoft should have said here is that no more links to a database can be opened. That is why adding more UNIONs caused this error. Because each separate reference to a link to an object (table or query) causes another link (microsoft uses the term "database") to be opened.
I am using two SQL queries in VBA that i believe they could be done in one, but I cant get it to work. I Want to turn the VBA portion into a Query outside of VBA, the VBA keeps breaking my file due to the amount of data it processes. (By break i mean it gives a message that says "this file is not a valid database" rendering the file corrupted). I search for that error but all i found was not related to breaking because of VBA code.
Anyways, here are the two queries ran with VBA.
SELECT ET.VerintEID AS EID, Sum(ET.ExceptMin)/60 AS Exeptions
FROM Tbl_VExceptTime AS ET
INNER JOIN Tbl_VCodes ON ET.Exception = Tbl_VCodes.Exception
WHERE (ET.ExceptDate Between #" & sDate & "# And #" & eDate & "#)
GROUP BY ET.VerintEID, Tbl_VCodes.IsApd
HAVING Tbl_VCodes.IsApd = ""OFF"";
I loop these results to update a table.
Do While Not .EOF
SQL = "UPDATE Tbl_AttendanceByAgent SET EXC = " & recSet.Fields(1).Value & _
" WHERE VerintID = '" & recSet.Fields(0).Value & "'"
CurrentDb.Execute SQL
.MoveNext
Loop
I know that i can save the results from the first query into a table and without looping I can update the main table with another SQL query, but I believe it can be done on a single SQL. I have tried using an UPDATE with a SELECT of the first query but it just errors out on me with an invalid syntax.
Yes this could be achieved in one single query as shown below
UPDATE Tbl_AttendanceByAgent
SET Tbl_AttendanceByAgent.EXC = t2.Exeptions
from Tbl_AttendanceByAgent t1
inner join (
SELECT ET.VerintEID AS EID, Sum(ET.ExceptMin)/60 AS Exeptions
FROM Tbl_VExceptTime AS ET
INNER JOIN Tbl_VCodes as TV ON ET.Exception = TV.Exception
WHERE (ET.ExceptDate Between #" & sDate & "# And #" & eDate & "#)
GROUP BY ET.VerintEID, TV.IsApd
HAVING Tbl_VCodes.IsApd = 'OFF'
) AS t2 on t2.EID = t1.VerintID
Note: I suppose you will replace sDate, eDate with values within your code
This question is an answer to the described errors and the given code, although it technically does not answer the request for a single SQL statement. I started adding a comment, but that's just too tedious when this answer box allows everything to be expressed efficiently at once.
First of all, referring to CurrentDb is actually NOT a basic reference to a single object instance. Rather it is more like a function call that generates a new, unique "clone" of the underlying database object. Calling it over and over again is known to produce memory leaks, and at the least is very inefficient. See MS docs for details.
Although the given code is short, it's not sweet. Not only is it repeatedly creating new database objects, it is repeatedly executing an SQL statement to update what I assume is a single row each time. That also entails regenerating the SQL string each time.
Even if executing the SQL statement repeatedly was an efficient option, there are better ways to do that, like creating a temporary (in-memory) QueryDef object with parameters. Each loop iteration then just resets the parameters and executes the same prepared SQL statement.
But in this case, it may actually be more efficient to load the table being updated into a DAO.Recordset, then use the in-memory Recordset to search for a match, then use the recordset to update the row.
I suspect that addressing a couple of those issues would make your VBA code viable.
Dim db as Database
Set db = CurrentDb 'Get just a single instance and reuse
Dim qry as QueryDef
SQL = "PARAMETERS pEXC Text ( 255 ), pID Long; " & _
" UPDATE Tbl_AttendanceByAgent SET EXC = pEXC " & _
" WHERE VerintID = pID"
set qry = db.CreateQueryDef("", SQL)
'With recSet '???
Do While Not .EOF
qry.Parameters("pEXC") = recSet.Fields(1).Value
qry.Parameters("pID") = recSet.Fields(0).Value
qry.Execute
.MoveNext
Loop
'End With recSet '???
'OR an alternative
Dim recUpdate As DAO.Recordset2
Set recUpdate = db.OpenRecordset("Tbl_AttendanceByAgent", DB_OPEN_TABLE)
Do While Not .EOF
recUpdate.FindFirst "VerintID = " & recSet.Fields(0).Value
If Not recUpdate.NoMatch Then
recUpdate.Edit
recUpdate.Fields("EXC") = recSet.Fields(1).Value
recUpdate.Update
End If
.MoveNext
Loop
I realized in commenting on Gro's answer, that the original query's aggregate clauses will produce unique values on EID, but it then becomes obvious that there is no need to group on (and sum) values which do not have Tbl_VCodes.IsApd = 'OFF'. The query would be more efficient like
SELECT ET.VerintEID AS EID, Sum(ET.ExceptMin)/60 AS Exeptions
FROM Tbl_VExceptTime AS ET
INNER JOIN Tbl_VCodes ON ET.Exception = Tbl_VCodes.Exception
WHERE (ET.ExceptDate Between #" & sDate & "# And #" & eDate & "#)
AND Tbl_VCodes.IsApd = 'OFF'
GROUP BY ET.VerintEID;
BTW, you could consider implementing the same temporary QueryDef pattern as I showed above, then you'd change the first WHERE expression to something like
PARAMETERS PsDate DateTime, PeDate DateTime;
...
WHERE (ET.ExceptDate Between [PsDate] And [PeDate])
...
I have 2 tables, one of them as been imported from an Excel file and therefore i created the 2nd one in order to gather some info from the imported and generate in the end a new Excel Sheet to be imported in another place.
My question here is:
I have table a with ID's, names, account status, etc.
The second table has the ID's imported from table a, and now i want to generate a commentary (a value with the concatenation from the several columns in table a) like this:
DATE: 20/12/2017 | FirstName LastName | ID: 123456
For this i prepared by table b with the ID's already and my code in MS ACCESS VBA at the moment is:
CurrentDb.Execute "UPDATE a " _
& " SET a.Commentary = 'Date: ' + CONVERT(nvarchar,GETDATE(),103) + ' | FirstName LastName | ID: ' + b.ID " _
& " FROM tableA a " _
& " INNER JOIN tableB b " _
& " ON a.IdNum = b.ID"
I don't know why this gives me the error of:
Run-time error '3075': Syntax error (missing operator) in query
expression
....(the above code)
Tried to modify the code to more VBA language, more MS-Access language but with no luck.
Thank you,
Diogo
Your syntax looks like T-SQL, if you're going to use CurrentDb.Execute, you need to use Jet/ACE SQL. That means: specifying ALL tables directly after UPDATE, no CONVERT, no GETDATE(), and & as the preferred concatenation operator.
CurrentDb.Execute "UPDATE tableA a INNER JOIN tableB b ON a.IdNum = b.ID" _
& " SET a.Commentary = 'Date: ' & Date() & ' | FirstName LastName | ID: ' & b.ID "
Note that I assume you're using linked tables, since I don't see a schema specified anywhere.
No. You will need a connection string and a saved or created pass-through query that uses this connection.
Then set the SQL property of this query to your SQL string and execute the query.
Or - if you have the tables linked - do as Erik suggests.
You could also use an ADODB command to pass the query directly to the SQL server.
You'd want to add some error handling, but something like this will work as a starting point:
Public Sub ExecuteStatementOnSqlServer(sql As String)
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = bf_sqlServerConnection
.CommandType = adCmdText
.CommandText = sql
.Execute
End With
Set cmd = Nothing
End Sub
There are three mdb files in folders on a network drive that may hold the required record(s). How do I determine which db holds the record(s), ideally without data transfer/linking/etc.? Then a single SQL or DAO select can get the data from the correct db. Note: I'm trying to use Access as a front end to SQL using existing Access data spread all around the network drives.
My current solution of configuring 3 DAO objects and checking for no results, in succession until found, seems to load the remote tables to the local recordset and takes too long.
Is there a way to use IF EXISTS in this scenario?
This code throws "Invalid SQL statement; expected DELETE,INSERT,PROCEDURE,SELECT,OR UPDATE" error but is generally what I'd like to do :
Dim strSQL As String
Dim strSku As String
Dim intDbToSearch As Integer
strSku = DLookup("SKUNo", "tblCurrentItem") 'Note: this returns valid SKU#
strSQL = "IF EXISTS(SELECT xxTable.SKUNo "
strSQL = strSQL & "FROM [S:\Our Inventory\Cust Sleeves.mdb].[xxTable] "
strSQL = strSQL & "Where xxTable.SKUNo = " & "'" & strSku & "') Then intDbToSearch = 1"
DoCmd.RunSQL strSQL
This is one of three IF Exists that would run if SKUNo not found in db 1 or 2.
Ultimately intDbToSearch should point to db 1,2,or 3 if SKUNo found or 0 if not.
Thanks
In the end, I pushed usage rules for the 3 databases upstream and can now predetermine which database to search. Thanks again for your input.
Will the sought for SKU always occur in only 1 of the tables?
If you don't want to set table links or use VBA recordsets, only other approach I can see is a query object with a dynamic parameter that references a form control for the SKU input. No idea if this will be faster and will need a query for each remote table.
SELECT SKUNo FROM xxTable IN "S:\Our Inventory\Cust Sleeves.mdb" WHERE SKUNo = Forms!formname!cbxSKU
Then just do DCount on the query.
Dim intDbToSearch As Integer
If DCount("*", "xxQuery") > 0 Then
intDbToSearch = 1
End If
Could UNION the SELECT statements so would have only 1 query object to work with.
SELECT "x1" AS Source, SKUNo FROM xxTable IN "S:\Our Inventory 1\Cust Sleeves.mdb" WHERE SKUNo = Forms!formname!cbxSKU
UNION SELECT "x2", SKUNo FROM xxTable IN "S:\Our Inventory 2\Cust Sleeves.mdb" WHERE SKUNo = Forms!formname!cbxSKU
UNION SELECT "x3", SKUNo FROM xxTable IN "S:\Our Inventory 3\Cust Sleeves.mdb" WHERE SKUNo = Forms!formname!cbxSKU;
How about a simple Function to check if exists by passing the table name and value?
Something like this:
Public Function ExistInTable(Byval TableName As String, ByVal Value As String) As Boolean
ExistInTable = (DCount("*", TableName, "[SKUNo]='" & Value & "'" > 0)
End Function
To call it:
Sub Test()
If ExistInTable("T1", "Whatever") Then 'Exists in T1
If ExistInTable("T2", "Whatever") Then 'Exists in T2
'....
End Sub
I am trying to run an Update Query in VBA and am at a lost as to what I'm supposed to write for the code. I'm running a query to find the most recent date from a table. That query works fine. Now I want to run an update query to update another table's date field to equal to the date that was queried. Here is what I have:
Dim Date1 As Date
Dim newdate1
'selects datadate 1
Date1 = CurrentDb.OpenRecordset("Select Max(Date1_Event) from TBL_Event WHERE ID = '" & [Forms]![FRM_Main]![ID] & "'")(0)
'update datadate 1
newdate1 = CurrentDb.OpenRecordset("Update Tbl_Name set CollectionDate = DataDate1 WHERE PID = '" & [Forms]![FRM_Main]![ID] & "'")(0)
Is there a way to run an update query like this? Thank you.
Action queries (DELETE, UPDATE, INSERT INTO) are to be executed (CurrentDb.Execute) while SELECT queries are to be opened as recordsets (CurrentDb.OpenRecordset).
Additionally, consider using parameterization to avoid any need of quote enclosure or string concatenation in query. And here the max date is calculated with domain aggregate, DMax(), instead of opening another query.
Dim strSQL As String
Dim qdef As Querydef
' PREPARE SQL STATEMENT
strSQL = "PARAMETERS [MaxDateParam] Date, [FormIDParam] Long;" _
& "UPDATE Tbl_Name SET CollectionDate = [MaxDateParam]" _
& " WHERE PID = [FormIDParam];"
' BUILD TEMP QUERY
Set qdef = CurrentDb.CreateQueryDef("", strSQL)
' BIND PARAMETERS
qdef!MaxDateParam = DMax("Date1_Event", "TBL_Event", "ID=" & [Forms]![FRM_Main]![ID])
qdef!FormIDParam = [Forms]![FRM_Main]![ID]
' EXECUTE ACTION
qdef.Execute dbFailOnError
Set qdef = Nothing
Though above may look unusual and slightly more lines. Don't be intimidated and run for the easy 1-2 lines. Parameterization is a programming industry best practice not just in VBA but across all general purpose languages that run dynamic SQL queries using values from user input.