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
Related
Need help for the below listed issues.
I am getting "YearN" as user input to the select statement to Run a Query in VBA for Access Database. The statement works when a number directly entered in where clause say "2027". Not sure how to reference a input variable/object. Please help.
Need help to refresh the record as I am getting runtime error whenever the code trying to execute line "A.open strconnection" saying "The database has been placed in a state by user 'Admin' on machine that prevents it from being opened or locked". Please advise
Dim YearNumber As Long
DoCmd.RefreshRecord
YearN = InputBox("Enter the Record Year to delete:")
If YearN = "" Then
MsgBox "Year not entered. Query exit"
Else
Dim A As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Set A = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\OneDrive - fab\Access_Db\file.Accdb"
strSql = "DELETE FROM APAC_tbl_test WHERE Year= YearN;"
DoCmd.SetWarnings False
A.Open strConnection
Set rs = A.Execute(strSql)
DoCmd.SetWarnings True
Set rs = Nothing
A.Close
Set A = Nothing
End If
End Sub```
If you are trying to have a "pop-up" box where the user inputs a value for the WHERE clause within Access, you don't necessarily need VBA code. Simply write your SQL statement and in the WHERE clause you'll do something like below:
DELETE FROM APAC_tbl_test WHERE Year = [Enter Year:]
Once you run the above code it should prompt user for the year with "pop-up" box that is labelled "Enter Year".
So basically,
WHERE [column name] = [custom prompt message:]
Important to keep the brackets [ ] and the semicolon :.
More information can be found at this link - https://support.microsoft.com/en-us/office/use-parameters-to-ask-for-input-when-running-a-query-c2806d3d-d500-45a8-8507-ec6af351b6ed
I have a query, called "RosterQC" that filters results based on the text object "Text35" in the Form "Quality":
SELECT Record No
FROM TableX
WHERE Date = [Forms]![Quality]![Text35]
Running the query itself is fine, but when I try to run it on VBA. It gives me an error 3061 - too few parameters expected 1. My VBA code as of below,
Sub Duplicate()
DoCmd.RunSQL "Delete * from old_table"
CurrentDb.Execute "Insert Into old_table Select RosterQC.* From RosterQC"
DoCmd.OpenReport "RosterQC_Report", View:=acViewPreview
End Sub
Appreciate if someone please give help me by providing me with some insights or solutions? Thanks.
You must pass the parameter to the query before running it but, in this case, it might be simpler to rewrite the task:
Sub Duplicate()
Dim Sql As String
Sql = "Delete * from old_table"
CurrentDb.Execute Sql
Sql = "Insert Into old_table Select [Record No] From TableX Where [Date] = #" & Format([Forms]![Quality]![Text35], "yyyy\/mm\/dd") & "#"
CurrentDb.Execute Sql
DoCmd.OpenReport "RosterQC_Report", View:=acViewPreview
End Sub
I am attempting to execute a SQL query inside of VBA Code. The query works in MS Access and asks the user to input a value for Customer_Name and Part_Number
What I have done is written the VBA Code in outlook so we can run the macro to execute the query from Outlook. The code I have currently works until the very bottom line on the DoCmd.RunSQL portion. I think I have this syntax incorrect. I need to tell it to run the string of SQL listed above:
Public Sub AppendAllTables()
Part_Number = InputBox("Enter Part Number")
Customer_Name = InputBox("Enter Customer Name")
Dim strsqlQuery As String
Dim Y As String
Y = "YES, Exact Match"
Dim P As String
P = "Possible Match - Base 6"
Dim X As String
X = "*"
strsqlQuery = "SELECT Append_All_Tables.Customer,
Append_All_Tables.CustomerCode, Append_All_Tables.PartNumber,
Append_All_Tables.Description, Append_All_Tables.Vehicle, SWITCH" &
Customer_Name & " = Append_All_Tables.PartNumber, " & Y & ", LEFT(" &
Part_Number & ",12) = LEFT(Append_All_Tables.PartNumber,12)," & Y & ",
LEFT(" & Part_Number & ",6) = LEFT(Append_All_Tables.PartNumber,6)," & P
& ") AS Interchangeability FROM Append_All_Tables WHERE" & Customer_Name
& "Like " & X & Customer_Name & X & "AND
LEFT(Append_All_Tables.PartNumber,6) = LEFT(" & Part_Number & ",6);"
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase "path.accdb"
appAccess.DoCmd.RunSQL "strsqlQuery"
End Sub
Please note, the path has been changed for privacy. The SQL code already works in Access. I am only needing the last line to be evaluated.
If you want to have a datasheet form view show these records you can use
DoCmd.OpenForm
First create a query with the data you want to see, then bind that to your form using the Record Source property, then when you call DoCmd.OpenForm pass in the filter you want.
I'm not following what you're trying to do with SWITCH in your query (is that supposed to be the switch() function? it has no parentheses). But you'll need to adjust that to join to use a Where statement instead.
I agree with a couple of the above posts.
You need to do a Debug.Print of the strsqlQuery variable BEFORE YOU DO ANYTHING! Then evaluate that statement. Does it look right? As Matt says, it doesn't look like you have line continuations, which would make your SQL statement incomplete (and thus, the computer doesn't think its a query at all).
My personal preference is to define the SQL like you have, then create the actual query using that SQL (create query def), and then call that query, because it will now be an actual object in the database. The QUERY can show up as a datasheet without any form requirement, but a pure SQL Statement cannot.
Michael
Remove the quotes.
appAccess.DoCmd.RunSQL "strsqlQuery" to appAccess.DoCmd.RunSQL strsqlQuery
I currently have a split default view
I'm trying to change the field "Email verzonden?" from "Niet verzonden!" to "Verzonden!" when the user presses a button, but only when the checkbox in front of it is checked.
I tried to use an update statement with SQL but I get the following error code:
Run-time error '3464': Data type mismatch in criteria expression.
Does anyone know how to fix this and how to get it to work?
This is the SQL that I'm using:
Private Sub cmdTest_Click()
Dim SQL As String
SQL = "Update artikelfiche SET Verzonden = 'Verzonden!' WHERE Bestellen = -1"
DoCmd.RunSQL SQL
End Sub
Thanks in advance!
I should look better before posting questions.
This is the solution though:
Private Sub cmdTest_Click()
Dim SQL As String
SQL = "Update artikelfiche SET Verzonden = 'Verzonden!' WHERE Bestellen = '-1'"
DoCmd.RunSQL SQL
End Sub
Basically, I forgot the '' at the criteria of Bestellen.
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