Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access 2007 How to execute SQL statement in Report event (On Open) in VB

Posted on 2008-10-23
5
Medium Priority
?
1,653 Views
Last Modified: 2013-11-28
First, my knowledge of VB and Access is close to none.

I want execute an INSERT SQL statement when an Access report is opened.
I have created my Access report. Then I have created a method for the On Open event of the report that looks like this:

Private Sub Report_Open(Cancel As Integer)
   CurrentDb.Execute("INSERT INTO dbo_example VALUE('hello')")        
End Sub

However, if I try to open the report I get the Run-time error 3151 saying that ODBC connection failed.
The Report is linked to a Record Source that is a query. The report works fine if I remove this method. The error is really thrown at that line (CurrenDb.Execute)

How can I get an opened database connection from the Report's event?
Thanks in advance.
0
Comment
Question by:jboula
  • 3
  • 2
5 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22794595
First, can you tell us why you need to do this?

Perhaps there is a better way.
0
 

Author Comment

by:jboula
ID: 22796703
I want to keep a record in the database each time a report is opened/printed by the user. This is sort of an audit trail.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22799122
jboula,

Can you run this query on it's own?

For one thing, I cannot see what "Field" you are trying to insert your value into.

The Syntax for an Insert Query is:

INSERT INTO YourTable (YourField)
VALUES (YourValue)

An example in your case might look something like this:
INSERT INTO tblReportRunCounter (RunNumber)
VALUES (5)

JeffCoachman
0
 

Author Comment

by:jboula
ID: 22799260
The query was just an example, it is not the problem. The point was just that I wanted to execute a SQL statement from the report on open event. I think a found an alternative way to do it which seem to work:

DoCmd.RunSQL ("INSERT INTO dbo_Rapports (RAMQ_ID, CreatedAt) VALUES ('XXXX12345678',  NOW())")

The only annoying thing is that it shows a warning message to the user: "Your about to append X records to the database". Anyway to suppress that message?
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 1500 total points
ID: 22799588
jboula,

You need to toggle the warning alerts on/off:

'Disable SQL warning prompts
Docmd.Setwarnings False
DoCmd.RunSQL ("INSERT INTO dbo_Rapports (RAMQ_ID, CreatedAt) VALUES ('XXXX12345678',  NOW())")
'Re-enable SQL warning prompts
Docmd.Setwarnings True


JeffCoachman
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question