SQL error message (1 Viewer)

Tom d

Member
Local time
Yesterday, 20:22
Joined
Jul 12, 2022
Messages
47
' strSql = "Delete * from [tlb Blood Pressures] where [date Entered] >= " & fromdate & " AND [date Entered] <= " & todate
strSql = "Select * from [tlb Blood Pressures] where [date Entered] >= " & fromdate & " AND [date Entered] <= " & todate

MsgBox " strSql=" & strSql
DoCmd.RunSQL strSql, dbFailOnError

The select statement produces the follow error on the doCmd.RunSql command
A runsql action requires an argument consisting of an SQL statement.
Runtime error '2342'
The SQL Delete statement works in another module.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 08:22
Joined
May 7, 2009
Messages
19,245
try:

strSql = "Select * from [tlb Blood Pressures] where [date Entered] >= " & Format$(fromdate, "\#mm\/dd\/yyyy\#") & " AND [date Entered] <= " & Format$(todate, "\#mm\/dd\/yyyy\#")
 

Tom d

Member
Local time
Yesterday, 20:22
Joined
Jul 12, 2022
Messages
47
try:

strSql = "Select * from [tlb Blood Pressures] where [date Entered] >= " & Format$(fromdate, "\#mm\/dd\/yyyy\#") & " AND [date Entered] <= " & Format$(todate, "\#mm\/dd\/yyyy\#")
Same error message
 

cheekybuddha

AWF VIP
Local time
Today, 01:22
Joined
Jul 21, 2014
Messages
2,280
RunSQL only works with action queries (UPDATE/DELETE/INSERT etc), not SELECT

To view the records either copy the SQL in the MsgBox and paste into a new query in SQL view, or create a QueryDef in code and open that.

Ask if you're not sure how to do it.
 

sonic8

AWF VIP
Local time
Today, 02:22
Joined
Oct 27, 2015
Messages
998
The SQL Delete statement works in another module.
Please double check your code. In this posting the Delete statement is commented out and replaced by a Select statement. - DoCmd.RunSql expects an action query (Insert, Delete, Update), not a Select statement.
 

Minty

AWF VIP
Local time
Today, 01:22
Joined
Jul 26, 2013
Messages
10,371
You can't RUN a SELECT query, you can only RUN an action query.

EDIT : Blimey is this an Echo Chamber ;)
 

Tom d

Member
Local time
Yesterday, 20:22
Joined
Jul 12, 2022
Messages
47
RunSQL only works with action queries (UPDATE/DELETE/INSERT etc), not SELECT

To view the records either copy the SQL in the MsgBox and paste into a new query in SQL view, or create a QueryDef in code and open that.

Ask if you're not sure how to do it.
How do you do that
 

cheekybuddha

AWF VIP
Local time
Today, 01:22
Joined
Jul 21, 2014
Messages
2,280
Untested, but try something like:
Code:
' strSql = "Delete * from [tlb Blood Pressures] where [date Entered] >= " & Format$(fromdate, "\#mm\/dd\/yyyy\#") & " AND [date Entered] <= " & Format$(todate, "\#mm\/dd\/yyyy\#")
strSql = "Select * from [tlb Blood Pressures] where [date Entered] >= " & Format$(fromdate, "\#mm\/dd\/yyyy\#") & " AND [date Entered] <= " & Format$(todate, "\#mm\/dd\/yyyy\#")

MsgBox " strSql=" & strSql

  Const TMP_QRY As String = "TEMP_QRY"

  On Error Resume Next
' Delete the query if it exists already
  DoCmd.DeleteObject acQuery, TMP_QRY
  On Error GoTo 0
' Create  a query called "TEMP_QRY" using the SQL, and open
  CurrentDb.CreateQueryDef TMP_QRY, strSql
  DoCmd.OpenQuery TMP_QRY
 

Tom d

Member
Local time
Yesterday, 20:22
Joined
Jul 12, 2022
Messages
47
You can't RUN a SELECT query, you can only RUN an action query.

EDIT : Blimey is this an Echo Chamber ;)
I have attached my access data base for you to see., how do you add an action query
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 19:22
Joined
Feb 28, 2001
Messages
27,189
Running a SELECT SQL statement could have just silently done nothing. At least it let you know that the "action" was not going to work right.

So you SELECTed a few records. What was going to come next? Because a RunSQL is a run-and-done type of action. A SELECT is reserved for something with persistence, like maybe using a recordset or maybe feeding a COMBO or LIST BOX. There is no persistence in what you showed. You must ask yourself what you were going to do next... and then tell US so we can better advise you.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 08:22
Joined
May 7, 2009
Messages
19,245
yes, like what the other says. RunSQL is an action command.

check the new code.
 

