'工程->引用Microsoft ADO Ext. 2.x for DDL and Security
'在窗体上有1个DataGrid、6个CommandButton、3个TextBox
Option Explicit
Public mCnnString As String
Dim mRst As New ADODB.Recordset
Private Sub Command1_Click() '1.在数据库mdb1上创建 "ID" "用户名" "密码" 字段的UserPassWord表
Dim mTbl As New Table
Dim mIdx As New ADOX.Index
Dim mCat As New ADOX.Catalog
' 打开目录。
mCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" & App.Path & "\db1.mdb" & "Persist Security Info=False"
' 定义表并将其追加到库中
mTbl.Name = "UserPassWord"
mTbl.ParentCatalog = mCat
mTbl.Columns.Append "ID", adVarWChar
mTbl.Columns.Append "用户名", adVarWChar
mTbl.Columns.Append "密码", adVarWChar
mCat.Tables.Append mTbl
' 定义多列索引
mIdx.Name = "MultiColIdx"
mIdx.Columns.Append "ID"
' 将索引追加到表上
mTbl.Indexes.Append mIdx
End Sub
Private Sub Command2_Click() '2.添加 "01" "小明" "999999" 到相应字段中的数据库文件中
Dim mCon As New ADODB.Connection
mCon.CursorLocation = adUseClient
mCon.Open mCnnString
mCon.Execute "Insert Into UserPassWord Values('" & "01'," & " '" & "小明" & "'," & " '999999" & "')"
mCon.Close
Set mCon = Nothing
End Sub
Private Sub Command3_Click() '3.删除指定ID(Text1.Text)整行的数据行
Dim mCon As New ADODB.Connection
mCon.CursorLocation = adUseClient
mCon.Open mCnnString
mCon.Execute "Delete From UserPassWord Where ID = '" & Trim(Text1.Text) & "'"
mCon.Close
Set mCon = Nothing
End Sub
Private Sub Command4_Click() '4.根据用ID(Text2.Text)查询数据库
If mRst.State = adStateOpen Then mRst.Close
mRst.CursorLocation = adUseClient
mRst.Open "Select * From UserPassWord Where ID = '" & Trim(Text2.Text) & "'", mCnnString, adOpenStatic, adLockOptimistic, adCmdText
Set DataGrid1.DataSource = mRst
End Sub
Private Sub Command5_Click() '4.根据用户名(Text3.Text)查询数据库
If mRst.State = adStateOpen Then mRst.Close
mRst.CursorLocation = adUseClient
mRst.Open "Select * From UserPassWord Where 用户名 = '" & Trim(Text3.Text) & "'", mCnnString, adOpenStatic, adLockOptimistic, adCmdText
Set DataGrid1.DataSource = mRst
End Sub
Private Sub Command6_Click() '5.清空数据表,保留字段
Dim mCon As New ADODB.Connection
mCon.CursorLocation = adUseClient
mCon.Open mCnnString
mCon.Execute "Delete From UserPassWord Where True"
mCon.Close
Set mCon = Nothing
End Sub
Private Sub Form_Load()
mCnnString = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" & App.Path & "\db1.mdb" & "Persist Security Info=False" '连接字符串
End Sub
Private Sub Form_Unload(Cancel As Integer) '退出时候释放mRst对象
If mRst.State = adStateOpen Then
mRst.Close
Set mRst = Nothing
End If
End Sub
内部要一个一个的查,你可以一起显示。比如分三次查处张三李四王五的地址,然后合并一起显示。
查一个的方法很简单
新建一个Data Project,加入你这个数据的人保存在表tblPeoples中人名保存的字段是name,地址字段是address
Dim mConn As ADODB.Connection
Dim mRst As ADODB.Recordset
Dim peoples() As String
Dim addresses As String
Private Sub Command1_Click()
Dim i As Integer
Dim sName As String
Dim sSQL As String
peoples = Split(Text1.Text, "") '假如人名之间用分号分隔
addresses = ""
For i = 0 To UBound(peoples)
sName = peolples(i)
sSQL = "select address from tblPeoples where [name]='" &sName &"'"
Set mRst = mConn.Execute(sSQL)
If Not (mRst.BOF Or mRst.EOF) Then
addresses = addresses &mRst.Fields("address") &""
mRst.Close
End If
Next
Text2.Text = addresses
End Sub
Private Sub Form_Load()
Set mConn = New ADODB.Connection
Set mRst = New ADODB.Recordset
mConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51Persist Security Info=FalseData Source=xxxx.mdb"""
mConn.Open
End Sub
Private Sub Form_Unload(Cancel As Integer)
If mConn.State = adStateOpen Then
mConn.Close
Set mConn = Nothing
End If
End Sub
大致的过程可以这样
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)