Solved

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

Posted on 2008-10-23
5
1,637 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Familiarize people with the process of utilizing SQL Server views 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 Access…
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.

734 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