Murray Brown
asked on
VB.net Access SQL Month to date - problem with reserved words
Hi
I am using the following function build a query that gets month to date numbers
from an Access database table
There is, however a problem with Reserved words (Year etc)
How do I change the code so that I don't have this proble?
Public Function MonthToDateAccess(ByVal oFromTables As String, ByVal oAmountField As String, ByVal oDateField As String, ByVal oOtherFields As String) As String
Try
Dim S As String = ""
S = S & "SELECT Year(" & oDateField & ") As Year, Month(" & oDateField & ") As Month, SUM(" & oAmountField & ") As TotalAmount" & oOtherFields
S = S & " " & oFromTables
S = S & " WHERE Year(" & oDateField & ") = Year(Date()) AND Month(" & oDateField & ") = Month(Date()) "
S = S & " GROUP BY Year(" & oDateField & ") As Year, Month(" & oDateField & ") As Month " & oOtherFields
S = S & " ORDER BY Year(" & oDateField & ") As Year, Month(" & oDateField & ") As Month"
MonthToDateAccess = S
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
I am using the following function build a query that gets month to date numbers
from an Access database table
There is, however a problem with Reserved words (Year etc)
How do I change the code so that I don't have this proble?
Public Function MonthToDateAccess(ByVal oFromTables As String, ByVal oAmountField As String, ByVal oDateField As String, ByVal oOtherFields As String) As String
Try
Dim S As String = ""
S = S & "SELECT Year(" & oDateField & ") As Year, Month(" & oDateField & ") As Month, SUM(" & oAmountField & ") As TotalAmount" & oOtherFields
S = S & " " & oFromTables
S = S & " WHERE Year(" & oDateField & ") = Year(Date()) AND Month(" & oDateField & ") = Month(Date()) "
S = S & " GROUP BY Year(" & oDateField & ") As Year, Month(" & oDateField & ") As Month " & oOtherFields
S = S & " ORDER BY Year(" & oDateField & ") As Year, Month(" & oDateField & ") As Month"
MonthToDateAccess = S
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Use square brackets around reserved words that are used as field names:
Or better yet rename those fields to NOT use reserved words:
MonthOfPayment
YearOfPayment
S = S & "SELECT Year(" & oDateField & ") As [Year], Month(" & oDateField & ") As [Month], SUM(" & oAmountField & ") As TotalAmount" & oOtherFields
'etcetera
Or better yet rename those fields to NOT use reserved words:
MonthOfPayment
YearOfPayment
ASKER
I tested with just two columns and got the same error with the resultant SQL expression
SELECT Year([Transactions].[Trans action Date]) As [Year], Month([Transactions].[Tran saction Date]) As [Month], SUM([Transactions].[Amount ]) As TotalAmount FROM [Transactions] WHERE Year([Transactions].[Trans action Date]) = Year(Date()) AND Month([Transactions].[Tran saction Date]) = Month(Date()) GROUP BY Year([Transactions].[Trans action Date]) As [Year], Month([Transactions].[Tran saction Date]) As [Month] ORDER BY Year([Transactions].[Trans action Date]) As [Year], Month([Transactions].[Tran saction Date]) As [Month]
SELECT Year([Transactions].[Trans
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Great! That worked. Thanks very much
Open in new window