I am using ACCESS 2010 and trying to run a simple command in one of my form buttons btnRemove_Click() and for some reason it always returns an error "Run-Time error '2342' A RunSQL action requires an argument consisting of an SQL statement"
my code is here...
Private Sub btnRemove_Click()
Dim srtSQL As String
StrSQL = "SELECT TblProduct.Product
, TblItem.Product_ID
, TblOrder.OrderID
FROM TblProduct
INNER JOIN (TblOrder
INNER JOIN TblItem
ON TblOrder.OrderID = TblItem.OrderID)
ON TblProduct.ProductID = TblItem.Product_ID
WHERE (((TblOrder.OrderID)=3));"
DoCmd.RunSQL StrSQL
Text62.SetFocus
Text62.Text = StrSQL
End Sub
the reason I want to run the DoCmd.RunSQL command as string is that later I will remove the (TblOrder.OrderID)=3 by a field in my form and I can see many people using this command normally in there videos in YouTube and tutorials, but seriously I have no idea why does not work.
Might be a bit late on this one. But the problem is DoCmd.RunSQL is for running Updates/Delete type SQL and not SELECT. You need to Open a Recordset to "use" the Data returned.
Related
I'm trying to run some SQL from a button however I keep getting runtime error 2342 "A runSQL action requires an argument consisting of a SQL statement." I know the statement works when I run it direct.
As far as I can see I have set it up exactly as per the example in the dev centre documents but it's still failing - code details below, any advice on what I am doing stoopid gratefully received! Thanks
Example in document:
Public Sub DoSQL()
Dim SQL As String
SQL = "UPDATE Employees" & _
"SET Employees.Title = 'Regional Sales Manager'" & _
"WHERE Employees.Title = 'Sales Manager'"
DoCmd.RunSQL SQL
End Sub
My code:
Private Sub btnTestNextSeqNum_Click()
Dim GetSeqNum As String
GetSeqNum = "SELECT dt1.MaxAccSeqNum FROM (SELECT Item.AccessionYear AS AccYear, Max(Item.AccessionSequenceNumber) AS MaxAccSeqNum FROM Item GROUP BY Item.AccessionYear) AS DT1 WHERE dt1.AccYear = 2020;"
msgbox GetSeqNum *this shows that the variable does contain the full SQL statement
DoCmd.RunSQL GetSeqNum
End Sub
I think I'm at a loss here. I'm trying to open a recordset with the given SQL-String, but all I get is an Error ORA-00933 SQL Command not properly ended.
I already tried to circle in the error and it seems I made a mistake in the WHERE-clause, but I'm kinda stuck and don't get it where the mistake's at.
strsql = "SELECT QSYS.DOKUMENTE.SDATEINAME
FROM QSYS.DOKUMENTE
INNER JOIN QSYS.RQMS_STAMM
ON QSYS.DOKUMENTE.NLFD = QSYS.RQMS_STAMM.NRQNR"
& _ "WHERE QSYS.DOKUMENTE.NLFD = QSYS.RQMS_STAMM.NRQNR
AND QSYS.RMQS_STAMM.SRQNR = '"& Me.lstReklamenummern.Value &"'"
objrcrd.Open strsql
I am trying to run this code to fire an update query from VBA. Access is giving me a syntax error. I suspect this has to do with the fact that I'm trying to run an update query using an INNER JOIN with a form. Is what I'm trying to do at all possible?
Private Sub Btn_Edit_Data_Click()
Dim db As DAO.Database
Dim UpdateQdf As DAO.QueryDef
Dim UpdateSQL As String
Set db = CurrentDb()
Set UpdateQdf = db.QueryDefs("Qry_Update_Counterparty_Data")
UpdateSQL = "UPDATE Repository_Redux INNER JOIN [Forms]![Frm_Reject_Button] ON Repository_Redux.[Counterparty ID] = [Forms]![Frm_Reject_Button]![Txt_CP_ID] " & _
"SET Repository_Redux.[Counterparty Name] = [Forms]![Frm_Reject_Button]![Txt_CP_Name_Edit]"
UpdateQdf.SQL = UpdateSQL
DoCmd.OpenQuery "Qry_Update_Counterparty_Data"
Set db = Nothing
Set qdf = Nothing
End Sub
I solved it this way, thanks everyone:
UpdateSQL = "UPDATE Repository_Redux SET Repository_Redux.[Counterparty Name] = [Forms]![Frm_Reject_Button]![Txt_CP_Name_Edit] WHERE Repository_Redux.[Counterparty ID] = [Forms]![Frm_Reject_Button]![Txt_CP_ID]"
Just a suggestion, I've not tried this...
On your form you have have an event handler that stores in a global variable (yes, I know that's dodgy) the values from your form that you intend to use in the query. Then you can define a function that reads the global variable. Then you can use the function in the SQL query.
Let us know how you get on.
Googling suggests other have tried this
Global Variable as query parameter - PC Review
How to put global variable name in query
Anybody else got a better "global state machine" to bridge the form to the SQL?
No, you can never join a form in a query (or SELECT FROM a form, for that matter). You can only join in tables or other queries.
You can, however, try to join in a forms record source.
I have the following SQL Statement:
SELECT StockChild.ProductId, Sum(StockChild.Qty) AS SumOfQty,
PRODUCTDETAILS.Title, Sum(SALESORDERchild.Stockout) AS SumOfStockout
FROM (PRODUCTDETAILS
INNER JOIN SALESORDERchild
ON PRODUCTDETAILS.productID = SALESORDERchild.ProductId)
INNER JOIN StockChild
ON PRODUCTDETAILS.productID = StockChild.ProductID
WHERE (((StockChild.ProductID)=[Forms]![StockChild]![ProductId])
AND ((SALESORDERchild.ProductID)=[Forms]![StockChild]![ProductId]))
GROUP BY StockChild.ProductId, PRODUCTDETAILS.Title;
I'm trying to get the summation of values from 2 different tables using the above SQL Statement in access:
1) Sum of StockChild quantity based on productID
2) Sum of Salesorderchild Stockout based on productID
If i query it separately, i managed to get the values that i needed but i'm unable to put it into a single form.
but when i query it together as above, the values jump all over the place and i can't seem to understand why.
And if i add another record in the salesorderchild, of the already existing isbn, all the values will jump as well.
is there something that i am doing wrongly? or how should i go about to tackle this matter.
I added some explanations in the image attached.
Update:
I was trying another method whereby i just did a normal query for the initial stock to be displayed(which worked fine getting the numbers i need)
and over in the Total stockout i was trying out a
=DSum("[stockout]","[SALESORDERchild]","[ProductId]=" & [Forms]!
[StockChild]![ProductId])
but it was returning me a #Name? did i use this correctly?, should i do a vba code instead or is there a way to do it this way?
i tried the following vba code function as an alternative to select out the value but it was telling me the user-defined type not defined. am i missing out something? - (fixed this part by defining the reference of active x data object)
Private Sub Form_Current()
Dim intI As Integer
Dim fld As Field
Dim rst As ADODB.Recordset
Dim pid As String
pid = ProductID.Value
Set rst = New ADODB.Recordset
rst.Open "SELECT Sum(SALESORDERchild.Stockout) AS SumOfStockout FROM
SALESORDERchild WHERE SALESORDERchild.ProductID ='" & pid & "';",
CurrentProject.Connection, adOpenKeyset, adLockOptimistic
tb_stockout.Value = rst.Fields("SumOfStockout")
End Sub
Done Thanks everyone :)
I wonder why I can't execute this SQL query in access 2007 through VBA, which can be executed when i create a query myself:
Private Sub SQL_Click()
Dim curDatabase As Database
Dim RS_REPORT As Recordset
Set curDatabase = CurrentDb
Set RS_REPORT = _
curDatabase.OpenRecordset("REPORT_CONTENT_ARCHIVE", dbOpenDynaset)
strStatement = "SELECT REPORT_CATEGORY1.DESCRIPTION, REPORT_CATEGORY2.DESCRIPTION, REPORT_CATEGORY3.DESCRIPTION, REPORT_RECOMMENDATION.RECOMMENDATION, REPORT_CONTENT_ARCHIVE.REPORT_UID, REPORT_CONTENT_ARCHIVE.CATEGORY1_ID, REPORT_CONTENT_ARCHIVE.CATEGORY2_ID, REPORT_CONTENT_ARCHIVE.CATEGORY3_ID" & _
"FROM (REPORT_CATEGORY1 RIGHT JOIN REPORT_CATEGORY2 ON REPORT_CATEGORY1.CATEGORY1_ID=REPORT_CATEGORY2.CATEGORY1_ID) RIGHT JOIN (REPORT_CATEGORY3 RIGHT JOIN (REPORT_CONTENT_ARCHIVE INNER JOIN REPORT_RECOMMENDATION ON (REPORT_CONTENT_ARCHIVE.CATEGORY3_ID=REPORT_RECOMMENDATION.CATEGORY3_ID) AND (REPORT_CONTENT_ARCHIVE.CATEGORY2_ID=REPORT_RECOMMENDATION.CATEGORY2_ID) AND (REPORT_CONTENT_ARCHIVE.CATEGORY1_ID=REPORT_RECOMMENDATION.CATEGORY1_ID)) ON (REPORT_CATEGORY3.CATEGORY2_ID=REPORT_RECOMMENDATION.CATEGORY2_ID) AND (REPORT_CATEGORY3.CATEGORY1_ID=REPORT_RECOMMENDATION.CATEGORY1_ID) AND (REPORT_CATEGORY3.CATEGORY3_ID=REPORT_RECOMMENDATION.CATEGORY3_ID)) ON (REPORT_CATEGORY2.CATEGORY2_ID=REPORT_CATEGORY3.CATEGORY2_ID) AND (REPORT_CATEGORY2.CATEGORY1_ID=REPORT_CATEGORY3.CATEGORY1_ID)" & _
"WHERE (((REPORT_CONTENT_ARCHIVE.REPORT_UID)=12));"
Set qryMRSA = curDatabase.CreateQueryDef("DATABASE_RECOMMENDATION_1", strStatement)
End Sub
I receive error of 3075, what is it?
Thanks!
This may be something obvious, but if your SQL string is writen in your code exactly as you write it here, you need an aditional space before FROM and WHERE.
Another way to find out what is going on is to print the SQL string, using Debug.Print strStatement, and copying it to a new blank query object, and see if it works.
Hope this is helpful