Vba requery parameter syntax - vba

I need to set the parameter for a requery command, but I canĀ“t find the correct syntax.
tbTreinamentosConc.Requery Profissional = prof
tbTreinamentosConc refers to table Trn_Done, which has the column Profissional, among the others and prof is the parameter I want to be used when searching in the table Trn_Done, in the field Profissional.
What is the correct way to get it working?
Thanks in advance!

A requery will simply update the information in a given object, you can't pass arguments to it. You can however write an SQL statement to query for something specific...
Dim strSQL As String
strSQL = "SELECT * " & _
"FROM tbTreinamentosConc "
"WHERE Profissional ='" & txtInput & ";"
DoCmd.RunSQL strSQL
txtInput will be a text box on your field that you can enter the 'prof' parameter into; This should work.

Related

Outputting the result of an SQL query into a text box

I'm creating a form that displays information about an individual that is taken from two tables when a name is entered by the user. I want the fields to be output into they're own text boxes. My code looks similar to what's below.
When I run the code it displays the literal query "SELECT name etc..." in the textbox. I saw Dlookup works for textboxes but to my understanding it doesn't work well with more than one table. Any advice would be greatly appreciated!
PS I'm a VBA/access newbie
Dim SQL, SearchInput As String
SQL = "SELECT name" & _
"FROM tablename INNER JOIN othertablename ON tablename.name = othertablename.name" & _
"WHERE tablename.name LIKE ""*" & SearchInput & "*""
Me.txtbox = SQL
I'm pretty sure this is a duplicate, but it's faster to answer than hunt for the other posts.
You need to declare a recordset and assign the data returned from the select statement to it. This will leave you with something very similar to an array. After that you need to just line up the array element to the positions of the columns. IE. rs(0)=name in the select statement above.
Dim rs As Recordset
Dim SQL As String, SearchInput As String
SQL = "SELECT name " & _
"FROM tablename INNER JOIN othertablename ON tablename.name = othertablename.name " & _
"WHERE tablename.name LIKE ""*" & SearchInput & "*""
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
Me.txtBox = rs(0)
That should work.
Since you are new to MS Access & VBA in general, I'll propose a solution which avoids VBA entirely.
In your current code, I assume that SearchInput is sourcing its value from another control on your form.
I would suggest creating a new saved query (called it MyQuery) with the following SQL:
select table2.name from table1 inner join table2 on table1.name = table2.name
Then, in the Control Source for your textbox, use a DLookup expression with the following arguments:
=dlookup("[name]", "MyQuery", "table1.name like '*" & [SearchInput] & "*'")
Here, [SearchInput] refers to the name of the control on your form containing the search criteria.

Run one MS Access SQL script on a particular Table chosen by user

I have a MS Access 2016 database (*.accdb) with 20+ Tables. Fields in each of them vary slightly from Table to Table. I've no VBA experience, so I'm sticking only to the SQL query below (redacted).
SQL script
myvar below is the parameter I'd like to be prompted when the script is run so that I enter the Table I want the changes applied to.
PARAMETERS
[myvar] TableID;
UPDATE
[myvar]
INNER JOIN
Excel_Data ON [myvar].[Part Number] = Excel_Data.[Part Number]
SET
[myvar].[Value] = '?',
[myvar].Description = Excel_Data.Description,
[myvar].[Ref] = '?'
.
.
.
WHERE
[myvar].Description Is Null;
Output
Error message:
Too few parameters. Expected 0.
What I need
I prefer a solution for above in a SQL script form as above, not involving VBA, preferably. I'd like to enter the Table name when prompted so the script knows which table to UPDATE. FYI: The PARAMETERS work when it is not a Table as I've shown in my script above.
Help/advise is highly appreciated.
EDIT 1
Since it seems not possible to use parameters as Table names, could you suggest a VBA solution? A sample code, perhaps?
As said in the comments, you can't really solve this without VBA.
You can store your SQL query in a string, and use a placeholder to indicate the tablename. Then get the tablename using an inputbox and replace the placeholder with the tablename.
Dim sqlString As String
sqlString = "UPDATE [%Placeholder%] " & vbCrLf & _
"INNER JOIN Excel_Data ON [%Placeholder%].[Part Number] = Excel_Data.[Part Number] " & vbCrLf & _
"SET [%Placeholder%].[Value] = '?', " & vbCrLf & _
...
"WHERE [%Placeholder%].Description Is Null;"
sqlString = Replace(sqlString, "%PlaceHolder%", InputBox("Enter a tablename"))
CurrentDb.Execute sqlString
In a more mature solution, I'd create a form with a combobox containing all available table names, and add a function to sanitize tablenames (replace "]" with "]]")

