如题 多谢了

解决方案 »

  1.   

    引用    Microsoft ADO Ext. 2.X for DDL and Security
    新建数据库
    Sub CreateDatabase()   Dim cat As New ADOX.Catalog
       cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\new.mdb"End Sub如下代码演示如何创建新表。Sub CreateTable()   Dim tbl As New Table
       Dim cat As New ADOX.Catalog'打开目录。
       ' 打开目录。
       cat.ActiveConnection = _ 
          "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=c:\Program Files\Microsoft Office\" & _
          "Office\Samples\Northwind.mdb;"   tbl.Name = "MyTable"
       tbl.Columns.Append "Column1", adInteger
       tbl.Columns.Append "Column2", adInteger
       tbl.Columns.Append "Column3", adVarWChar, 50
       cat.Tables.Append tblEnd Sub
      

  2.   

    是的,需要引用adox,但这样创建数据库会出现一个问题,就是不能按汉字排序。
      

  3.   

    引用 Microsoft DAO 3.51 object library、Microsoft Remote Data Object 2.0Private Sub 建立一个数据库_Click()
        SaveToTable
    End Sub
    创一个函数:
    Function CreateTable(sTableName As String)
    ' Create a new table to store the Node information in
        Dim td As TableDef
        Dim fd As Field    
        Set td = mDB.CreateTableDef(sTableName)
        Set fd = td.CreateField("key", dbText, 4)
        td.Fields.Append fd
        Set fd = td.CreateField("parent", dbText, 4)
        td.Fields.Append fd
        Set fd = td.CreateField("text", dbText, 20)
        td.Fields.Append fd
        Set fd = td.CreateField("image", dbInteger)
        td.Fields.Append fd
        Set fd = td.CreateField("selectedimage", dbInteger)
        td.Fields.Append fd
        mDB.TableDefs.Append td
               
    End Function
    一个过程:
    Sub SaveToTable()
    'Ask the user for the name of a mdb and table if it does not exist create it.
    'Then store all of the nodes from the TreeView into the table.
        Dim sResponse As String
        Dim sMDBName As String
        Dim sTableName As String
        Dim i As Integer
               
        sResponse = MsgBox("Click YES to Create a new MDB", vbYesNo)
        If sResponse = vbYes Then
            CommonDialog1.Filter = "Access Database(*.MDB) |*.mdb"
            CommonDialog1.ShowSave
            If Len(CommonDialog1.FileName) < 1 Then
                MsgBox ("You did not enter a file name")
                Exit Sub
            Else
                sTableName = InputBox("Enter a Table name", _
                 "Table name to save Nodes to")           If Len(sTableName) < 1 Then
                    MsgBox ("You did not supply a table name")
                    Exit Sub
               End If
                Set mDB = Workspaces(1).CreateDatabase(CommonDialog1.FileName, _
                  dbLangGeneral)
               CreateTable (sTableName) ' call the sub that creates a new table
               Set mRS = mDB.OpenRecordset(sTableName)
               WriteToTable  'Go to the sub that writes the nodes into the table
            End If
        Else
            CommonDialog1.Filter = "Access Database(*.MDB) |*.mdb"
            CommonDialog1.ShowOpen
            If Len(CommonDialog1.FileName) < 1 Then
                MsgBox ("No File was Choosen")
                Exit Sub
            End If
        
            sTableName = InputBox("Enter a Table name", _
            "Table name to save Nodes to")
            
            If Len(sTableName) < 1 Then
                MsgBox ("You did not supply a table name")
                Exit Sub
            End If
            Set mDB = DBEngine.Workspaces(1).OpenDatabase(CommonDialog1.FileName)
            For i = 0 To mDB.TableDefs.Count - 1 'TableDefs is a 0 based collection
                If mDB.TableDefs(i).Name = sTableName Then
                ' check to see if the table exist
                    sResponse = MsgBox("All records in your table will be destroyed", vbOKCancel)
                    If sResponse = vbOK Then
                        Set mRS = mDB.OpenRecordset(sTableName)
                        WriteToTable  'Go to the sub that writes the nodes into the table
                        mRS.Close 'close the recordset
                        mDB.Close 'close the database
                        Exit Sub
                    Else
                        mDB.Close 'close the database
                        Exit Sub
                    End If
                End If
            Next
            CreateTable (sTableName) ' call the sub that creates a new table
            Set mRS = mDB.OpenRecordset(sTableName)
            WriteToTable  'Go to the sub that writes the nodes into the table
        End If
    mRS.Close 'close the recordset
    mDB.Close 'close the database
    End Sub
    Sub WriteToTable()
    'Writes the Node information from the TreeView into a table
        Dim i As Integer
        Dim iTmp As Integer
        Dim iIndex As Integer
        
        If mRS.RecordCount > 0 Then
        ' Delete any records that may be in the table
            mRS.MoveFirst
            Do While mRS.EOF = False
                mRS.Delete
                mRS.MoveNext
            Loop
        End If
        
        GetFirstParent 'Find a root node in the treeview
        'get the index of the root node that is at the top of the treeview
        iIndex = TreeView1.Nodes(mnIndex).FirstSibling.Index
        iTmp = iIndex
        mRS.AddNew
        mRS("parent") = "0_" 'this is a root node
        mRS("key") = "sdf"
        mRS("text") = "sdf"
        mRS("image") = "fg"
        mRS("selectedimage") ="sdf"
        mRS.Update
        End Sub
      

  4.   

    看看FAQhttp://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=179417
      

  5.   

    Sub CreateDatabase()   Dim cat As New ADOX.Catalog
       cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\new.mdb"End Sub
    敢问各位大哥 上面的 Data Source = c:\new.mdb 这里 我想让这个数据库的路径和名称到时候根据客户的选择动态确定,如何做到呢 我用了 app.path 好像不行啊
      

  6.   

    这样
    Sub CreateDatabase()   Dim cat As New ADOX.Catalog
       cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & app.path & "\new.mdb"End Sub
      

  7.   

    用APP.path是可以的,不过上面这位兄弟写的有时还会出错。
    应该:
    dim strSoftPath as string
    strSoftPath = App.Path
    If Mid$(strSoftPath, Len(strSoftPath), 1) <> "\" Then strSoftPath = strSoftPath & "\"
    '生成strSoftPath形如c:\或c:\abc\,后面一定带有\,如你不想带,可修改上面及下面语句'引用微软 ADO Ext.2.7 for dll and Security
    '创建数据库和数据表及字段
    Private Sub Command1_Click()
        CreateDatabase
        CreateTable
        MsgBox "成功"
    End Sub
    Sub CreateDatabase()
       Dim cat As New ADOX.Catalog
       cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\new.mdb"
    End SubSub CreateTable()
       Dim tbl As New Table
       Dim cat As New ADOX.Catalog
    'Open the catalog.
       ' Open the Catalog.
       cat.ActiveConnection = _
          "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & strSoftPath & "new.mdb;"  '此处用strSoftPath换了C:\   tbl.name = "MyTable"
       tbl.Columns.Append "Column1", adInteger
       tbl.Columns.Append "Column2", adInteger
       tbl.Columns.Append "Column3", adVarWChar, 50
       cat.Tables.Append tblEnd Sub
    '检查表是否存在
    Dim cnn1 As ADODB.Connection
    Dim rstSchema As ADODB.Recordset
    Dim strCnn As StringSet cnn1 = New ADODB.Connection
    strCnn = "Provider=sqloledb;" & _
    "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
    cnn1.Open strCnnSet rstSchema = cnn1.OpenSchema(adSchemaTables)Do Until rstSchema.EOF
    Debug.Print "Table name: " & _
    rstSchema!TABLE_NAME & vbCr & _
    "Table type: " & rstSchema!TABLE_TYPE & vbCr
    rstSchema.MoveNext
    Loop
    rstSchema.Closecnn1.Close