vb 检测数据库中的表名和字段名
1.表名:表1 表类型:XXX
A1,A2,A3
2.表名:表2 表类型:XXX
B1,B2,B3
当然如果有表3,表4等都能依次显示下去。 展开
'要在"工程"菜单中引用对象库"Microsoft ActiveX Data Objects 2.5 Library"和"Microsoft ADO Ext 2.1. For DDL Security"
Dim DBmode As New ADOX.Catalog '不用DBmode用另外一个名字也可以
Dim DEXdatabase As String '连接数据库用的字符
Dim BaseName As String '数据库名
Dim PathName As String '数据库路径
Dim Conn As Connection
Dim RSdb As Recordset
Dim MyTable As Table
Private Sub Combo1_Click()
Dim Table As String
On Error GoTo EditErr '当读系统表时会出错,因为不允许
Table = Combo1.Text
Me.Cls
Set RSdb = New Recordset
RSdb.Open "select * from " & Table, Conn, adOpenStatic, adLockReadOnly
print "表名",DBmode.Tables(I).Name
print "类型",DBmode.Tables(I).Type
Print "共有" & RSdb.Fields.Count & "个字段"
For I = 0 To RSdb.Fields.Count - 1
Print RSdb.Fields(I).Name
Next
RSdb.Close
Set RSdb = Nothing
Exit Sub
EditErr:
MsgBox (Err.Description)
End Sub
Private Sub Command1_Click()
Dim A As Long
Dim B As Long
On Error GoTo EditErr '
Combo1.Clear
DBmode.ActiveConnection = DEXdatabase
A = DBmode.Tables.Count
B = 0
For I = 0 To A - 1
If DBmode.Tables(I).Type = "SYSTEM TABLE" Then '系统表不显示,一定要大写,否则会出错
Else
Combo1.AddItem (DBmode.Tables(I).Name)
B = B + 1
End If
Next
Combo1.Text = Combo1.List(0)
Combo1_Click
Exit Sub
EditErr:
MsgBox (Err.Description)
End Sub
Private Sub Form_Load()
BaseName = "my_database.mdb" '你的数据库名,你可以改动它
PathName = App.Path & "\" & BaseName '你的数据库位置,你可以改动它
DEXdatabase = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & PathName
Set Conn = New Connection
Conn.CursorLocation = adUseClient
Conn.Open DEXdatabase
End Sub
显示所有记录:
adodc1.recordsource = "select * from producetable1"
adodc1.refresh
显示某字段
adodc1.recordsource = "select worknamber from producetable1"
adodc1.refresh
datagrid就跟着刷心了。