2017-05-22 36 views
0

我正在编写更新SQLSERVER表的vba代码。 我想检查新记录的所有值是否正确,然后再尝试更新它们。 如何检查字段是否具有UNIQUE约束?通过ADODB VBA检查SQL字段是否具有UNIQUE约束

我已经尝试列出所有特性和属性,但propertie 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

我试着用Field.Type了类似的做法,但显然它并不像“属性工作“......我得到很随机的输出。 –

回答

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 
+0

谢谢!我暂时把这个错误信息记录下来并修改成向用户显示问题所在,但很高兴看到另一个场所进行调查。问候! –