Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- strSQL = _
- "UPDATE MyTable SET MyField = 1234 WHERE IDField = 1"
- ' Use RunSQL to execute the statement.
- ' This will result in a user confirmation dialog.
- DoCmd.RunSQL strSQL
- ' Use DAO to execute the statement -- no dialog.
- CurrentDb.Execute strSQL, dbFailOnError
- Working with recordsets is more object-oriented, but usually less efficient
- than just executing SQL statements. Here are some examples:
- Dim db As DAO.Database
- Dim rst As DAO.Recordset
- Set db = CurrentDb
- ' Read a value from a specific record, ID known in advance:
- Set rst = db.OpenRecordset( _
- "SELECT MyField FROM MyTable WHERE IDField = 1")
- MsgBox "The value is" & rst!MyField
- rst.Close
- ' Open a recordset and loop through records, editing some of them:
- Set rst = db.OpenRecordset("MyTable")
- With rst
- Do Until .EOF
- ' Let's update those records where MyField is
- ' evenly divisible by 5
- If !MyField Mod 5 = 0 Then
- .Edit
- !MyField = !MyField / 5
- .Update
- End If
- .MoveNext
- Loop
- ' Let's find a particular record.
- .FindFirst "IDField = 123"
- ' Now let's delete the record we found.
- .Delete
- ' And now, let's add a new record.
- .AddNew
- !MyField = 10101
- !SomeTextField = "foo"
- ' Note: I'm assuming primary key is autonumber,
- ' so we don't need to -- and can't -- set it.
- .Update
- ' Always close the recordsets you open.
- .Close
- End With
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement