I have created a form to search in person table.
But when I input text for searching and click to the 'Search' Button, it says Enter Parameter Value and so I input my text again and click OK, then it will work.
Here is the code that makes this problem(another_table created from lookup):
Task = "SELECT * FROM person WHERE (item1=(SELECT code_item2 FROM
another_table WHERE name_item2=" & Me.txtSearch.Value & "))"
Me.RecordSource = Task
If Me.txtSearch.Value is numeric
SELECT * FROM person WHERE item1 ΙΝ (SELECT code_item2 FROM
another_table WHERE name_item2=" & Me.txtSearch.Value & ")"
if is alphanumeric then
SELECT * FROM person WHERE item1 ΙΝ (SELECT code_item2 FROM
another_table WHERE name_item2='" & Me.txtSearch.Value & "')"
Consider saving a permanent SQL in form's recordsource, then simply call .Requery with search parameter change. Doing so, you reference search text with absolute Forms reference and with NZ, you can set up a default value if search is empty like on form open. No concatenation or quote punctuation needed.
SQL (save as stored query to be used in form recordsource)
SELECT * FROM person
WHERE item1 IN
(SELECT code_item2 FROM another_table
WHERE name_item2 = NZ(Form!myFormName!txtSearch, 'Default Value'))
VBA
Me.Form.Requery
Related
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.
Essentially I would like to update a subforms column values with a name found in a combo box.
A table called "tbl_jobs" is the source behind the subform, the column I am trying to update is called "Person_Name".
The combo box is called "PersonCombo" .
I am working on creating a query called "updateRecord" using the Access query designer that is executed by the button "updateButton"
The following is how the query will be executed:
DoCmd.OpenQuery "updateRecord"
The content of the query is what I am having trouble with:
UPDATE tbl_jobs SET Person_Name = '" & PersonCombo & "' WHERE [Select] = True
Instead of filling the column data with the values from the chosen name in "PersonCombo" like Jamie, Mickey, Haley, etc. (values from PersonCombo) it just says " & PersonCombo & "
What is wrong with my syntax?
If this is a saved query, it doesn't know about the current form.
You need to use the full path to the control, but no concatenation, e.g.
UPDATE tbl_jobs SET Person_Name = Forms!myForm!PersonCombo WHERE [Select] = True
If the combo box is on a subform, refer to:
Forms: Refer to Form and Subform properties and controls
e.g. Forms!Mainform!Subform1.Form!ControlName
I do not believe you can pass a variable to a saved query. Instead build the query in code and run it:
dim SQL as string
PersonCombo = "thePerson"
SQL = "UPDATE tbl_jobs SET Person_Name = '" & PersonCombo & "' WHERE [Select] = True"
DoCmd.RunSQL SQL
I want the below Delete statement to be executed when the user click the delete button.
CurrentDb.Execute "Delete from [TableName] where ColumnName is not null"
But I want the user to input the tablename and the columnName. Can you please help
This will grab parameter from form control:
SELECT *
FROM Customers
WHERE Country = _
[Forms]![frmSelectCountry]![txtCountry]
This will prompt user "Enter County":
SELECT *
FROM Customers
WHERE Country = _
[Enter Country]
To prompt a user for the table name and prompt user to "Enter County":
tblName= InputBox("Please enter a name for the source table", "?")
mysql = "DELETE * FROM " & tblName _
& "WHERE (((" & tblName & ".[City])=[Enter City]));"
You could build a form (named "frmSelectTable") with a combo box control (named "cboTable") on it listing the tables to choose from and have the code retrieve the combo box controls value.
tblName = [Forms]![frmSelectTable]![cboTable]
Incorporate with your work:
tblName = InputBox("Please enter a name for the source table", "?")
mysql = "DELETE * FROM " & tblName _
& "WHERE ColumnName Is Not Null;"
CurrentDb.Execute mysql
Here is a tutorial that will walk you through making the form that has a combo box listing all the tables in your database:
How to Get all Table Names into Combo box
I have a word document macro enabled, I have a drop down button within the document which takes information from one form field and places it into another form field.
Select Case ActiveDocument.FormFields("Dropdown1").Result
Case "Order"
ActiveDocument.FormFields("ord1").Result ActiveDocument.FormFields("p1").Result
End Select
How can I adjust the code so that, instead of replacing everything in field "ord1" it would join the existing text within the field?
Try something simple like this:
ActiveDocument.FormFields("ord1").Result = _
ActiveDocument.FormFields("ord1").Result & " " & _
ActiveDocument.FormFields("p1").Result
Good day
ms-access 2007
I have 2 sub-Datasheet's on my form,
One display a list of items.
the other is blank.
Is there a way by placing a button on the form "Copy/Add", it copies the highlighted field to a field in the blank data-sheet.
both field's are of the same type.
Thank you
Probably the easiest thing is to run an append query. Let us say the button is on the main form, you would need a little code on the lines of:
Set db = CurrentDB
strSQL = "INSERT INTO TableB (ID, SomeField) " _
& "SELECT ID, SomeField FROM TableA WHERE ID = " _
& Me.[NameOfSubformControl].Form.[NumericIDField]
db.Execute strSQL, dbFailOnError
You will need quotes if the ID field is not numeric.