我用的datagrid控件。想实现是,我在一个文本框输入编号后,单击确定按钮。其它文本框就会显示对应的数据库中的数据计算后的结果。如,我想显示在其中一个文本框显示客户年龄,用现在的日期减去字段【出生日期】。想要全部的过程。嘿嘿……我说的有点乱,大致就是这个意思。求帮忙啊……
如图所示:
程序中主要涉及到一下几点:
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