Vous êtes sur la page 1sur 11

dim db as new adodb.connection 'then open your database with db connection dim r as new adodb.recordset r.

open "select Name from Teacher", db, adopendynamic, adlockoptimistic, adcmdtext 'I guess column name is Name do while not r.eof combo1.additem(r.fields(0)) r.movenext loop

1. Private Sub Form_Load() 2. Dim RS As ADODB.Recordset 3. Dim Conn As ADODB.Connection 4. Dim Comm As ADODB.Command 5. Dim str As String 6. 7. Set Conn = New ADODB.Connection 8. Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =
D:\Only Me\Authors.mdb"

9. Conn.Open 10. Set Comm = New ADODB.Command 11. Set Comm.ActiveConnection = Conn 12. Comm.CommandText = "SELECT Au_ID FROM Authors" 13. Comm.CommandType = adCmdText 14. Set RS = Comm.Execute 15. 16. Do Until RS.EOF = True

17. Combo1.AddItem RS!Au_ID 18. RS.MoveNext 19. Loop 20. 21. Set RS = Nothing 22. Set Comm = Nothing 23. Set Conn = Nothing 24. End Sub

Editing data

EDIT Module1.setConnectProc Set globRSrecordset = New ADODB.Recordset globRSrecordset.Open "SELECT * FROM table_name WHERE BkTitle='" & Trim(txtTitle) & "'", strVar, adOpenKeyset, adLockOptimistic With globRSrecordset If Not .EOF Then !BkTitle = txtTitle !Author = txtAuthor !BkRefNo = Val(txtBkRefNo) !TrnDate = txtDate MsgBox "Record Updated!", vbInformation, "Updated Existing Record" .Update .MoveLast End If .Close End With

Delete

DELETE connectModule.setConnectProc Set globRSrecordset = New ADODB.Recordset globRSrecordset.Open "SELECT * FROM table_name WHERE BkTitle='" & Trim(txtTitle) & "'", strVar, adOpenKeyset, adLockOptimistic With globRSrecordset If Not .EOF Then .Delete End If .Close End With Public globDBConnect As ADODB.Connection Public globRSrecordset As ADODB.Recordset Public strVar As String Public Sub setConnectProc() Set globDBConnect = New ADODB.Connection strVar = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Location\DatabseName.accdb;Pers ist Security Info=False" globDBConnect.Open strVar End Sub

Instraction For Safe Project Load Components Add Your Project 1) Add Microsoft ADO Data Control 6.0 (OLEDB) 2) Add Microsoft Windows Commom Controls-2.6 Your Project Dim db As ADODB.Connection Dim rst As ADODB.Recordset Dim id As Integer Private Sub Form_Load()

Open Database Connection Set db = New ADODB.Connection db.Open Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\s4sajoy SampleCode\SampleDB.mdb;Persist Security Info=False Set rst = New ADODB.Recordset rst.Open select * from tblSample Order By SerialNo DESC, db, adOpenStatic, adLockOptimistic, adCmdText If rst.RecordCount = 0 Then id = 1 Else id = rst(SerialNo) + 1 End If rst.Close lblID.Caption = id End Sub Private Sub cmdAdd_Click() If Trim(txtName.Text) = Then MsgBox input name, vbInformation Else Set rst = New ADODB.Recordset

rst.Open select * from tblSample, db, adOpenStatic, adLockOptimistic, adCmdText rst.AddNew rst(SerialNo) = CInt(lblID.Caption) rst(Date) = DTPicker1.Value rst(Name) = Trim(txtName.Text) rst.Update rst.Close MsgBox One Person Added. & Chr(13) & Nmae : & Trim(txtName.Text), vbInformation id = id + 1 lblID.Caption = id DTPicker1.Value = Date txtName.Text = txtName.SetFocus End If End Sub Private Sub cmdUpdate_Click() If Trim(txtID.Text) = Then MsgBox input id no

txtID.SetFocus Else Set rst = New ADODB.Recordset rst.Open UPDATE tblSample SET tblSample.Date = # & DTPicker1.Value & #, tblSample.Name = & txtName.Text & WHERE tblSample.SerialNo= & txtID.Text, db, adOpenStatic, adLockOptimistic, adCmdText MsgBox One Row Updated End If End Sub Private Sub cmdDelete_Click() If Trim(txtID.Text) = Then MsgBox input id no txtID.SetFocus Else: Set rst = New ADODB.Recordset rst.Open Delete * From tblSample Where SerialNo= & txtID.Text, db, adOpenStatic, adLockOptimistic, adCmdText MsgBox Delete Successfull End If End Sub

dim con as connection Private Sub Command3_Click() rec.Open "update from inventory where p_name = '" & Text1.Text & "', p_price = " & Text2.Text & ", p_quantity = " & Text3.Text & "", con, adOpenStatic, adLockOptimistic MsgBox "Updated" Unload Me End Sub

