473,566 Members | 3,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting a value from an Access form into a SQL pass through query

94 New Member
I am creating a small Access db which performs a series of updates to a SQL database.

The Access db consists of a ‘Main Form’, from which the user can run each update via a series of command buttons.

Each update has been written as a SQL pass-through query.

Example Update:
UPDATE unit_instance_o ccurrences uio
SET fes_active_plac es =
(SELECT COUNT(*) FROM registration_un its ru
WHERE ru.fes_unit_ins tance_code = uio.fes_uins_in stance_code
AND ru.uio_occurren ce_code = uio.calocc_occu rrence_code
AND ru.progress_sta tus = 'A'
AND ru.uio_occurren ce_code = 07);
Each query is dependant on a year (Eg: 07) however I want the user to determine that year from a combo box on the ‘Main Form’.

How do I best go about this?

The key thing is that i want to be able to run my pass through query from a button on the form. Before clicking the button the user must select the year. Somehow I either want to be able to insert that year as a parameter into the body of the PTQ or if necessary I can rewrite the pass-through update queries as pure Access-update queries and insert it there instead.

Have pretty much hit a wall with this so any help would be greatly appreciated!
Sep 13 '07 #1
22 11286
MMcCarthy
14,534 Recognized Expert Moderator MVP
The best way to do this is to create the query dynamically in VBA. For the code to work the query will already have to be created.

For the purposes of this code I have put it on a command button called cmdExecuteQuery and I've given the combo box the name cboYear as I don't know it.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExecuteQuery()
  2. Dim db As DAO.database
  3. Dim qdfPassThru As DAO.QueryDef
  4. Dim strSQL As String
  5.  
  6.     Set db = CurrentDb
  7.     strSQL = "UPDATE unit_instance_occurrences uio " & _
  8.              "SET fes_active_places = " & _
  9.              "(SELECT COUNT(*) FROM registration_units ru " & _
  10.              "WHERE ru.fes_unit_instance_code = uio.fes_uins_instance_code " & _
  11.              "AND ru.uio_occurrence_code = uio.calocc_occurrence_code " & _
  12.              "AND ru.progress_status = 'A' " & _
  13.              "AND ru.uio_occurrence_code = " & Me!cboYear & ");"
  14.  
  15.     'create query
  16.     Set qdfPassThru = db.CreateQueryDef("QueryName")
  17.     qdfPassThru.SQL = strSQL
  18.     qdfPassThru.Execute ' you can leave this out if you don't want to run the query
  19.  
  20.     Set qdfPassThru = Nothing
  21.     Set db = Nothing
  22.  
  23. End Sub
  24.  
Sep 14 '07 #2
Lewe22
94 New Member
Thanks for your reply!
Have copied the code in exactly and renamed my button and combo box accordingly but am still having a problem. Nothing is happening at all when i click the button. By placing a series of messsage boxes in the code i have narrowed it down to the fact that my VB doesn't seem to understand the first 2 commands.

Dim db As DAO.database
Dim qdfPassThru As DAO.QueryDef

Even putting a message box infront of those two lines still nothing happens.
When i cut those two lines out i see the message boxes.

Any ideas???

Private Sub cmdExecuteQuery _Click()
MsgBox ("Test1")
Dim db As DAO.database
Dim qdfPassThru As DAO.QueryDef
Dim strSQL As String
MsgBox ("Test2")
Set db = CurrentDb
strSQL = "UPDATE unit_instance_o ccurrences uio " & _
"SET fes_active_plac es = " & _
"(SELECT COUNT(*) FROM registration_un its ru " & _
"WHERE ru.fes_unit_ins tance_code = uio.fes_uins_in stance_code " & _
"AND ru.uio_occurren ce_code = uio.calocc_occu rrence_code " & _
"AND ru.progress_sta tus = 'A' " & _
"AND ru.uio_occurren ce_code = " & Me!cboYear & ");"
MsgBox ("Test3")
'create query
Set qdfPassThru = db.CreateQueryD ef("QueryName" )
qdfPassThru.SQL = strSQL
qdfPassThru.Exe cute ' you can leave this out if you don't want to run the query
MsgBox ("Test4")
Set qdfPassThru = Nothing
Set db = Nothing
MsgBox ("Test5")
End Sub
Sep 17 '07 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
Thanks for your reply!
Have copied the code in exactly and renamed my button and combo box accordingly but am still having a problem. Have narrowed it down to the fact that....

Dim db As DAO.database
Dim qdfPassThru As DAO.QueryDef

doesn't seem to be recognised in my visual basic. Any ideas?????
In the VBA editor, go to Tools - References and in the list of ticked reference libraries find the one called Microsoft DAO X.X Object Library and tick it.
Sep 17 '07 #4
FishVal
2,653 Recognized Expert Specialist
Hi, there.

Check whether your button name is [cmdExecuteQuery]
Check whether your button "OnClick" property is still properly set.
  • Go to form design view.
  • Open the button properties window.
  • Check whether "OnClick" is set to [Event Procedure]
  • Click on [...] button rightward to get to cmdExecuteQuery _Click sub.

