vb连接access数据库后如何对其中的数据进行某种计算,然后把结果显示在文本框里?谢谢
如图所示:
程序中主要涉及到一下几点:
1、数据控件 aAdodc 连接数据库
在Form_Load 中使用如下的程序连接Access 数据库 ...\db7.mdb
Dim connStr As String
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db7.mdb;Persist Security Info=False"
With Adodc1
.ConnectionString = connStr
.CommandType = adCmdText
.CursorType = adOpenDynamic
.RecordSource = "SELECT * FROM T107"
.Refresh
End With
2、数据控件 DataGrid 的设置,要研究其属性设置
3、利用ADO 访问Access数据库
如按扭"计算实际年龄"的程序部分
Private Sub Command1_Click()
Dim connStr As String
Dim mySQL As String
Dim adoConn As New ADODB.Connection
Dim adoRs As New ADODB.Recordset
If Trim(Text1) = "" Then
MsgBox "请输入有效的编号!", 48, "重要提示"
Exit Sub
End If
mySQL = "SELECT DATEDIFF('yyyy',sBirth,Date()) As Years FROM T107 WHERE sCode='" + Trim(Text1) + "'"
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db7.mdb;Persist Security Info=False"
adoConn.Open connStr
adoRs.Open mySQL, adoConn, adOpenDynamic, adLockReadOnly
If adoRs.BOF And adoRs.EOF Then
MsgBox "输入的编号查无此人!", 48, "重要提示"
adoRs.Close
adoConn.Close
Exit Sub
Else
Text2 = "您已经:" + Str(adoRs(0)) + "岁"
End If
End Sub
更新详细的内容可以下载附件中的程序,其中包含了数据库db7.mdb 及Form7
2024-07-18 广告
2013-11-28
首先建立一个类模块,来建立全局的数据量连接
Public conn As New ADODB.Connection
'获取数据库连接 data.mdb是放在和程序相同目录下的一个access数据库文件
Public Function GetConnection()
Dim connectionstring As String
Dim flag As Boolean
On Error GoTo ErrLab
flag = True
connectionstring = "provider=Microsoft.Jet.oledb.4.0;data source=data.mdb"
conn.Open connectionstring
ErrHandle:
GetConnection = flag
Exit Function
ErrLab:
flag = False
Resume ErrHandle
End FunctionEnd Sub
--------------------示例:查询操作
Dim sql As String
Dim rsTmp As New ADODB.Recordset
call GetConnection
sql = "select * from MyTab "
rsTmp.Open sql, conn, adOpenForwardOnly, adLockOptimistic
While Not rsTmp.EOF
Print rsTmp!fname
rsTmp.MoveNext
Wend
rsTmp.Close