Expected End of Statement Highlighting WHERE - sql

I am attempting to delete records from a table based on a listbox selection in my form. The table is DailyLog & UserLoggedOn where username is a variable based on user. The listbox is Me.lstRespLBPers. The field is the table is RespLBPerson. I keep getting "Expected end of Statement" error message with the word WHERE highlighted. What am I doing wrong. Code is listed below:
sql = "DELETE * FROM DailyLog_" & UserLoggedOn WHERE [RespLBPerson] <> Me.LstRespLBPers
I tried the code and expected the records to be deleted from the table but got the error message.

Related

Ensure Unique Records in Form (MS Access)

Using a Form in MS Access, I need to ensure only unique records are entered into the table.
It's unfortunately in a situation where I can't enforce unique records via the table's primary keys.
I have the following code in the BeforeUpdate for the Form, but it is not working. Any help would be greatly appreciated!
Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "[Role_Details]", "[Role] = " & Me.[ComboRole] & " AND [Session] = " & Me.[ComboSession]) > 0 Then
MsgBox "Duplicate!"
Cancel = True
Me.[ComboSession].SetFocus
Exit Sub
End If
End Sub
Note: The table name is "Role_Details". Field names are "Role" and "Session". With "ComboRole" and "ComboSession" being the Form Field Labels.
Any thoughts on where I've gone wrong here?
Updates##
When I open the DataSheet Form, it presents a popup box saying "Enter Parameter Value" and "frm_Role_Details.Session". I'm not sure why this is, but I can enter past it and open the Form.
Then, them i'm entering a record an error pops up saying "Run-time error '2465': Can't find the field '|1' referred to in your expression. Both Fields are Text Strings. I'm at a loss!
When concatenating in VBA, text fields require apostrophe delimiters for inputs.
If DCount("*", "[Role_Details]", "[Role] = '" & Me.[ComboRole] & "' AND [Session] = '" & Me.[ComboSession] & "'")> 0 Then
Date/Time fields use # delimiter.
Number fields do not use any delimiter.

Access VBA Run-time error 3075 syntax error (missing operator) open on double click from listbox