P.S. Never mind. Haven't read your last post thoroughly. The problem definitely seems to be in references.
Sep 17 '07 #5
Lewe22
94 New Member
Thanks for both your help!

At the risk of sounding stupid......... It is now falling over after MsgBox3
Private Sub cmdExecuteQuery _Click()
MsgBox ("Test1")

Dim db As DAO.database
Dim qdfPassThru As DAO.QueryDef
Dim strSQL As String
MsgBox ("Test2")

Set db = CurrentDb
strSQL = "UPDATE fes_unit_instan ce_occurrences uio " & _
"SET fes_active_plac es = " & _
"(SELECT COUNT(*) FROM fes_registratio n_units ru " & _
"WHERE ru.fes_unit_ins tance_code = uio.fes_uins_in stance_code " & _
"AND ru.uio_occurren ce_code = uio.calocc_occu rrence_code " & _
"AND ru.progress_sta tus = 'A' " & _
"AND ru.uio_occurren ce_code = " & Me!cboYear & ");"
MsgBox ("Test3")

'create query
Set qdfPassThru = db.CreateQueryD ef("Test")
MsgBox ("Test4")

qdfPassThru.SQL = strSQL
qdfPassThru.Exe cute ' you can leave this out if you don't want to run the query
MsgBox ("Test5")

Set qdfPassThru = Nothing
Set db = Nothing
MsgBox ("Test6")

End Sub
I can see that the update query "Test" is being created correcly but it does not run.
When trying to run "Test" independently i get the error message "Operation must use an updateable query"

Hope you can help again!!
Sep 18 '07 #6
FishVal
2,653 Recognized Expert Specialist
Hi, there.

At the risk of sounding stupid......... Did you delete the query before run the code 2nd time?
Sep 18 '07 #7
Lewe22
94 New Member
I have tried that, it just doesn't seem to get any further than trying to execute the update query...
Sep 18 '07 #8
FishVal
2,653 Recognized Expert Specialist
I have tried that, it just doesn't seem to get any further than trying to execute the update query...
I suggest you to create working pass-through query in query builder.
Then in code just change SQL expression.
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.QueryDefs("qrySomeQuery").SQL="......."
  2.  
Sep 18 '07 #9
Lewe22
94 New Member
I'm a bit confused....how does that code fit into mine? Which bits do i need to replace?
Sep 19 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
6400
by: rickcheney | last post by:
I just changed my Access 2002 database to a SQL Server ADP project. I had a form where the user entered a value into a text box and when a command button on the form was clicked a Report was opened. The reports record source is a query. The query uses the value from the form text box to restrict the query. Table name = EggsTable one of the...
2
11677
by: Josh Strickland | last post by:
I am attempting to create an Access database which uses forms to enter data. The issue I am having is returning the query results from the Stored Procedure back in to the Access Form. tCetecM1CUST (SQL Table that contains the Customer Information) tAccountingDetail (SQL Table that contains the information in the form) frmAccountingEntry...
2
30304
by: rg | last post by:
Hi all, I have query about passing a parameter from form to a query. My situation is as follows: I have a query that pulls up some data from a table, however there is a condition that must be filled in by the user. Hence when you run the query there is a "Enter Parameter Value" prompt that must be completed. A further complication is...
15
7221
by: brettclare | last post by:
I have linked a large SQL Server table to Access, however 'only' 2,195,439 records are shown and are available to query. Can I increase the size (cache??)/number of records showing in Access? Thank you.
10
2351
by: Hank | last post by:
We have just recently migrated the data from our Access 2000 backend to Postgres. All forms and reports seem to run correctly but, in many cases, very slowly. We do not want to switch over until we can speed things up. We would like to start implementing Stored Procedures so we can do Server-Side processing. Can anyone recommend a book...
11
4353
by: c676228 | last post by:
Hi everyone, I am just wodering in asp program, if there is anybody writing store procedure for inserting data into database since there are so many parameters need to be passed into store procedure(assume there are many columns in the table). I need to insert data into two separate tables, the relation between these two tables is 1 row of...
2
1396
by: Lewe22 | last post by:
I am creating a small Access db which performs a series of updates to a SQL db. The Access db consists of a ‘Main Form’, from which the user can run each update via a series of command buttons. Each update has been written as a SQL pass-through query. Example Update: UPDATE unit_instance_occurrences uio SET fes_active_places =
4
2557
by: sialater | last post by:
Hello, I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have members. I have this formatted in a way where the groups are in a drop down box. Once a group is selected, the two selection boxes below are refreshed...
13
3708
by: magickarle | last post by:
Hi, I got a pass-through query (that takes about 15 mins to process) I would like to integrate variables to it. IE: something simple: Select EmplID from empl_Lst where empl_lst.timestamp between !! And !! Not sure how to do so (should it be a query in Access or a macro) The connection would be ODBC.
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.