怎么用VB来对ACCESS数据库读写,我想把表直接显示出来.

如题所述

(注意要把你的数据库文件和你的工程窗体啥的放在同一个文件夹里)
先插入一个模块,在模块里写代码
Public cn As ADODB.Connection
Sub con()
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库名.扩展名;Persist Security Info=False"(在一行里)
End Sub
在窗体里写代码
call con
cn.open //打开数据库
Dim rs As ADODB.Recordset //rs是变量
Set rs = New ADODB.Recordset
查找
rs.Open "select * from 表名 where 字段名1=' " & Text1.Text & " ' and 字段名2= ' " & Text2.Text & " ' ", cn //text里的内容是符合你要查找的要求
rs.Open "select * from 表名" //查找整个表的内容
Set DataGrid1.DataSource = rs //显示在DataGrid里
往数据库里添加内容是(不过貌似只能一个一个的吧,要不就用循环)
cn.Execute "INSERT INTO 表明 (字段1,字段2,字段3) VALUES('" & Text1 & "','" & Text2 & "','" & Combo3.Text & "')"
删除 cn.Execute "DELETE FROM 表明 where 字段名='" & Text1 & "'"
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-15
AdodcName.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & _
App.Path & "\DataBase\DeviceManageSystem.mdb"
AdodcName.RecordSource = strSQL '"select * from DeviceSystem"
AdodcName.Refresh
Set DGName.DataSource = AdodcName
DGName.Refresh
'这是我原来一个软件的语句,希望对你有所启发
AdodcName:ADODC
DGName:DATAGRID