My goal is to open a specific record from a list box to populate a different form with that specific data that was double clicked on. My column name is Claim ID 15 on the list box and in the master table that contains all data. This column contains values such as C123456789. On the Master table, Claim ID 15's data type is short text. When I double click on the row in the list box it gives me the following error: Run-time error '3075': syntax error (missing operator)in query expression '[Claim_ID_15=C123456789'.
Private Sub SearchList_DblClick(Cancel As Integer)
DoCmd.OpenForm "profileForm", , , "Claim ID 15=" & SearchList
End Sub
I have a rudimentary understanding of visual basic, thank you in advance.
Advise not to use spaces nor punctuation/special characters in naming convention. If you do, must enclose object names in [ ] characters. Also, parameters for text fields must have apostrophe delimiters. Use # for date/time field, nothing for number field.
DoCmd.OpenForm "profileForm", , , "[Claim ID 15]='" & SearchList & "'"
Thank you for the help and the information! I made the changes and it worked
Private Sub SearchList_DblClick(Cancel As Integer)
DoCmd.OpenForm "profileForm", , , "[Claim ID 15]='" & SearchList & "'"
End Sub

SQLite3 code to Dynamically insert Column Value of row data to delete

i use python+SQLite to code. I have been trying to create a block where a user can enter a column value to delete corresponding row data in an SQLite3 database.
Practically, the code snippet below works, but i will have to enter all email addresses manually. I want to dynamically insert the email for this SQLite Line of code so as to delete corresponding records
DELETE FROM Records WHERE email = 'xyz#xyz.com'
I have tried the following;
entered_email = input('enter email: ')
string = f" 'DELETE FROM Records WHERE email = '{entered_email}'"
c.execute(string)
It does not work. I get the error line below
sqlite3.OperationalError: near "'DELETE FROM Records WHERE email = '": syntax error
I will be very glad if i can get an explanation or description on how best to go about this. thanks
Don't concatenate user input in the query string - this opnns up your code to SQL injection.
Instead, use a parameterized query:
entered_email = input('enter email: ')
c.execute('DELETE FROM Records WHERE email = ?', (entered_email, ))

Run-time error '3075' VBA: Check if file already exists in table

I gave a piece of code where I am trying to check if a file has already been uploaded to a table. When files are uploaded the name of the file uploaded is put into a column called 'Name_of_report'. I am trying to run the below code and check if the file already exists in 'Table1' which holds all data.
However when I run the below code I get the error: Run-time error '3075': Syntax error (missing operator) in query expression 'Name_of_report' = File1. xlsx (which is the value assigned to the TableName variable)
I know it's a simple fix but I'm very new to VBA and have tried various changes and can't seen to get it working.
An help would be appreciated.
Sub Check_TableExists(TableName As Variant)
If CurrentDb.OpenRecordset("SELECT count(Name_of_Report) FROM Table1 WHERE Name_of_Report =" & TableName & ";").Fields(0) > 0 Then
MsgBox ("Data already exists in table")
End If
Parameters for text type field need apostrophe delimiters.
If CurrentDb.OpenRecordset("SELECT count(Name_of_Report) FROM Table1 WHERE Name_of_Report ='" & TableName & "';").Fields(0) > 0 Then
However, domain aggregate function could accomplish instead of opening recordset.
If DCount("*", "Table1", "Name_of_Report='" & TableName & "'") > 0 Then

Run time error 3021- no current record

I want to link the result of a query to a Textbox but I get this error: here is my code:
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT XValue, YValue,Wert FROM tb_DCM_Daten WHERE (FzgID=" & Forms!frm_fahrzeug!ID & " AND Name='" & List2.Value & "')")
Text10.Text = rst!XValue //error in this line
It should be return c.a 20 record
Why do I get this error and how can I solve it?
One possible reason for the error is that Name is a reserved word in Access, so you should use
... & " AND [Name]='" & ...
You could also test for rst.EOF before trying to use rst!XValue. That is, to verify whether or not your query is returning at least one row you can add the code
If rst.EOF Then
MsgBox "The Recordset is empty."
End If
immediately after the .OpenRecordset call. If the Recordset is empty, then you'll need to verify your SQL statement as described by #GregHNZ in his comment above.
Usually, I would do this. Create a new query in Access , switch to SQL View , Paste my code there and go to Design >> Run.
SELECT XValue, YValue,Wert FROM [tb_DCM_Daten] WHERE [FzgID]=12 AND [Name]='ABC';
if your query syntax is correct you should see the result otherwise error mssg will tell where you are wrong. I used to debug a much more complicated query than yours and this is the way that I've done.
If there is still error, maybe you should try
Dim sql as String
sql = "SELECT...."
Set rst = CurrentDb.OpenRecordset(sql)
Another possible reason might be your table name. I just wonder what is your table name exactly ? if your table contains white space you should make it like this [DCM Daten].
One more thing I like to add that may cause this, is your returning a sets of resultset that has "Reserved word" fields, for example:
Your "Customers" table has field name like the following:
Custnum | Date | Custname
we know that Date field is a reserved word for most database
so when you get the records using
SELECT * FROM Customers
this will possible return "No Current Record", so instead selecting all fields for that table, just minimize your field selection like this:
SELECT custnum, custname FROM Customers
After trying the solutions above to no avail, I found another solution: Yes/No fields in Access tables cannot be Null (See allenbrowne.com/bug-14)
Although my situation was slightly different in that I only got the "No current record." error when running my query using GROUPBY, my query worked after temporary eliminating the Yes/No field.
However, my Yes/No field surprisingly did not contain any Nulls. But, troubleshooting led me to find an associated error that was indeed populating my query result with Null Yes/No values. Fixing that associated error eliminated the Null Yes/No values in my results, thus eliminating this error.
I got the same error in the following situation:
In my case the recordset returned one record including some fields with Null value.
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT * FROM tbl WHERE (criteria)", dbOpenDynaset)
Textbox1 = rst!field1 'error in this line - Non-Null value
Textbox2 = rst!field2 'Null value
Textbox3 = rst!field1 'Null value
Viewing Locals when rst is opened and before asignments, shows the recordset as I expect it to be. The error is thrown when trying to a asign a value from this recordset.
What fixed this, is ensuring that all fields contained non-Null values.
Just posting this for future seekers.