Passing value to textbox with sql query via VBA code - MS ACCESS

I am currently working in MS ACCESS. I have 2 forms one called MyForm and the second one name Transit. I have one table called SimulationTable which have 4 fields Fiscal_Year, Scenario_Number, Description and Operating_Unit. I would like with VBA code when clicking in a button to display result of SQL query in the textbox called TXTBOX which is in Transit form. I have tried many times but it doesn't work.
Any idea of how to fix ? Thank you.
Here is the SQL query:
SELECT SimulationTable.Description FROM SimulationTable WHERE Fiscal_Year=Forms!MainForm!OperatingFY AND Operating_Unit = Forms!MainForm!Text3 AND Scenario_Number = Forms!MainForm!Selected_scenario
Could just use DLookup():
Me.TXTBOX = DLookup("[Description]", "SimulationTable", "Fiscal_Year=" & Forms!MainForm!OperatingFY & _
" AND Operating_Unit = " & Forms!MainForm!Text3 & _
" AND Scenario_Number = " & Forms!MainForm!Selected_scenario)
Are these criteria fields all number type? Is TXTBOX bound to a field? If not, could even just put the DLookup() expression in ControlSource property.
It works. I have added apostrophes around the "Operating_Unit" which is a field in text format. Thanks very much June7.
Me.TXTBOX = DLookup("[Description]", "SimulationTable", "Fiscal_Year=" & Forms!MainForm!OperatingFY & " AND Operating_Unit ='" & Forms!MainForm!Text3 & "' AND Scenario_Number =" & Forms!MainForm!Selected_scenario)

parameterized query not working in VB

statement = "SELECT OrderID, (SELECT VendName FROM Vendors WHERE Vendors.VendorID = Orders.VendorID) " &
",OrderDt, RcvdDt, OrderTotal " &
"FROM Orders " &
"WHERE VendName=? " &
"ORDER BY OrderDt DESC"
Dim cmd As New OleDbCommand(statement, connection)
cmd.Parameters.AddWithValue("VendName", txtVendorFilter.Text)
Dim reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.Default)
I was trying to do this before by simply concatenating the textbox value right into the SQL and I was getting a "No values given for required parameters", and read that that I should use parameterized queries instead. So I tried this and it doesn't give me any errors, but the reader never has anything in it. I've never used parameterized queries before, so I'm a bit lost as to why this isn't working.
Edit:
I've changed the above code to account for OLEDB from what I briefly read on how it should work, and it's giving me the "no values given for required parameters" again.
One problem is here:
"WHERE VendName='#x' " &
Drop the ' marks - the parameterization will take care of this for you:
"WHERE VendName= #x " &
Using the ' in the query means that '#x' is treated as a string type, not a parameter name.
Additionally, since you are using OleDb, names parameters are not supported. You need to use ? to signify a parameter in the query:
"WHERE VendName= ? " &

ms-access: update record where some field = textbox value

can someone please help me with the following query
i need to update a datasheet (table) in access through a form:
i will have something like this
SQLtext = "update table1 set column1="sometext" where column2=textbox1.value"
DoCmd.RunSQL SQLtext
is this possible to do?
i have a textbox on a form and when i click a button on that form i want to update data in the datasheet where one of the columns is equal to the value property of a textbox
thank you!
This is what you need (note the subtle change)
SQLtext = "update table1 set column1='sometext' where column2='" & textbox1.value & "'"
DoCmd.RunSQL SQLtext
Note: For production code you will want to escape out any single quotes in the textbox1.value string using the string replace function, otherwise you will get a SQL error whenever a user types a single quote in that field.