第一種方法:設置adodc1的屬以連接數據庫.在adodc1控件上右鍵--Adodc屬性--使用連接字符串--生成--Microsoft Jet 4.0 OLE DB Provider--下一步--選擇或輸入數據庫名稱---找到要連接的數據庫后,確定.然后記錄源設置屬性. 如果要把內容提交到數據庫一般使用adCmdTable. 表選擇要連接的表。
設置完畢后就可以了.
如果我們想把內容提交到數據庫.舉個例子..
在窗體建立一個文本框,設置屬性中的DataSource為adodc1
設置好后在窗體加一個添加記錄和一個提交的按鈕,設置代碼:
Private Sub Command1_Click()
Adodc1.Recordset.Update
Adodc1.Refresh '刷新
End Sub
添加按鈕代碼:
Private Sub Command2_Click()
Adodc1.Recordset.AddNew '添加新紀錄
Adodc1.Recordset("姓名").Value = Text1.Text
End Sub
第二種方法:
在窗體添加Adodc控件一個text控件 一個添加記錄按鈕一個提交按鈕
在窗體設置代碼:
Private Sub Form_Load()
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db1.mdb;Persist Security Info=False" '設置數據庫路徑
Adodc1.CommandType = adCmdText '設置記錄源
Adodc1.RecordSource = "select * from message" '連接數據庫的message表文件
Set Text1.DataSource = Adodc1
text1.DataField = "姓名"
End Sub
添加記錄按鈕代碼:
Private Sub Command1_Click()
Adodc1.Recordset.AddNew '添加新紀錄
End Sub
提交代碼:
Private Sub Command2_Click()
Adodc1.Recordset.Update
Adodc1.Refresh '刷新
End Sub
首記錄按鈕的代碼:
Private Sub sjl_Click()
Adodc1.Recordset.MoveFirst
End Sub
上一條記錄按鈕代碼:
Private Sub up_Click()
Adodc1.Recordset.MovePrevious
If Adodc1.Recordset.BOF Then
Adodc1.Recordset.MoveFirst
End If
End Sub
下一條記錄代碼:
Private Sub down_Click()
Adodc1.Recordset.MoveNext
If Adodc1.Recordset.EOF Then
Adodc1.Recordset.MoveLast
End If
End Sub
末記錄代碼:
Private Sub mjl_Click()
Adodc1.Recordset.MoveLast
End Sub
刪除記錄代碼:
Private Sub Command3_Click()
Adodc1.Recordset.Delete
Adodc1.Recordset.MoveNext
If (Adodc1.Recordset.BOF Or Adodc1.Recordset.EOF) Then
MsgBox "已經無記錄", , "提示"
End If
End Sub
如果有什么不對的地方,請大家指正。