Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.32 KB | None | 0 0
  1. strSQL = _
  2. "UPDATE MyTable SET MyField = 1234 WHERE IDField = 1"
  3.  
  4. ' Use RunSQL to execute the statement.
  5. ' This will result in a user confirmation dialog.
  6. DoCmd.RunSQL strSQL
  7.  
  8. ' Use DAO to execute the statement -- no dialog.
  9. CurrentDb.Execute strSQL, dbFailOnError
  10.  
  11. Working with recordsets is more object-oriented, but usually less efficient
  12. than just executing SQL statements. Here are some examples:
  13.  
  14. Dim db As DAO.Database
  15. Dim rst As DAO.Recordset
  16.  
  17. Set db = CurrentDb
  18.  
  19. ' Read a value from a specific record, ID known in advance:
  20. Set rst = db.OpenRecordset( _
  21. "SELECT MyField FROM MyTable WHERE IDField = 1")
  22. MsgBox "The value is" & rst!MyField
  23. rst.Close
  24.  
  25. ' Open a recordset and loop through records, editing some of them:
  26. Set rst = db.OpenRecordset("MyTable")
  27. With rst
  28. Do Until .EOF
  29. ' Let's update those records where MyField is
  30. ' evenly divisible by 5
  31. If !MyField Mod 5 = 0 Then
  32. .Edit
  33. !MyField = !MyField / 5
  34. .Update
  35. End If
  36. .MoveNext
  37. Loop
  38.  
  39. ' Let's find a particular record.
  40. .FindFirst "IDField = 123"
  41.  
  42. ' Now let's delete the record we found.
  43. .Delete
  44.  
  45. ' And now, let's add a new record.
  46. .AddNew
  47. !MyField = 10101
  48. !SomeTextField = "foo"
  49. ' Note: I'm assuming primary key is autonumber,
  50. ' so we don't need to -- and can't -- set it.
  51. .Update
  52.  
  53. ' Always close the recordsets you open.
  54. .Close
  55.  
  56. End With
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement