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
Related
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
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 have an access 2010 database that I'm using to track the installation of equipment.
One table (tblSerial_Numbers) contains all equipment part numbers and serial numbers with a yes/no field type to indicate whether it's been installed or not
The second table (tblInstallation) tracks installation data i.e. date, location, part#, serial# etc...
I have a form based from tblInstallation for the end user to enter all the info needed to populate tblInstallation.
My problem is i want a checkbox on the same form to update the Installed field in tblSerial_numbers.
So for the checkbox i have an After Update event sub with the following code
Private Sub chkInstalled_AfterUpdate()
CurrentDb.Execute "UPDATE tblSerial_Numbers " & _
"SET Installed = " & Nz(Me.chkInstalled) & _
" WHERE Serial_Number = " & Nz(Me.cboSerNum)
End Sub
This is supposed to use the serial number specified in the form combo box (cboSerNum) to set "Installed" in tblSerial_Numbers to whatever the checkbox on the form is but it's not working. There are no errors either.
Any Help is appreciated.
UPDATE:
Syntax error has been resolved, updating Installed field in tblSerial_Numbers still not working.
I've been able to solve this.
The heart of the problem was the SerNum field in tblInstallation. It was a lookup from tblSerial_numbers. The value in cboSerNum was the primary key (autonumber) for the record in tblSerial_Numbers, not the actual serial number of the device.
A Simple modification of the SQL query fixed the updating of the installation field in tblSerial_Numbers.
Private Sub chkInstalled_AfterUpdate()
CurrentDb.Execute "UPDATE tblSerial_Numbers " & _
"SET Installed = " & Nz(Me.chkInstalled) & _
" WHERE ID = " & Nz(Me.cboSerNum)
End Sub
This is my final code (just made it look more conventional)
Private Sub chkInstalled_AfterUpdate()
CurrentDb.Execute "UPDATE tblSerial_Numbers " & _
"SET tblSerial_Numbers.[Installed] = " & Me.chkInstalled & _
" WHERE tblSerial_Numbers.[ID] = " & Me.cboSerNum & ";"
End Sub
Trying to set up a query where one of the fields is selected by the value in a combobox. Can't figure out the right syntax. I'm in the SQL view of the query builder, using the following:
SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, Deviations.Active, Deviations.[Forms]![DeviationSelectionForm]![cmbAircraft]
FROM Deviations
WHERE Deviations.[Forms]![DeviationSelectionForm]![cmbAircraft]=True
ORDER BY Deviations.Deviation DESC
The combo box selects the tail number of the aircraft which is a checkbox in the table. There are multiple aircraft tail numbers as checkbox fields. So if there were no combobox and I wanted the query to use as example aircraft 416, the query would be:
SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, Deviations.Active, Deviations.[416]
FROM Deviations
WHERE Deviations.[416]=True
ORDER BY Deviations.Deviation DESC
When I test this query it works as I need it to.
So the question is how to I create the query with the combobox in a form identifying the field name of a table?
Build the Query SQL viacode in the combobox afterUpdate event.
Dim strSql as String
strSql = "SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, " _
& "Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, " _
& "Deviations.Active, Deviations." & Me!cmbAircraft _
& " FROM Deviations " _
& "WHERE Deviations." & Me!cmbAircraft & " =True " _
& "ORDER BY Deviations.Deviation DESC"
CurrentDb.QueryDefs(<yourqueryname>).SQL = strSql
That said, it seems like you have built a spreadsheet instead of a database.
Your tables are not normalized. Aircraft should be records, not columns. I suggest you read up on database design and normalization.
So right now I have a listbox that gets data from an access database and prints it out like below:
When the user clicks on "Just Lose It" for example, and then clicks "Remove Song" button, how can I delete that entry from the database and then refresh the list box with the updated table.
I was thinking of creating an SQL DELETE Statement and then use a ADODB.Recordset to run that statement but I'm not sure how to tell the user clicked that entry.
Any ideas?
Use something like the following... add unique key if in hidden column
sqlStr = "DELETE FROM music WHERE [Title] = '" & music_lb.column(0) & "' AND [Artist] = '" & music_lb.column(1) & "' AND [Album] = '" & music_lb.column(2) & "';"