473,499 Members | 1,738 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_occurrences uio
SET fes_active_places =
(SELECT COUNT(*) FROM registration_units ru
WHERE ru.fes_unit_instance_code = uio.fes_uins_instance_code
AND ru.uio_occurrence_code = uio.calocc_occurrence_code
AND ru.progress_status = 'A'
AND ru.uio_occurrence_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 11259
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_occurrences uio " & _
"SET fes_active_places = " & _
"(SELECT COUNT(*) FROM registration_units ru " & _
"WHERE ru.fes_unit_instance_code = uio.fes_uins_instance_code " & _
"AND ru.uio_occurrence_code = uio.calocc_occurrence_code " & _
"AND ru.progress_status = 'A' " & _
"AND ru.uio_occurrence_code = " & Me!cboYear & ");"
MsgBox ("Test3")
'create query
Set qdfPassThru = db.CreateQueryDef("QueryName")
qdfPassThru.SQL = strSQL
qdfPassThru.Execute ' 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_instance_occurrences uio " & _
"SET fes_active_places = " & _
"(SELECT COUNT(*) FROM fes_registration_units ru " & _
"WHERE ru.fes_unit_instance_code = uio.fes_uins_instance_code " & _
"AND ru.uio_occurrence_code = uio.calocc_occurrence_code " & _
"AND ru.progress_status = 'A' " & _
"AND ru.uio_occurrence_code = " & Me!cboYear & ");"
MsgBox ("Test3")

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

qdfPassThru.SQL = strSQL
qdfPassThru.Execute ' 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
FishVal
2,653 Recognized Expert Specialist
I'm a bit confused....how does that code fit into mine? Which bits do i need to replace?
  • create working pass-through query in Query builder and save it as e.g. [qrySomeQuery]
  • then place code like the following in your form module
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdExecuteQuery_Click()
    2.     Dim strSQL As String
    3.  
    4.     strSQL = "UPDATE unit_instance_occurrences uio " & _
    5.              "SET fes_active_places = " & _
    6.              "(SELECT COUNT(*) FROM registration_units ru " & _
    7.              "WHERE ru.fes_unit_instance_code = uio.fes_uins_instance_code " & _
    8.              "AND ru.uio_occurrence_code = uio.calocc_occurrence_code " & _
    9.              "AND ru.progress_status = 'A' " & _
    10.              "AND ru.uio_occurrence_code = " & Me!cboYear & ");"
    11.  
    12.     With CurrentDb.QueryDefs("qrySomeQuery")
    13.         .SQL = strSQL
    14.         .Execute
    15.     End With
    16. End Sub
    17.  
Sep 19 '07 #11
Lewe22
94 New Member
Thanks!

Unfortunately still finding the same error "Operation must use an updateable query". I have just started another thread at....
http://www.thescripts.com/forum/show...22#post2826022

Any help on that would be great!!
Sep 19 '07 #12
FishVal
2,653 Recognized Expert Specialist
Does the query built in query builder work? If so, post the query SQL.
Sep 19 '07 #13
Lewe22
94 New Member
Unfortunately I don't have query builder. Can you suggest somewhere to get it from?
Sep 20 '07 #14
FishVal
2,653 Recognized Expert Specialist
Unfortunately I don't have query builder. Can you suggest somewhere to get it from?
I've meant query design which is the part of all Access versions.
Sorry if have confused you.
Sep 20 '07 #15
MMcCarthy
14,534 Recognized Expert Moderator MVP
Is "unit_instance_occurrences uio" a query or a table?
Sep 21 '07 #16
Lewe22
94 New Member
Is "unit_instance_occurrences uio" a query or a table?
"unit_instance_occurrences uio" is a table, as is "registration_units ru"
Sep 21 '07 #17
MMcCarthy
14,534 Recognized Expert Moderator MVP
"unit_instance_occurrences uio" is a table, as is "registration_units ru"
I'm trying to remember general SQL syntax. In Access a tablename which included spaces would have to be enclosed with square brackets. Whats the rule in your backend. You don't mention what backend system you are using.
Sep 21 '07 #18
Lewe22
94 New Member
My latest code...
Private Sub New_Click()
On Error GoTo err_New_Click
Dim db As DAO.Database
Dim qdfPassThru As DAO.QueryDef
Set db = CurrentDb
CurrentDb.QueryDefs("Active Update Query").SQL =
"UPDATE fes_unit_instance_occurrences uio " & _
"SET fes_active_places = " & _
"(SELECT COUNT(*) FROM fes_registration_units ru " & _
"WHERE ru.fes_unit_instance_code = uio.fes_uins_instance_code " & _
"AND ru.uio_occurrence_code = uio.calocc_occurrence_code " & _
"AND ru.progress_status = 'A' " & _
"AND ru.uio_occurrence_code = " & Me!cboYear & ");"
CurrentDb.QueryDefs("Active Update Query").Updatable
Set qdfPassThru = db.QueryDefs("Active Update Query")
qdfPassThru.Execute ' you can leave this out if you don't want to run the query
Set qdfPassThru = Nothing
Set db = Nothing
exit_New_Click:
Exit Sub
err_New_Click:
MsgBox (Err.Description)
End Sub
"fes_unit_instance_occurrences" is the table name "uio" it's alias and
"fes_registration_units" is the table name "ru" is it's alias.
Both tables are in my access db as ODBC linked tables to a SQL server.
Also, the combo box "cboYear" is linked to another ODBC table to pick up the year.
Sep 21 '07 #19
Lewe22
94 New Member
Sorry, the line...

"CurrentDb.QueryDefs("Active Update Query").Updatable"

has since been removed.
Sep 21 '07 #20
MMcCarthy
14,534 Recognized Expert Moderator MVP
Sorry, the line...

"CurrentDb.QueryDefs("Active Update Query").Updatable"

has since been removed.
You have set up "Active Update Query" as a pass through query though? It is an action query, yes?

Check the properties of the query.
Sep 21 '07 #21
Lewe22
94 New Member
Success at last!!!!!!!

Thank you so much.

Turns out it was as simple an error as the fact the "Active Pass Through" query was initially created as a Access update query. Upon recreating as pass through everything worked fine.
Sep 21 '07 #22
MMcCarthy
14,534 Recognized Expert Moderator MVP
Success at last!!!!!!!

Thank you so much.

Turns out it was as simple an error as the fact the "Active Pass Through" query was initially created as a Access update query. Upon recreating as pass through everything worked fine.
That's great. Glad you got it working.
Sep 21 '07 #23

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

Similar topics

7
6388
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...
2
11669
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. ...
2
30292
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...
15
7213
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? ...
10
2340
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...
11
4346
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...
2
1393
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. ...
4
2553
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...
13
3699
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...
0
7128
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,...
0
7169
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,...
1
6892
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...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4917
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...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.