condition sql statement with excel cell reference - sql

I am applying some SQL query into a spreadsheet, and have trouble to minimize number of worksheets/connections.
i have 3 scenarios, for each of them, I use same query
select *
from ***scenario_table***
where mycolumn > 100
Now I have to do this three times for different "scenario_table", and I really wanna to use a cell as reference (let's say cell $A$1)
I wanna sth like this
select *
from
case when [$A$1] = 1 ***scenario1_table***
case when [$A$1] = 2 ***scenario2_table***
case when [$A$1] = 3 ***scenario3_table***
where mycolumn > 100
my I know whether there is any way to work it out?
Thanks

This solution uses vba.
Assuming your cell with content is in Sheet1!A1 and there is only one conecction in your workbook, insert following code in Sheet1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A1")) Then
Select Case Me.Range("A1").Value
Case 1: SetTable ("Scenary1_Table")
Case 2: SetTable ("Scenary2_Table")
Case 3: SetTable ("Scenary3_Table")
End Select
End If
End Sub
Sub SetTable(ByVal TableName As String)
With ThisWorkbook.Connections(1)
.ODBCConnection.CommandText = "SELECT * FROM " & TableName
.Refresh
End With
End Sub

Related

MS Acces - select case mixing tables

I have problem with VBA code select case. Code is linked to combobox(Combo0) and event after update. In combobox there is a list of values: BHP, PPOZ, ISO. When i choose BHP, it should change source object property in subfrom(Child03) to subfrmBHP. Same with PPOZ and ISO. But for some reason, when i choose BHP it shows table from PPOZ, when i choose PPOZ it shows BHP and when i choose ISO it shows BHP. Code looks like this:
Private Sub Combo0_AfterUpdate()
Select Case ChangeCombo
Case Combo0 = "BHP"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmBHP"
Case Combo0 = "PPOŻ"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmPPOZ"
Case Combo0 = "ISO"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmISO"
End Select
End Sub
I aleredy recreated tables, form, sub form. Someone suggested me it could be data binding in subform, but there is nothing written there.
Your method references two controls, ChangeCombo and Combo0. Not sure if it's a typo or you have something else in mind.
Anyway, Case Combo0 = "BHP" is incorrect, so assuming Combo0 is the correct control, try the below.
Private Sub Combo0_AfterUpdate()
With Forms("frmInstrukcje").Form.Child3
Select Case Me.Combo0.Value
Case "BHP":
.SourceObject = "subfrmBHP"
Case "PPOŻ":
.SourceObject = "subfrmPPOZ"
Case "ISO":
.SourceObject = "subfrmISO"
End Select
End With
End Sub

How to update text box based on combo box value

Beginner on Using MS access 2016 and VBA. I am trying to show information on how many items have been reserved or is in the reserve table based on what the user selects as the tool.
The tool combo box(cmbo_Tool) selects a tool from Table A. I need to count the amount of times this tool appears in Table B and then display it in textBox A.
I have made a query involving both tables, but i'm unsure on how to apply it to the label.
Instead I have used the AfterUpdate event on cmbo_tool and using the DCount option.
Another way I thought about is taking the Tool Id (in this case, say 5) from Table A, and searching for it in a column table B, and counting.
'using Dcount'
Private Sub cmbo_Tool_AfterUpdate()
Me.Text1404 = DCount("cmbo_Tool", "tbl_Booking", "Tool")
End Sub
'Using Table id'
Private Sub cmbo_Tool_AfterUpdate()
Dim T_var as integer
Dim FinalOut as integer
T_var = Me.cmbo_Tool.Column(0) 'This gives 5'
'I need to make T_Var link to Table b and count'
Me.Text1404 = FinalOut
End Sub
Using Dcount method, it gives a number im unsure of. I'm not even sure if im using dcount correctly.
Using the table id method, Im unsure how to take the value 5 and count it in table B, then display in the textbox.
I guess it could be something like this:
Private Sub cmbo_Tool_AfterUpdate()
Me!Text1404.Value = DCount("*", "tbl_Booking", "Tool = " & cmbo_Tool & "")
' If Tool is text, then use quotes:
' Me!Text1404.Value = DCount("*", "tbl_Booking", "Tool = '" & cmbo_Tool & "'")
End Sub
And do rename your controls to something meaningful.

comparing a table and a query

I have a form, a table ("my_table") and a query ("my_query").
I want a function to look if any of the table's record's IDs ("my_ID") match with the IDs of the query (also "my_ID") to update a field ("my_Property") of given record with a value from the form.
I copied and modified this code. My code causes an error (have to translate): "Runtime error '3061': 1 parameter was expected, but too few parameters were passed."
I think the issue is that in the if loop I don't compare tbl.Fields("my_ID") with a discreet value but with a set values. Maybe I should also iterate through all values of qry.Fields("my_ID") but I don't see how to do the code. Also, this would significantly slow down the already slow process since my_Table contains more than 40,000 records. Is there a faster method of comparing the ids?
Private Sub btn_Click()
Dim db As Database
Set db = CurrentDb
Dim tbl As Recordset
Set tbl = db.OpenRecordset("my_Table")
Dim qry As QueryDef
Set qry = db.OpenRecordset("my_Query")
tbl.MoveFirst
Do Until tbl.EOF
If tbl.Fields("my_ID") = qry.Fields("my_ID") Then
tbl.Edit
tbl.Fields("my_Property") = Me!textbox1.Value
tbl.Update
End If
tbl.MoveNext
Loop
End Sub
As #Harassed Dad and #Andre suggested I simply copied "my_query" and changed the copy to an update query "my_updatequery" with the update field of "my_Property" set to
[Forms]![My_Form]![tbx_my_Value]. Then I changed the code for the button to:
Private Sub btn_Click()
DoCmd.OpenQuery "my_updatequery"
Forms.my_Form.Subform2.Requery
End Sub
Forms.my_Form.Subform2.Requery updates basically a subform showing the "my_query"-query.
I will look further for another way to call the update query to avoid the two prompts that opening an update query always entails.
edit:
To to avoid the propmts I simply turned Warnings off and on again:
Private Sub btn_Click()
DoCmd.SetWarnings False
DoCmd.OpenQuery "my_updatequery"
DoCmd.SetWarnings True
Forms.my_Form.Subform2.Requery
End Sub
Not the most elegant way but it will do the job.

How to add BOTH static values and records from a table as list items to a combo box in VBA

I have a combo box that gets its list items from a query. But in addition to these items, I need one static option that will be there in all cases.
I tried doing this in the naive way:
Private Sub Form_Open(Cancel As Integer)
Set rst = CurrentDb.OpenRecordset("SELECT ESDNodes.Description, ESDNodes.Sort FROM ESDNodes WHERE (((ESDNodes.parentID) =" & parentID & ")) ORDER BY ESDNodes.Sort")
Set nextSiblingSelect.Recordset = rst
nextSiblingSelect.AddItem Item:="Make Last", Index:=0
End Sub
but end up with this run-time error
The RowSourceType property must be set to 'Value List' to use this method.
which I half-expected.
Is there any trick around this? I suppose I could add a dummy record to my table set and change the query but that would be rather ugly and I would not prefer it.
Try a UNION query as the combobox RowSource. Maybe like:
SELECT Description, Sort FROM ESDNodes
UNION SELECT "Make Last", 0 FROM ESDNodes ORDER BY Sort;

Passing value from the textbox to "TOP n" clause in SQL query

I believe I have quite simple question, that would help me finish my project. I don't usually work with Access, but I was asked to help someone so here I am.
My problem: I have a Form1 called "Start" in which there is a TextBox1 called "Kat1".
I also have a SQL Query as:
SELECT TOP 3 tbl.Example FROM TABLE TBL
What I want to achieve is to let the user to write some number in "Kat1" so that Query returns this much top rows.
I hope there is a way to this without using VBA, since my query is rather complicated, there are more textboxes, more subqueries with selecting top rows etc.
I tried putting SELECT TOP [Start]![Kat1]!Value or simmilar. There maybe something wrong with my syntax or maybe this is all wrong and there is another way.
Thank for help in advance.
Edit:
For future readers ;) This is how I solved it with VBA:
Sub Query_Change()
SQLstring = "SELECT TOP KAT1 col1 FROM TBL UNION SELECT TOP KAT2 col1 FROM TBL etc..."`
CurrentDb.QueryDefs("MyQuery").SQL = SQLstring
For i = 1 To 4
SQLstring = Replace(SQLstring, "KAT" & i, Forms!Start!("Kat" & i).Value)
Next i
CurrentDb.QueryDefs("MyQuery").SQL = SQLstring
End Sub
The code will run after user puts values into TextBoxes.
This is not possible in a Access query, the "TOP n" part cannot take a variable / parameter.
(It's possible in SQL Server, see Dynamic SELECT TOP #var In SQL Server )
You need VBA to do it. Either, since it's in the very first part of a SELECT statement, read the original Querydef.SQL and edit it (replace the 3rd "word").
Or have a table with template SQL code, with e.g.
SELECT TOP $count$ FROM table WHERE ...
Replace $count$ with your number and write the result to a querydef.
Edit
In this case, I would definitely go the template route.
Build your query "qUnionTop" with sample values for TOP n.
Copy the SQL to a table where you store the template SQL.
Edit the SQL with variables, e.g.
SELECT TOP $c1$ col1 FROM tblx UNION SELECT TOP $c2$ col1 FROM TBLY UNION ...
Before you open your query run code like this:
Sub DynamicQueryFromTemplate()
Dim S As String
Dim i As Long
' Read template SELECT SQL from tblTemplates
S = DLookup("Sql", "tblTemplates", "Key = 'qUnionTop'")
' Replace $c[x]$
For i = 1 To 4
S = Replace(S, "$c" & i & "$", Forms!Start("Kat" & i).Value)
Next i
CurrentDb.QueryDefs("qUnionTop").SQL = S
End Sub
Whenever your query needs to change, repeat steps 2.+3.
On the form properties go to the event tab. On the on load event you should create an Event Procedure looking like the following:
Option Compare Database
Private Sub Command0_Click()
Dim kat As String
kat = "SELECT TOP " +Me.Kat1.Value + "TableName.TableField "
CurrentDb.QueryDefs("QueryName") .SQL = kat
On Error Resume Nest
DoDmd.RunQuery "QueryName"
End Sub
Private Sub Form_Load()
End Sub
Private Sub testrun()
End Sub