获取数据库字段的问题。谢谢了

游子 2008-03-31 09:00:56
我在窗体上有个按钮可以选择数据库文件,当我选择了一数据库文件后在LIST1里可以获取
相应数据库里的表名称,现在我要选择LIST1里表名称,怎么在LIST2里获取相应表的字段名称??
数据库名称为:AA.mdb
里面的表有:11和12
谢谢了。
...全文
34 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cbm6666 2008-03-31
在QQ上面知道你对ListBox的使用还不熟, 给你一个完整的吧

'引用 Microsoft ActiveX Data Objects 2.5 Library
'引用 Microsoft ADO Ext 2.5 for DDL and security

'添加 List1 List2

Option Explicit
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Dim sql$, Appdisk$, aa$, i&
Private Sub Form_Load()
Appdisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
'******************** Combo1 添加此库的所有表名
Set conn = New Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Appdisk & "bwscale.mdb;Persist Security Info=False"
Dim cat As New ADOX.Catalog
Set cat.ActiveConnection = conn
For i = 0 To cat.Tables.Count - 1
If Left(cat.Tables(i).Name, 4) <> "MSys" Then List1.AddItem cat.Tables(i).Name
Next i
Me.AutoRedraw = True
End Sub

Private Sub List1_Click()
If List1.ListCount = 0 Then Exit Sub
sql = "select * from " & List1.Text
rs.Open sql, conn, adOpenStatic, adLockOptimistic
List2.Clear
For i = 0 To rs.Fields.Count - 1
Select Case rs.Fields(i).Type
Case 202
aa = "文字"
Case 7
aa = "日期"
Case 6
aa = "货币"
Case 5
aa = "双精度"
Case 4
aa = "单精度"
Case 3
aa = "长整型"
Case 2
aa = "整型"
Case 205
aa = "二进制"
Case 211
aa = "布尔型"
Case Else
aa = "再对照"
End Select
List2.AddItem rs.Fields(i).Name & "---" & aa & "---" & rs.Fields(i).DefinedSize
Next i
rs.Close
End Sub


回复
cbm6666 2008-03-31
抱歉 忘了补充一行

'还要再引用 Microsoft ADO Ext 2.5 for DDL and security
回复
游子 2008-03-31
Dim cat As New ADOX.Catalog
一楼 不行啊!!
回复
cbm6666 2008-03-31
'引用 Microsoft ActiveX Data Objects 2.5 Library

'添加 Combo1

Option Explicit
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Dim sql$, Appdisk$, aa$, i&
Private Sub Form_Load()
Appdisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
'******************** Combo1 添加此库的所有表名
Set conn = New Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Appdisk & "bwscale.mdb;Persist Security Info=False"
Dim cat As New ADOX.Catalog
Set cat.ActiveConnection = conn
For i = 0 To cat.Tables.Count - 1
If Left(cat.Tables(i).Name, 4) <> "MSys" Then Combo1.AddItem cat.Tables(i).Name
Next i
Me.AutoRedraw = True
End Sub

Private Sub Combo1_Click()
If Combo1.ListCount = 0 Then Exit Sub
sql = "select * from " & Combo1.Text
rs.Open sql, conn, adOpenStatic, adLockOptimistic
Me.Cls
For i = 0 To rs.Fields.Count - 1
Select Case rs.Fields(i).Type
Case 202
aa = "文字"
Case 7
aa = "日期"
Case 6
aa = "货币"
Case 5
aa = "双精度"
Case 4
aa = "单精度"
Case 3
aa = "长整型"
Case 2
aa = "整型"
Case 205
aa = "二进制"
Case 211
aa = "布尔型"
Case Else
aa = "再对照"
End Select
Print rs.Fields(i).Name & "---" & aa & "---" & rs.Fields(i).DefinedSize
Next i
rs.Close
End Sub

回复
游子 2008-03-31
忘记说了 数据库是ACCESS数据库 谢谢各位
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7489

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-03-31 09:00
社区公告
暂无公告