?
Solved

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

Posted on 2008-10-23
5
Medium Priority
?
1,650 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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 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