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

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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