Answered by:
problem with function - error with sql statement

Question
-
User-1892634376 posted
So I have a function that is looking for the last value in the primary key column (1001,1002, etc). The function has the normal OleDbConnection and OleDbAdapter variables but the sql line that I'm trying to set as a string to be passed to Access keeps coming back incorrect.
Public Function FindMax(ByVal tableName As String, ByVal columnName As String, ByVal Path As String) As TPsStaffSet
' Sets up the containers amd the data base connection
Dim DS As TPsStaffSet
Dim sqlConn As OleDbConnection
Dim sqlDA As OleDbDataAdapter
Dim sql As String
' Sets up the database connection and looks for the address
sqlConn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Path & "")
sql = "SELECT MAX(" & columnName & ") FROM " & tableName --the error starts here
' Fills the container with the found address
sqlDA = New OleDbDataAdapter(sql, sqlConn)
' sets up the new object for the found address
DS = New TPsStaffSet
' fills up the new object with the address
sqlDA.Fill(DS.CONTRACT)
Return DS
End Function
Any suggestion??
Saturday, October 16, 2010 11:53 PM
Answers
-
User-577741185 posted
You need to make sure the DataTable schema is the the same as the query schema. Most likely, it will be the MaxLength property that doesn't match, or unique/null values.
Here are plenty more blogs to read.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, October 17, 2010 12:28 PM
All replies
-
User-577741185 posted
but the sql line that I'm trying to set as a string to be passed to Access keeps coming back incorrectmeaning?
sql = "SELECT MAX(" & columnName & ") FROM " & tableName --the error starts here
What's the error?
Sunday, October 17, 2010 12:21 AM -
User-1892634376 posted
ok...after a closer look, its not the sql statement that has a problem. This is the error I get: "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints" on this line: sqlDA.Fill(DS.CONTRACT).
Sunday, October 17, 2010 9:35 AM -
User-577741185 posted
You need to make sure the DataTable schema is the the same as the query schema. Most likely, it will be the MaxLength property that doesn't match, or unique/null values.
Here are plenty more blogs to read.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, October 17, 2010 12:28 PM