VBA Sql Error

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
339
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
HI All, I am getting a VB runtime error when I am running a function (Code Below)
The Error is Runtime Error -2147217900 (800040e14): The SELECT statement includes a reserved word or an argumetn name that is misspelled or missing, or the punctuation is incorrect.

When I run the query in Access's Query Builder, it runs without error.

Can you please provide some guidance.

Thanks

Code:
Function CheckIfCurrentATBAppointmentIsPending(StudentID As String, NearestATB As Integer) As Boolean
Dim Flag As Boolean
Dim ATBsql As String
ATBsql = "SELECT MATBS.MaxAppt, ATBS.ATBSession AS SID" & _
         "FROM (SELECT tblATBAppointment.StudentID, Max(tblATBAppointment.AppointmentID) AS MaxAppt " & _
         "      FROM tblATBAppointment " & _
         "      GROUP BY tblATBAppointment.StudentID)  AS MATBS  " & _
         "INNER JOIN tblATBAppointment AS ATBS ON MATBS.MaxAppt = ATBS.AppointmentID " & _
         "WHERE (((MATBS.StudentID)='" & StudentID & "'));"
Flag = False

Dim ATBAppt As ADODB.Recordset
Set ATBAppt = New ADODB.Recordset
ATBAppt.ActiveConnection = CurrentProject.Connection
ATBAppt.CursorType = adOpenDynamic
ATBAppt.LockType = adLockOptimistic
ATBAppt.Open ATBsql

If Not ATBAppt.BOF Then
    ATBAppt.MoveFirst
End If


If ATBAppt.BOF And ATBAppt.EOF Then
'If there are no records, return False to allow scheduling
    Flag = False
ElseIf ATBAppt![SID] < NearestATB Then
'If the returned record is prior to  the date of the nearest ATB, return False to allow scheduling
    Flag = False
Else
    Flag = True
End If

ATBAppt.Close
Set ATBAppt = Nothing

CheckIfCurrentATBAppointmentIsPending = Flag

End Function
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
For one thing, there is no space between "SID" and "FROM" in youyr first two lines of your SQL construction:
Code:
ATBsql = "SELECT MATBS.MaxAppt, ATBS.ATBSession AS SID" & _
"FROM (SELECT tblATBAppointment.StudentID, Max(tblATBAppointment.AppointmentID) AS MaxAppt " & _

In creating SQL code in VBA, I would often have it print out to a Message Box so I can see if I have written it correctly and not made any glaring errrors.
 
Upvote 0

Forum statistics

Threads
1,221,614
Messages
6,160,839
Members
451,673
Latest member
wella86

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