Set statement error - ms-access

I am receiving the 3075 error (invalid operator) from the script below. The VBA error is marking the set statement in yellow. I don't understand why. What is lacking?
strSql = "SELECT FA_AVG.RadNr, (Left([Text],122)) AS LetaEfter1, Mid([Text],130,18) AS LetaEfter2, Right(Left([Text],184),36) AS LetaEfter3, FA_AVG.PerNr, FA_AVG.Fil, FA_AVG.GR1 " & _
"FROM FA_AVG " & _
"WHERE (((FA_AVG.RadNr)=20) AND ((Left([Text],122))='" & strTemp1 & "') AND ((Right(Left([Text],184),36))='" & strTemp3 & "') AND (Trim(FA_AVG.PerNr)='" & strPer & "'))"
Set rstAddData = CurrentDb.OpenRecordset(strSql)

Run this and study the output, and it will probably be quite clear to you why it won't run:
strSql = "SELECT FA_AVG.RadNr, (Left([Text],122)) AS LetaEfter1, Mid([Text],130,18) AS LetaEfter2, Right(Left([Text],184),36) AS LetaEfter3, FA_AVG.PerNr, FA_AVG.Fil, FA_AVG.GR1 " & _
"FROM FA_AVG " & _
"WHERE (((FA_AVG.RadNr)=20) AND ((Left([Text],122))='" & strTemp1 & "') AND ((Right(Left([Text],184),36))='" & strTemp3 & "') AND (Trim(FA_AVG.PerNr)='" & strPer & "'))"
Debug.Print strSQL

Related

SQL Inserting in to an external table

I am looking to insert data into a different table that users are using.
I think I am on the right track to piecing this together but looks like ill need a hand
My Code:
CurrentDb.Execute "INSERT INTO [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBname & "].[" & tblengagement & "]" _
& "(CDP,Open_Date, Open_Time) VALUES " _
& "'" & Environ("UserName") & "', Date(), Time());"
Error
Syntax error in INSERT INTO statement
What I am trying to achieve
When a user opens the database I want to send the username, date, time to a database stored in a different location
UPDATE
I have broken the code down and got the below working, just need to work out how to get date, time in there:
CurrentDb.Execute " INSERT INTO [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBname & "].[" & tblengagement & "] " _
& "(CDP) VALUES " _
& "('" & Environ("UserName") & "');"
date() and time() are functions and should not be within the quotation string. Dates and Times need to be enclosed by # signs.
CurrentDb.Execute "INSERT INTO [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBname & "].[" & tblengagement & "]" _
& "(CDP,Open_Date, Open_Time) VALUES " _
& "'" & Environ("UserName") & "',#" & Date() & "#,#" & Time() & "#);"

Access VBA code : How to define multiple WHERE statements ? UPDATE - SET - WHERE statement

I got following code to work, except for the last line.
So I want to update a table called loggingX , this is working with the code below, except for I want the WHERE clause to not only check for 1 field (this is working) , but I want the WHERE to also check for field WHid to be a fixed value.
I would like to know how I can add multiple parts to my WHERE statement here. UPDATE should only be done if these 2 conditions below are met. I only have trouble and want to know how to put both conditions in the WHERE clause.
stdid=" & Me.txtID.Tag
WHid=" & Me.txtWHid
Complete update statement for current DB (AND is not working):
CurrentDb.Execute "UPDATE loggingX " & _
" SET stdid=" & Me.txtID & _
", stdname='" & Me.txtName & "'" & _
", gender='" & Me.cboGender & "'" & _
", phone='" & Me.txtPhone & "'" & _
", address='" & Me.txtAddress & "'" & _
", WHid='" & Me.txtWHid & "'" & _
" WHERE stdid=" & Me.txtID.Tag
" AND WHid=" & Me.txtWHid
You're missing an ampersand and underscore on the second to last line:
CurrentDb.Execute "UPDATE loggingX " & _
" SET stdid=" & Me.txtID & _
", stdname='" & Me.txtName & "'" & _
", gender='" & Me.cboGender & "'" & _
", phone='" & Me.txtPhone & "'" & _
", address='" & Me.txtAddress & "'" & _
", WHid='" & Me.txtWHid & "'" & _
" WHERE stdid=" & Me.txtID.Tag & _
" AND WHid=" & Me.txtWHid

