2010-08-04 208 views
3

如何在除PersonalID列之外的表中更改所有列值(Nullable = True和Allow Zero Length = True)?當腳本執行時,我得到錯誤 「-2147217887 - 多步OLE DB操作產生錯誤。檢查每個OLE DB狀態值(如果可用),沒有工作完成。」 這是隻有一個表的代碼。我需要創建更多的表,所以設置每列的Nullable屬性是不實際的。請幫助!更改列的屬性值

Public Sub DBcreation() 
Dim tbl As New Table 
Dim cat As New ADOX.Catalog 
Dim col As ADOX.Column 
Dim prp As ADOX.Property 
Dim oCn As ADODB.Connection 
Dim sConStr As String 


'Set Connection string 
sConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
         "Data Source=" & App.Path & "\mydbase.mdb" & ";" & _ 
         "Jet OLEDB:Engine Type=4;" 
' 1) Create the DataBase 
cat.Create sConStr 

' 2) Create Table name "PDetails" 
tbl.Name = "PDetails" 
' 3) Add Fields 
With tbl.Columns 
.Append "PersonalID", adInteger 'Number 
.Append "GHName", adVarWChar, 50 'Text 
.Append "FirstName", adVarWChar, 50 'Text 
.Append "FHName", adVarWChar, 50 'Text 
.Append "Surname", adVarWChar, 50 'Text 
.Append "BirthDate", adDate 
.Append "Gender", adVarWChar, 10 'Text 
.Append "Address", adLongVarWChar 'Memo 
.Append "Pincode", adInteger 'Number 
.Append "MobileNo", adInteger 'Number 
.Append "HomeNo", adInteger 'Number 
.Append "MaritalStatus", adVarWChar, 10 'Text 
.Append "Profession", adVarWChar, 50 'Text 
.Append "BloodGroup", adVarWChar, 10 'Text 
.Append "Photo", adVarWChar, 50 'Text 

' 4) 'Set the field properties. 


With !PersonaltID 'AutoNumber. 
.ParentCatalog = cat 
.Properties("Autoincrement") = True 
.Properties("Description") = "Automatically " & _ 
"generated unique identifier for this record." 
End With 

With !BirthDate 
Set .ParentCatalog = cat 
.Properties("Jet OLEDB:Column Validation Rule") = _ 
    "Is Null Or <=Date()" 
.Properties("Jet OLEDB:Column Validation Text") = _ 
    "Birth date cannot be future." 
End With 

End With 
' 5) Save the Table to the DataBase 
    cat.Tables.Append tbl 
' 6) Set Column Properties  
    For Each col In tbl.Columns 
    For Each prp In col.Properties 
    If col.Name <> "PersonalID" Then 
    If prp.Name = "Nullable" Then 
    prp.Value = True 'error generated 
    '-2147217887 - Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done. 
    End If 
    End If 
    Next 
    Next 

'Clean up 

    Set tbl = Nothing 
    Set cat = Nothing 
    set prp = Nothing 
    End Sub 
+0

只是一個小小的錯字「With!PersonaltID'自動編號。'應該是'With!PersonalID'AutoNumber。' – jakdep 2011-01-13 14:38:41

回答

2

我假設你希望Allow Zero Length和Nullable屬性都設置爲true。看起來MS Access不允許你通過ADOX設置Nullable屬性,就像我所知,用ADO命令設置Allow Zero Length屬性是不可能的,所以你需要使用ADOX和ADO命令。這是我怎麼會去這樣做:

6) Set Column Properties 
    For Each col In tbl.Columns 
     For Each prp In col.Properties 
      If col.Name <> "PersonalID" Then 
       col.Properties("Jet OLEDB:Allow Zero Length").Value = True 
      End If 
     Next 
    Next 

    Set oCn = cat.ActiveConnection 

    SetColumnToNullable oCn, "PDetails", "GHName", "VARCHAR(50)" 

    oCn.Close 

    ''' 
    End Sub 


Private Sub SetColumnToNullable(ByRef poCn As ADODB.Connection, _ 
           ByVal pstrTableName As String, _ 
           ByVal pstrColumnName As String, _ 
           ByVal pstrDataType As String) 

    poCn.Execute "ALTER TABLE " & pstrTableName & " ALTER COLUMN " & pstrColumnName & " " & pstrDataType & " NULL" 
End Sub 

這種方法的另一個好處是,以前的目錄開到處於打開的執行完成後,與數據庫的連接。

隨着

oCn.Close 

這不再是一個問題。