Solved

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

Posted on 2008-10-23
5
1,602 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 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now