Identify a data entry without changing the ID

I am trying to create an update form. It is supposed to take values from the text boxes on the form and update that registry using the ID's autonumber as an identifier. It says I cannot edit the ID even though I don't think I am.
Private Sub edit_Click()
'Will edit the currently selected record
CurrentDb.Execute "UPDATE DataInput " & _
" SET ID=" & Me.txtID & _
", [Date]='" & Me.Date & "'" & _
", [Time Up]='" & Me.txttimeup & "'" & _
", [Notes]='" & Me.CboNotes & "'" & _
", [Time Down]='" & Me.txtTimeDown & "'" & _
" WHERE ID=" & Me.txtID.Tag
Me.txtID.Tag = ""
'refresh data on form
DataInput_subform.Form.Requery
'Disable Update Button
Me.edit.Enabled = False
'Enable Edit Button
Me.cmdEdit.Enabled = True
'Clear texts
cmdClear_Click
Try formatting your code in a more readable way and add a debug like
Dim sSql As String
sSql = "UPDATE DataInput SET [Date]=#" & Format(Me.Date,"mm/dd/yyyy") & "#, "
sSql = sSql & "[Time Up] = '" & Me.txttimeup & "', [Notes] ='" & Me.CboNotes & "', "
sSql = sSql & "[Time Down] = '" & Me.txtTimeDown & "' "
sSql = sSql & "WHERE ID = " & Me.txtID & " ;"
Debug.Print sSql
CurrentDb.Execute sSql

3011 error code access recordset vba

This is the code that I have worked over many many times, fix one error code3061, then another, now this. ANY IDEAS WHY THIS ERROR IS HAPPENING, all objects spelled correctly?
Dim strSQL As String
Dim strForms As String
strForms = [Forms]![frmEnterResRecordset]![txtPhone]
MsgBox strForms
strSQL = "SELECT tblCustomer.IDCustomer, tblCustomer.PHONE, tblCustomer.LASTNAME, " & _
"tblCustomer.FIRSTNAME, tblCustomer.NAME, tblCustomer.EMAIL " & _
"FROM tblCustomer " & _
"WHERE (((tblCustomer.PHONE) Like " & "*'" & strForms & "'*" & "));"
Check your syntax near the Like operator. The asterisks are outside the single quotes. Try replacing it as follows:
strSQL = "SELECT tblCustomer.IDCustomer, tblCustomer.PHONE, tblCustomer.LASTNAME, " & _
"tblCustomer.FIRSTNAME, tblCustomer.NAME, tblCustomer.EMAIL " & _
"FROM tblCustomer " & _
"WHERE (((tblCustomer.PHONE) Like " & "'*" & strForms & "*'" & "));"
Something I like to do when building dynamic SQL statements like this is to print it to the debug window with the following statement:
debug.print strSQL
It's easier to spot statements like:
Like *'my_entered_value'*

Access Too Few Parameters Error

I have this code I have been working on to add data to a table. And there is a portion of code that is being highlighted with the error message RUN TIME ERROR 3061: TOO FEW PARAMETERS. EXPECTED 1
And I cannot figure out what the issue is and what I am missing. The following is the portion of code being highlighted.
CurrentDb.Execute "INSERT INTO KWTable(KW, Source, Code) " & _
" VALUES(" & Me.text_key & ",'" & Me.txt_code & "','" & _
Me.combo_source & "')"
Try
dim ssql as string
ssql = "INSERT INTO KWTable(KW, Source, Code) " & _
" VALUES(" & Me.text_key & ",'" & Me.txt_code & "','" & _
Me.combo_source & "')"
debug.print ssql
CurrentDb.Execute ssql