如题 多谢了
解决方案 »
- vb6.0 如何在运行的时候在跳出的form自动加载某个ICON图标
- 奇怪的问题 加msgbox 后才可以执行
- 求教: 有关VB创建的可以输出函数的DLL在VC中调用的问题
- vb中登陆对话框和SQL server数据库USER表里的密码比较后,然后登陆的问题,,
- 打印报表为什么会少一条记录?
- evb是什么?以前没听到过,请告知,谢
- 关于Data Environment
- 怎样在程序运行时,删除 Combobox 中的某个项,谢谢~~~
- 很简单的一个关于Combobox控件的问题
- 我快疯了,我想读ini中的一个段下的所有值,但是keyName不知道,该如何是好?
- 在一个窗体没关闭且有一条记录的情况下,我在另一个窗体把这条记录删除了,会出现错误。
- 请文在VB程序远行时怎么控制才能使它暂停几秒钟?
新建数据库
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
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
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\new.mdb"End Sub
敢问各位大哥 上面的 Data Source = c:\new.mdb 这里 我想让这个数据库的路径和名称到时候根据客户的选择动态确定,如何做到呢 我用了 app.path 好像不行啊
Sub CreateDatabase() Dim cat As New ADOX.Catalog
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & app.path & "\new.mdb"End Sub
应该:
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