字段允许为空

解决方案 »

  1.   

    ALTER TABLE 语句示例
    此示例在员工表中增加 Money 数据类型的一个工资字段。Sub AlterTableX1()
        Dim dbs As Database
        ' 在您的计算机中修改此行使其正确指到 Northwind 的路径。    Set dbs = OpenDatabase("Northwind.mdb")    ' 对运费超过 $100 的订单,
        '添加工资字段至员工表中,    '并且设置为 Currency 数据类型。    dbs.Execute "ALTER TABLE Employees " _        & "ADD COLUMN Salary CURRENCY;"
        dbs.Close
    End Sub
    此示例把工资字段的数据类型从 Money 改为 Char。Sub AlterTableX2()
        Dim dbs As Database
        ' 在您的计算机中修改此行使其正确指到 Northwind 的路径。    Set dbs = OpenDatabase("Northwind.mdb")    ' 对运费超过 $100 的订单,
        '添加工资字段至员工表中,    '并且设置为 Currency 数据类型。    dbs.Execute "ALTER TABLE Employees " _        & "ALTER COLUMN Salary CHAR(20);"
        dbs.Close
    End Sub
    此示例从员工表中删除工资字段。Sub AlterTableX3()
        Dim dbs As Database
        ' 在您的计算机中修改此行使其正确指到 Northwind 的路径。    Set dbs = OpenDatabase("Northwind.mdb")    ' 对运费超过 $100 的订单,
        '从员工表中删除工资字段。    dbs.Execute "ALTER TABLE Employees " _        & "DROP COLUMN Salary;"
        dbs.Close
    End Sub
    此示例在订单表中增加一个外部键。这个外部键是基于员工编号字段和引用至员工的表中的员工编号字段。在此示例中,不必把 EmployeeID 字段列在 REFERENCES 子句中的 Employees table 后面,因为 EmployeeID 是 Employees table 的主键。SubSub()
        Dim dbs As Database
        ' 在您的计算机中修改此行使其正确指到 Northwind 的路径。    Set dbs = OpenDatabase("Northwind.mdb")    ' 对运费超过 $100 的订单,
        '在订单表中添加外部键。    dbs.Execute "ALTER TABLE Orders " _        & "ADD CONSTRAINT OrdersRelationship " _        & "FOREIGN KEY (EmployeeID) " _        & "REFERENCES Employees (EmployeeID);"
        dbs.Close
    End Sub
    此示例从订单表中删除外部键。Sub AlterTableX3()
        Dim dbs As Database
        ' 在您的计算机中修改此行使其正确指到 Northwind 的路径。    Set dbs = OpenDatabase("Northwind.mdb")    ' 对运费超过 $100 的订单,
        ' 从订单表中清除OrdersRelationship外部键    '     dbs.Execute "ALTER TABLE Orders " _        & "DROP CONSTRAINT OrdersRelationship;"
        dbs.Close
    End Sub
      

  2.   

    如果是用ADO该怎样做呢?
      

  3.   

    Private Sub Command3_Click()
    Dim txtSQL As StringDim mrc As ADODB.RecordsettxtSQL = "alter table 表名 add 字段名 字段类型  长度"
    Set mrc = sql(txtSQL)
    Command3.Enabled = False
    End Sub
    Public Function sql(ByVal txtSQL As String) As ADODB.Recordset
    Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim provider As String
    Dim datasource As String
    Dim stoken() As String
    On Error GoTo errmsg
    provider = "Provider=Microsoft.Jet.OLEDB.4.0;"
    datasource = "Data Source=" & App.Path & "\gz.mdb"
    stoken = Split(txtSQL)
    Set conn = New ADODB.Connection
    conn.Open provider & datasource
    If InStr("INSERT,DELETE,UPDATE", UCase$(stoken(0))) Then
           conn.Execute txtSQL
    Else
        Set rst = New ADODB.Recordset
          rst.Open Trim$(txtSQL), conn
          Set sql = rst
    End If
    exitmsg:
       Set rst = Nothing
       Set conn = Nothing
       Exit Function
    errmsg:
     MsgBox "查询错误",vbOKOnly, "错误提示"
    Resume exitmsg
    End Function