Attachments

  • Blood Pressure.accdb
    2.5 MB · Views: 86

cheekybuddha

AWF VIP
Local time
Today, 01:22
Joined
Jul 21, 2014
Messages
2,280
I have attached my access data base for you to see., how do you add an action query
Let's rewind a second - what are you actually trying to do?

It seems as if you wish to inspect the records that you are about to delete before deleting them. Is this just for you as developer, or do the users need to see this too?
 

Tom d

Member
Local time
Yesterday, 20:22
Joined
Jul 12, 2022
Messages
47
Running a SELECT SQL statement could have just silently done nothing. At least it let you know that the "action" was not going to work right.

So you SELECTed a few records. What was going to come next? Because a RunSQL is a run-and-done type of action. A SELECT is reserved for something with persistence, like maybe using a recordset or maybe feeding a COMBO or LIST BOX. There is no persistence in what you showed. You must ask yourself what you were going to do next... and then tell US so we can better advise you.
There is a text box on the same form as the button containg AvgOf Systolic
I want to load the text box with the average of the selected table records
Let's rewind a second - what are you actually trying to do?

It seems as if you wish to inspect the records that you are about to delete before deleting them. Is this just for you as developer, or do the users need to see this too?
Forget abut the commented out delete statement that is on another form and works fine.
Running a SELECT SQL statement could have just silently done nothing. At least it let you know that the "action" was not going to work right.

So you SELECTed a few records. What was going to come next? Because a RunSQL is a run-and-done type of action. A SELECT is reserved for something with persistence, like maybe using a recordset or maybe feeding a COMBO or LIST BOX. There is no persistence in what you showed. You must ask yourself what you were going to do next... and then tell US so we can better advise you.
 

Attachments

  • Blood Pressure.accdb
    2.5 MB · Views: 81

cheekybuddha

AWF VIP
Local time
Today, 01:22
Joined
Jul 21, 2014
Messages
2,280
I want to load the text box with the average of the selected table records
Then you probably don't need any code at all!

Try setting the ControlSource of the textbox to:
Code:
=DAvg("Systolic", "[tlb Blood Pressures]", "[date Entered] >= " & Format$(fromdate, "\#mm\/dd\/yyyy\#") & " AND [date Entered] <= " & Format$(todate, "\#mm\/dd\/yyyy\#"))

You may need to adjust the fromdate and todate variables for the names of controls on your form
 

Tom d

Member
Local time
Yesterday, 20:22
Joined
Jul 12, 2022
Messages
47
There is a text box on the same form as the button containing AvgOf Systolic
I want to load the text box with the average of the selected table records

Forget about the commented out delete statement that is on another form and works fine.
The code is on the button on the form frmSelected_statistics
 

Tom d

Member
Local time
Yesterday, 20:22
Joined
Jul 12, 2022
Messages
47
Then you probably don't need any code at all!

Try setting the ControlSource of the textbox to:
Code:
=DAvg("Systolic", "[tlb Blood Pressures]", "[date Entered] >= " & Format$(fromdate, "\#mm\/dd\/yyyy\#") & " AND [date Entered] <= " & Format$(todate, "\#mm\/dd\/yyyy\#"))

You may need to adjust the fromdate and todate variables for the names of controls on your form
I just use the list boxes to get the dates . in the SQL statement that does not work. I am trying to get selected records from the access table.
 

cheekybuddha

AWF VIP
Local time
Today, 01:22
Joined
Jul 21, 2014
Messages
2,280
OK, you need to change your listboxes a little bit. You only need the dates from the table as RowSource, not the whole table!

So, for listboxes lst_from_date and lst_to_date, change:
RowSource: SELECT DISTINCT [Date Entered] FROM [tlb Blood Pressures] ORDER BY [Date Entered];
ColumnCount: 1
ColumnWidths: {empty}
Multi Select: None

Then, for the ControlSource of Text15:
=DAvg("Systolic","[tlb Blood Pressures]","([date Entered] >= " & Format$([lst_from_date],"\#mm\/dd\/yyyy\#") & " OR [lst_from_date] IS NULL) AND ([date Entered] <= " & Format$([lst_to_date],"\#mm\/dd\/yyyy\#") & " OR [lst_to_date] IS NULL)")

Then when you view your form the textbox should update as you select different dates in the listboxes.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 08:22
Joined
May 7, 2009
Messages
19,245
here is your form.
 

Attachments

  • Blood Pressure (1).accdb
    2.5 MB · Views: 83

Tom d

Member
Local time
Yesterday, 20:22
Joined
Jul 12, 2022
Messages
47

Attachments

  • Blood Pressure.accdb
    1 MB · Views: 62
Last edited:

Users who are viewing this thread

Top Bottom