1) Add ADODC control 2) Right Click on ADODC control and select ADODC properties 3) Select "Use Connection String" option, and click on "Build" 4 In "Connection" Tab, Enter the complete path of your student database 5) Click on "Test Connection" to verify database connection 6) Carry on with your code as like the Data Control Example: Adodc1.RecordSource = "select * from students" Adodc1.Refresh MsgBox Adodc1.Recordset.RecordCount

1. Private Sub Add_Click() 2. Adodc1.Refresh 3. Adodc1.Recordset.AddNew 4. Adodc1.Recordset.Fields("Au_Id") = Text1.Text 5. Adodc1.Recordset.Fields("Author") = Text2.Text 6. Adodc1.Recordset.Fields("YearBorn") = Text3.Text

7. Adodc1.Recordset.Update 8. MsgBox "Data Added" 9. End Sub

(In the General Declaration Area) Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset (in Form's Activate Event) cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=EMP.mdb;Persist Security Info=False" cn.Open rs.Open "select Eid,Ename,ppfSub,Basic From EmpData Order By Eid", cn, adOpenStatic, adLockPessimistic Text1.text=rs.Fields(0) etc

Option Explicit Private Dim Dim Dim Dim Sub cmdOpen_Click() Conn1 As New adodb.Connection Cmd1 As New adodb.Command Errs1 As Errors Rs1 As New adodb.Recordset

Dim i As Integer Dim AccessConnect As String ' Error Handling Variables Dim errLoop As Error Dim strTmp As String AccessConnect = "Driver={Microsoft Access Driver (*.mdb)};" & _

"Dbq=nwind.mdb;" & _ "DefaultDir=C:\program files\devstudio\vb;" & _ "Uid=Admin;Pwd=;" '--------------------------' Connection Object Methods '--------------------------On Error GoTo AdoError ' Full Error Handling which traverses ' Connection object

' Connection Open method #1: Open via ConnectionString Property Conn1.ConnectionString = AccessConnect Conn1.Open Conn1.Close Conn1.ConnectionString = "" ' Connection Open method #2: Conn1.Open AccessConnect Conn1.Close Open("[ODBC Connect String]","","")

' Connection Open method #3: Open("DSN","Uid","Pwd") Conn1.Open "Driver={Microsoft Access Driver (*.mdb)};" & _ "DBQ=nwind.mdb;" & _ "DefaultDir=C:\program files\devstudio\vb;" & _ "Uid=Admin;Pwd=;" Conn1.Close

'-------------------------' Recordset Object Methods '-------------------------' Don't assume that we have a connection object. On Error GoTo AdoErrorLite ' Recordset Open Method #1: Open via Connection.Execute(...) Conn1.Open AccessConnect Set Rs1 = Conn1.Execute("SELECT * FROM Employees") Rs1.Close Conn1.Close ' Recordset Open Method #2: Open via Command.Execute(...) Conn1.ConnectionString = AccessConnect Conn1.Open Cmd1.ActiveConnection = Conn1 Cmd1.CommandText = "SELECT * FROM Employees" Set Rs1 = Cmd1.Execute Rs1.Close Conn1.Close Conn1.ConnectionString = "" ' Recordset Open Method #3: Open via Command.Execute(...) Conn1.ConnectionString = AccessConnect Conn1.Open

Cmd1.ActiveConnection = Conn1 Cmd1.CommandText = "SELECT * FROM Employees" Rs1.Open Cmd1 Rs1.Close Conn1.Close Conn1.ConnectionString = "" ' Recordset Open Method #4: Open w/o Connection & w/Connect String Rs1.Open "SELECT * FROM Employees", AccessConnect, adOpenForwardOnly Rs1.Close Done: Set Rs1 = Nothing Set Cmd1 = Nothing Set Conn1 = Nothing Exit Sub

AdoError: i = 1 On Error Resume Next ' Enumerate Errors collection and display properties of ' each Error object (if Errors Collection is filled out) Set Errs1 = Conn1.Errors For Each errLoop In Errs1 With errLoop strTmp = strTmp & vbCrLf & "ADO Error # " & i & ":" strTmp = strTmp & vbCrLf & " ADO Error # " & .Number strTmp = strTmp & vbCrLf & " Description " & .Description strTmp = strTmp & vbCrLf & " Source " & .Source i = i + 1 End With Next AdoErrorLite: ' Get VB Error Object's information strTmp = strTmp & vbCrLf & "VB Error # " & Str(Err.Number) strTmp = strTmp & vbCrLf & " Generated by " & Err.Source strTmp = strTmp & vbCrLf & " Description " & Err.Description MsgBox strTmp ' Clean up gracefully without risking infinite loop in error handler On Error GoTo 0 GoTo Done End Sub

Vous aimerez peut-être aussi