我正在编写vba代码来更新SQLSERVER表。 我想在实际尝试更新之前检查新记录的所有值是否正常。 如何检查字段是否具有UNIQUE约束?
我已经尝试列出所有属性和属性,但是属性ISUNIQUE没有显示任何内容,虽然它是这样设置的,并且当在此字段中创建具有重复enty的新记录时会产生错误
属性
BASECATALOGNAME = TEBUS_Templates
BASECOLUMNNAME = Nombre del Armario
BASESCHEMANAME =
BASETABLENAME = TBL_FAKOM_ARMARIOS
CLSID =
COLLATINGSEQUENCE =
COMPUTEMODE =
DATETIMEPRECISION =
DEFAULTVALUE =
DOMAINCATALOG =
DOMAINSCHEMA =
DOMAINNAME =
HASDEFAULT =
ISAUTOINCREMENT = Falso
ISCASESENSITIVE = Falso
ISSEARCHABLE = 4
ISUNIQUE =
OCTETLENGTH = 200
KEYCOLUMN = Falso
OPTIMIZE = Falso
Attributes
adFldUnknownUpdatable
FWIW这是我整理上面列出的程序:
'2017-05-22 / B.Agullo /
Public Sub showFieldAtributesAndProperties(ByVal f As Field)
'description of sub
Dim p As Variant
Debug.Print f.Name
Debug.Print "Properties"
For Each p In f.Properties
Debug.Print p.Name & " = " & p.Value
Next
Debug.Print Chr(10) & "Attributes"
If ((adFldCacheDeferred And f.Attributes) = adFldCacheDeferred) Then Debug.Print "adFldCacheDeferred"
If ((adFldFixed And f.Attributes) = adFldFixed) Then Debug.Print "adFldFixed"
If ((adFldIsChapter And f.Attributes) = adFldIsChapter) Then Debug.Print "adFldIsChapter"
If ((adFldIsCollection And f.Attributes) = adFldIsCollection) Then Debug.Print "adFldIsCollection"
If ((adFldIsDefaultStream And f.Attributes) = adFldIsDefaultStream) Then Debug.Print "adFldIsDefaultStream"
If ((adFldIsNullable And f.Attributes) = adFldIsNullable) Then Debug.Print "adFldIsNullable"
If ((adFldIsRowURL And f.Attributes) = adFldIsRowURL) Then Debug.Print "adFldIsRowURL"
If ((adFldLong And f.Attributes) = adFldLong) Then Debug.Print "adFldLong"
If ((adFldMayBeNull And f.Attributes) = adFldMayBeNull) Then Debug.Print "adFldMayBeNull"
If ((adFldMayDefer And f.Attributes) = adFldMayDefer) Then Debug.Print "adFldMayDefer"
If ((adFldNegativeScale And f.Attributes) = adFldNegativeScale) Then Debug.Print "adFldNegativeScale"
If ((adFldRowID And f.Attributes) = adFldRowID) Then Debug.Print "adFldRowID"
If ((adFldRowVersion And f.Attributes) = adFldRowVersion) Then Debug.Print "adFldRowVersion"
If ((adFldUnknownUpdatable And f.Attributes) = adFldUnknownUpdatable) Then Debug.Print "adFldUnknownUpdatable"
If ((adFldUnspecified And f.Attributes) = adFldUnspecified) Then Debug.Print "adFldUnspecified"
If ((adFldUpdatable And f.Attributes) = adFldUpdatable) Then Debug.Print "adFldUpdatable"
release:
End Sub
另外,作为参考,这是用于创建表的SQL命令
CREATE TABLE TBL_FAKOM_ARMARIOS(
"ArmarioID" int IDENTITY(1,1) PRIMARY KEY NOT NULL
, "Nombre del Armario" nvarchar(100) UNIQUE NOT NULL
, "Fecha de Alta" dateTime NOT NULL
, "Fecha de Baja" dateTime
, "Usuario de Alta" nvarchar(50) NOT NULL
, "Usuario de Baja" nvarchar(50)
)
答案 0 :(得分:0)
您可以使用数据库架构,如此
Function IX_UNIQUE(strTableName As String, strFieldName As String) as Boolean
Dim c As ADODB.Connection
Dim r As ADODB.Recordset
Set c = New ADODB.Connection
c.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "C:\TESTsb.accdb" & ";" & _
"Persist Security Info=False;"
c.Open
Set r = New ADODB.Recordset
Set r = c.OpenSchema(adSchemaIndexes, Array(Empty, Empty, strFieldName , Empty, strTableName))
If Not r.EOF Then
IX_UNIQUE = r.Fields("UNIQUE").value
Else
IX_UNIQUE = False
End If
End Function