MS access 2010限制varchar

时间:2014-02-17 17:59:28

标签: sql ms-access-2010

我正在编写一个简单的数据库,我想将一个Varchar变量限制为几个值,我得到了这个:

PaymentMode VARCHAR (32),

CONSTRAINT chk_PaymentMode 
CHECK (PaymentMode IN 
  ('Daily', 'Weekly, 'Monthly', 'Every Six Months', 'Yearly'))

但它出现在"Syntax error in CONSTRAINT clause.",我不知道如何解决这个问题,请帮帮我们。

1 个答案:

答案 0 :(得分:0)

你错过了'Weekly'之后的结束单引号

由于您无法使其工作,我提供的代码将创建一个新表并在其上放置约束,在每一步显示结果。请:(1)将新模块添加到Access数据库,然后(2)将以下代码粘贴到该模块中,(3)将光标放在函数内的任何位置,然后(4)按F5。完成后,发布“立即”窗口中的所有文本。

Function Test_Constraint()
Dim strSQL      As String
Dim dbs As DAO.Database

Debug.Print "**** START TEST ****"
On Error Resume Next
Set dbs = CurrentDb
If Err.Number <> 0 Then Debug.Print "Error on Set dbs: " & Err.Number & vbTab & Err.Description
Err.Clear

strSQL = "CREATE TABLE Temp_Payment (StudentID INT, StudentName VARCHAR (32), PaymentID INT, PaymentMode VARCHAR (32), PRIMARY KEY (PaymentID))"
Debug.Print "Create Table SQL: " & strSQL
CurrentProject.Connection.Execute strSQL
If Err.Number <> 0 Then Debug.Print "Error on Create Table: " & Err.Number & vbTab & Err.Description
Err.Clear

strSQL = "ALTER TABLE Temp_Payment DROP CONSTRAINT chk_Temp_PaymentMode ;"
Debug.Print "DROP Constraint SQL: " & strSQL
CurrentProject.Connection.Execute strSQL
If Err.Number <> 0 Then Debug.Print "Error on DROP Constraint: " & Err.Number & vbTab & Err.Description & vbCrLf & strSQL
Err.Clear

strSQL = "ALTER TABLE Temp_Payment ADD CONSTRAINT chk_Temp_PaymentMode CHECK (PaymentMode IN ('Daily', 'Weekly', 'Monthly', 'Every Six Months', 'Yearly'))"
Debug.Print "ADD Constraint SQL: " & strSQL
CurrentProject.Connection.Execute strSQL
If Err.Number <> 0 Then Debug.Print "Error on ADD Constraint: " & Err.Number & vbTab & Err.Description & vbCrLf & strSQL
Err.Clear

Debug.Print "**** END TEST ****"
End Function