I'm creating a Voting System. so it's like this every time A Button1 is pressed + 1 or it will increase the vote in the Access database. I can't find anything in Google.
trx = "update [Table1] SET [Vote] = Vote + 1, (WHERE ID = 1)"
Don't include a comma before the WHERE clause. Also you don't need to put the WHERE clause inside parentheses.
Test this as a new query in the Access query designer.
update [Table1] SET [Vote] = Vote + 1 WHERE ID = 1
Fine tune as needed. And once you have it working in the query designer, adapt your VBA code to use that working statement.
Remove comma before Where and brackets around where clause
trx = "update [Table1] SET [Vote] = Vote + 1 WHERE ID = 1"
Related
I have several tables with the same data structure (they're filled with a bunch of stuff, in separate .accdb files to account for the 2GB limit) and need to retrieve info from one of them based on a field in a form.
Upon researching I came up with the following, but it won't seem to work.
SELECT MyNumber, MyName, MyPage, MyDrawing
FROM Switch([Forms]![View_Info]![Contract] = "Contract1", "tblContract1", [Forms]![View_Info]![Contract] = "Contract2", "tblContract2")
WHERE (MyNumber = [Forms]![View_Info]![MyNumber])
Syntax error in FROM clause.
In this example I only used 4 fields and 2 tables but in fact there are around 9 tables and 20 fields in each that I wish to retrieve.
Can someone shed some light on this? I have a really hard time with SQL, so I apologize if this is quite basic.
Thanks in advance, Rafael.
You cannot return the table name from a function in the SQL FROM clause. If your table is determined dynamically, then you must build the SQL command string dynamically.
Dim tableName As String, sql As String
tableName = Switch(...)
sql = "SELECT ... FROM [" & tableName & "] WHERE ..."
As #forpas explains in his answer, you can use a UNION query, but this will always query all the tables. Since the filter is not based on a table column, the filtering will occur on the client side, i.e. in your application.
Try this UNION:
SELECT MyNumber, MyName, MyPage, MyDrawing
FROM tblContract1
WHERE (MyNumber = [Forms]![View_Info]![MyNumber]) AND [Forms]![View_Info]![Contract] = "Contract1"
UNION
SELECT MyNumber, MyName, MyPage, MyDrawing
FROM tblContract2
WHERE (MyNumber = [Forms]![View_Info]![MyNumber]) AND [Forms]![View_Info]![Contract] = "Contract2"
Each query of the UNION contains in the WHERE clause the condition:
[Forms]![View_Info]![Contract] = "Contract?"
Is there a specified behavior for updating the same column 2+ times in the same UPDATE query, as follows?
UPDATE tbl SET a = 5, b = 'something', a = 6 WHERE c = 'whatever';
Is there a standardized behavior for this, or might it vary between flavors of SQL (e.g. it is "undefined behavior")? A cursory test with sqlite seems to indicate they are executed left-to-right, so the last column value will be the resulting one, but that doesn't imply that will always be the case.
Edit: The reason I'm trying to do this is I'm testing some SQL injection for a class project. One of the fields in an UPDATE is unsafely injected, and I'm trying to use it to overwrite previously SET fields from the same query.
This isn't exactly the answer you're looking for but assuming that the text "something" is a field you are passing in and it isn't parameterized or escaped you may be able to do this. This all depends on how the query is being built and what database it is being run against.
UPDATE tbl SET a = 5, b = 'something'; UPDATE tbl set a = 6;--' WHERE c = 'whatever';
by entering the following in the user input
something'; UPDATE tbl set a = 6;--
This assumes that the query is built dynamically something like this
var query = "UPDATE tbl set a = 5, b = '" + userInput + "' WHERE c = 'whatever'";
Here is a relevant question: How does the SQL injection from the "Bobby Tables" XKCD comic work?
I am having an issue with a query on Microsoft Access 2007. I've developed query which runs correctly when I run it from the query editor of Access. My problem is that I have put this very same query inside a string literal in a VBA code of a Report, on the event On Open' handler. My purpose is to assign this query to the property RecordSource of that Report, allowing the Report to be created dynamically with whatever query I want.
The issue arises when I actually open and run the Report. When the handler for the event Open finishes its execution I get the error: you tried to execute a query that does not include the specified expression "ID" as part of an aggregate function.
Here is the query (inside the VBA code for the open event handler).
Private Sub Report_Open(Cancel As Integer)
Dim Query As String
Dim WhereClause As String
WhereClause = [Forms]![Main Form]!
[WindowPrintOptions]![CustomizedReport]![txtResponsibility]
Query = "SELECT AllInfo.*
FROM (SELECT i.ID,
i.[Number],
i.Responsability.Value AS Responsability
FROM tbl_Projects AS p RIGHT JOIN
((tb_Items AS i LEFT JOIN tb_F_projects AS fp
ON i.ProjectID = fp.Id) LEFT JOIN
tb_items_comments AS ic ON ic.ItemID = i.ID)
ON i.ProjectID = p.Id WHERE ( (i.Status = 'Status 1')
AND (i.DateOpened >= #9/9/2014#) AND
(i.DateOpened <= #9/11/2014#) AND
(i.DueDate >= #9/12/2014#) AND (i.DueDate <= #9/12/2014#)
AND (i.Category = 'C1') AND (i.Deliverables LIKE '*'
& 'dates' & '*') AND (i.Responsability.Value = 'Some name')
) ) AS AllInfo"
Me.RecordSource = Query
End Sub
Note that I don't use 'WhereClause' yet. It will be inserted afterwards into the query. I just removed it for the purpose of testing the query alone, and check if it was ok without any change and found it actually wasn't. So, just remembering: this very same query does work when I run it from MS Access query builder, but not here, dynamically. What may be the problem?
I am attempting to update one column of a table based on data present in other records of the same table. All records either have the same date in the "CurrentDate" field or are null. I want to change those with null values to be the same as the rest of the fields.
Here is my code, but I am getting a syntax error:
Public Sub RiskVisual()
Dim db As DAO.Database
Set db = CurrentDb
---
DoCmd.RunSQL "UPDATE Hold3 SET CurrentDate = (SELECT CurrentDate FROM Hold3 LIMIT 1) WHERE CurrentDate IS NULL;"
End Sub
Thanks in advance for your help.
In MS Access the "TOP 1" works better than "LIMIT 1". You will also want to specify when seeking for the top 1 that the top 1 that is not null. Try something like this:
UPDATE Hold3 SET Hold3.CurrentDate = (SELECT TOP 1 Hold3.CurrentDate FROM Hold3 WHERE (((Hold3.CurrentDate) Is Not Null))) WHERE (((Hold3.CurrentDate) Is Null));
I have a query within access that selects all the contacts for a particular company based on the CompanyID Field. And on my form i have a selection of labels of which will be populated with the query result. However i'm a little stuck on how i should populate the labels, as there will be more than one contact returned from the query..
The Query
ConactData = "SELECT * FROM Contacts WHERE CompanyID = " & CompanyValue & ";"
Obviously i can do
Set rst = CurrentDb.OpenRecordset(ContactData, dbOpenSnapshot)
Me.lblTitle.Caption = rst!Title
Me.lblFirstName.Caption = rst!FirstName
Me.lblLastName.Caption = rst!LastName
Me.lblEmail.Caption = rst!Email
Me.lblMobileNumber.Caption = rst!MobileNumber
But this will just select the first result from the table, how then, can i move onto the next result? If i'm right in thinking the MoveNext method will simply go to the next record in the table, not the query result?
Why use labels? Just build the form bound to the table.
Then in your code go:
Me.RecordSource = "SELECT * FROM Contacts WHERE CompanyID = " & CompanyValue & ";"
This means you don’t need a bunch code to fill out the form, it is done for you. And your example would not allow editing of data either. To write a bunch of code when all the display of data is automatic is a waste of developer time and resources.
In fact, why not leave the form bound to the table, and then use a where clause to open the form
eg:
docmd.openform "frmContacts",,,"CompanyID = " & CompanyValue
So it not clear why you writing all that code and doing handstands - it simply not required.