你缺少後關閉單引號「每週」
既然你是無法得到這個工作,我提供的代碼,將創建一個新表,並將約束它,在顯示結果每一步。請:(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
爲什麼不使用具有外鍵約束的查找表? –
什麼是最簡單的方法?對不起,我在SQL中很新鮮,應該是我第一個簡單的項目,所以我不知道如何按照你提到的方式來完成。感謝您的答覆。 – user3320248