Access VBA, Runtime error 3141 reserved word

Tcurtis

Board Regular
Joined
Aug 15, 2009
Messages
149
Hi,
I am trying to get a bit of code to work but am not successful doing so. I get a runtime error 3141 telling me my select statement contains a reserved word, misspelled word, punctuation missing or incorrect.
Below is the code but I cannot find what is wrong with it. Any help would be really appreciated.
Option Compare Database
Option Explicit
Public wSort As String
Public wField As String
Const Sort1 = "SELECT tbl_tmp_Metrics_1yr_Captured_New.Barcode, FROM tbl_tmp_Metrics_1yr_Captured_ATS "
Private Sub Form_Open(Cancel As Integer)
wField = "Barcode"
wSort = " ASC"
End Sub
Private Sub Label2_Click()
wField = "Barcode"
Sortiraj
End Sub
Sub Sortiraj()
If wSort = " ASC" Then
wSort = " DESC"
Else
wSort = " ASC"
End If
Me.RecordSource = Sort1 & "ORDER BY tbl_tmp_Metrics_1yr_Captured_New." & wField & wSort & ";"
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
There shouldn't be a comma before FROM.
 
Upvote 0
Thanks, taking the comma out clears all the other data in my form. When I leave it in and debug, the line that is highlighted is: Me.RecordSource = Sort1 & "ORDER BY tbl_tmp_Metrics_1yr_Captured_New." & wField & wSort & ";"
 
Upvote 0
Is there an error on that line?

If there is what's the error message?

PS There should be a space between the field you want to sort (order) by and DESC/ASC.
 
Upvote 0
Same error as before but the line is highlighted when I debug. I am just trying to find a way to sort with a single click descending and ascending. Switching back and forth as you click on the header label. I have an example database that this runs in but when I change out the names and put it in mine it does not work.
 
Upvote 0
Did you try putting a space between wField and wSort?

Also, have you tried testing the SQL in Access?
 
Upvote 0
The select statement is wonky, there should definately not be a comma before the FROM as per Norie, also the FROM table name does not match the table referred to by the field.
tbl_tmp_Metrics_1yr_Capture_New
and
tbl_tmp_Metrics_1yr_Capture_ATS
they should be one or the other but not different.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top