Update Query in VBA

Montez659

Well-known Member
Joined
May 4, 2005
Messages
918
Is it possible to trigger an Access Update query via VBA, or is this another one of those parameter issues?

I had
Code:
DoCmd.OpenQuery qryUpdateAfterImport
which
triggers an update to a table from a table, but it doesn't seem to be triggering but also there are no error messages.

Tried to wade through what Google threw back at me, but those all deal with an SQL string in VBA, which is possible, it will just be a fairly lengthy bit o' code. I'd ratehr trigger the already concoted update query.

Thanks wise men and women!
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
And of course I found it 3.265 seconds after I post.
Code:
CurrentDb.Execute "qryUpdateAfterImport"
 
Upvote 0
And of course I found it 3.265 seconds after I post.
Code:
CurrentDb.Execute "qryUpdateAfterImport"

You should also use the part in red so it returns an error if there is an error or else it won't:

CurrentDb.Execute "qryUpdateAfterImport", dbFailOnError
 
Upvote 0
I could stop running code if there is an error, so introduce an error trap:
Code:
On Err Resume Next
CurrentDb.Execute "qryUpdateAfterImport", dbFailOnError
If Err Then
    '//Update Failed.  Let somebody know.
Else
    '//All's well that ends well.
End If
On Error GoTo 0
 
Upvote 0
Thanks Xen. I've always been a bit sketchy on proper error trapping. I don't know how many books that I have read but for some reason my brain has a hard time wrapping around it.
 
Upvote 0
All in good time.
I think if you read (and re-read) the VBA help on Resume and follow the links to the related articles you'll get all you need to know. VBA error handling is not as sophisticated as in most languages today (i.e, Try-Catch structure). But it gets the job done. Note that it's better not to use a lot of error trapping while writing code because you learn a lot when your code crashes and you are thrown into debug mode - mainly, you know exactly where the error occurred.

I should have given an example like this, as you can get more information about the error.
Code:
On Err Resume Next
CurrentDb.Execute "qryUpdateAfterImport", dbFailOnError
If Err Then
    [COLOR="Blue"]Msgbox "Error " & Err.Number & ": " & Err.Description[/COLOR]
Else
    '//All's well that ends well.
End If
On Error GoTo 0
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top