理解自学者的困难,我也是这么走过来的,我知道你的想法,我给一段代码,我也是慢慢的摸索出来的,你自己研究一下吧!
窗体添加控件,text1,text2,command6个分别是(查询,刷新,添加,修改,删除,退出)一个MSHFlexGrid1
我用的是ACCESS2007版的,所以如果你用2003版的就要修改provider=microsoft.ace.oledb.12.0这一句,自己查一下资料
希望我自己用来研究用的代码对你的学习有所帮助!
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Private Sub Command1_Click()'查询
If Text1.Text = "" Then
MsgBox "请您输入查询条件", 32, "系统"
Text1.SetFocus
Exit Sub
Else
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select * from 表名 where
表字段 = '" & Trim(Text1.Text) & "'"
rs.Open sql, cn, 3, 3
If rs.RecordCount = 0 Then
MsgBox "无此记录,请重新输入!", 32, "系统"
Else
Set MSHFlexGrid1.DataSource = rs
End If
End If
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
End Sub
Private Sub Command2_Click()'刷新
Call Form_Load
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select * from 表名 "
rs.Open sql, cn, 3, 3
If rs.RecordCount > 0 Then
Set MSHFlexGrid1.DataSource = rs
End If
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click()'添加
If Text1.Text = "" Then
MsgBox "请输入名称!", 32, "系统"
Text1.SetFocus
Exit Sub
End If
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select * from 表名 where
字段名 = '" & Trim(Text1.Text) & "'"
rs.Open sql, cn, 3, 3
If rs.RecordCount > 0 Then
MsgBox "已有此记录,请重新输入!", 32, "系统"
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
Exit Sub
Else
rs.AddNew
rs.Fields("字段名") = Trim(Text1.Text)
rs.Update
MsgBox "数据保存成功!", , "系统提示"
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select * from 表名"
rs.Open sql, cn, 3, 3
If rs.RecordCount > 0 Then
Set MSHFlexGrid1.DataSource = rs
End If
End If
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
End Sub
Private Sub Command4_Click()'删除
If Text1.Text = "" Then
MsgBox "无此记录!", , "系统提示"
Text1.SetFocus
Exit Sub
End If
If Text1.Text <> "" Then
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select * from 表名 where
字段名 = '" & Trim(Text1.Text) & "'"
rs.Open sql, cn, 3, 3
If rs.RecordCount > 0 Then
ml = MsgBox("确定删除此记录吗?", vbYesNo, "系统")
If ml = vbYes Then
rs.Delete
End If
MsgBox "删除成功!", , "系统提示"
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select * from 表名"
rs.Open sql, cn, 3, 3
If rs.RecordCount > 0 Then
Set MSHFlexGrid1.DataSource = rs
End If
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
Else
MsgBox "无此记录!", , "系统提示"
End If
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
End Sub
Private Sub Command5_Click()'退出
Unload Me
End Sub
Private Sub Command6_Click()'修改
Dim a
Dim b
If Text1.Text = "" Then
MsgBox "请输入原类型名称!", 32, "系统"
Text1.SetFocus
Exit Sub
End If
If Text2.Text = "" Then
MsgBox "请输入新类型名称!", 32, "系统"
Text2.SetFocus
Exit Sub
End If
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select " & Text1.Text & " from 表名 "
rs.Open sql, cn, 3, 3
'==========================
'循环对比是否已有记录
a = Trim(Text1.Text)
b = Trim(Text2.Text)
If rs.EOF And rs.BOF Then'如果没有任何记录直接新建添加一个记录
rs.AddNew
rs.Fields(a) = b
rs.Update
MsgBox "添加成功!", , "系统提示"
GoTo X
Else
Do While Not rs.EOF
If Text1.Text = rs.Fields(a) Then
rs.Fields(a) = b
rs.Update
a = 1
'设置一个变量标记是否从循环退出
MsgBox "修改成功!", , "系统提示"
Exit Do
Else
If Len(rs.Fields(a) & "") = 0 Then
rs.Fields(a) = b
rs.Update
a = 1
Exit Do
End If
End If
rs.MoveNext
Loop
If a = 1 Then
'如果存循环退出的
GoTo X
'结束
End If
End If
'==================================================
X:
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select " & Text1.Text & " from 表名 "
rs.Open sql, cn, 3, 3
If rs.RecordCount > 0 Then
Set MSHFlexGrid1.DataSource = rs
End If
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End Sub
Private Sub Form_Load()
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
cn.Open "provider=microsoft.ace.oledb.12.0;Data Source=" & App.Path & "\数据库名.accdb;Jet OLEDB:Database Password=1;"
sql = "select * from 表名 "
rs.Open sql, cn, 3, 3
If rs.RecordCount > 0 Then
Set MSHFlexGrid1.DataSource = rs
End If
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload Me
End Sub
Private Sub MSHFlexGrid1_Click()'点击表格,将单元格内容添加到text1中
Dim J As Integer
J = MSHFlexGrid1.MouseRow
If MSHFlexGrid1.Text = "" Then Exit Sub
'==================================
Text1.Text = MSHFlexGrid1.TextMatrix(J, 0)
End Sub
[
本帖最后由 wxflw 于 2012-12-9 10:00 编辑 ]