Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I run an update query in code?

Status
Not open for further replies.

tombirch

Programmer
Sep 2, 2000
16
DK
Hi
In a code I am trying to run several queries that update tables in my database.
So far I am able to run the queries using
docmd.OpenQuery
but this gives me no way to handle errors, and it also pop up a window asking me if I will allow the update to take place (which I don't want)
I have tried using recordset such as:
set qresult=db.openrecordset("queryname")
but this dosn't seem to work with an update queries.
Does anyone know how to run an update query from code?

Regards

Tombirch
 
dim strsql as string

strsql = UPDATE TBLName SET TBLname.firstname = "Thomas"
WHERE TBLname.firstname ="tomas"

docmd.setwarnings false 'turns off warnings
docmd.runsql (strsql)
docmd.set warnings true ' turns then back on
 
OK - so how can you run just a plain query with code? Seek not outside yourself; heaven is within.
 
scroce,
Here is a sample select query:

SELECT Employees.EmployeeID, Employees.LastName, Employees.FirstName, Employees.Title
FROM Employees;

Here is the code to run this select query, hope this helps your question.

Dim strSQl As String
strSQL = "SELECT Employees.EmployeeID" & vbCrLf
strSQL = strSQL & " , Employees.LastName" & vbCrLf
strSQL = strSQL & " , Employees.FirstName" & vbCrLf
strSQL = strSQL & " , Employees.Title" & vbCrLf
strSQL = strSQL & " FROM Employees;"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top