I am new to both MS Access and SQL. Now I am trying to create an inventory database for our company in Ms Access. I try to extract data from the reception form to update the inventory balance. But I met a syntax error message when I executed a SQL update statement. This is weird for me because I used the same statements that successfully running in other tables. The only difference is my former successful update working by direct text replacement and my error occurring update is working in a numeric object.
Please help me to check where I am wrong.
This is my code:
Private Sub Command96_Click()
CurrentDb.Execute "UPDATE tbl_Current_Stock" & _
"SET Stock_Level= Stock_Level + " & Me!txtOrderQty & "" & _
"Where tbl_Current_Stock.Raw_Material= " & Me!cboPurchase.Column(1) & ""
End Sub
Thanks!
You need to add spaces before SET and Where. Otherwise, your command will look something like UPDATE tbl_Current_stockSET Stock_Level= Stock_Level + 3Where.....
Private Sub Command96_Click()
CurrentDb.Execute "UPDATE tbl_Current_Stock" & " " & _
"SET Stock_Level= Stock_Level + " & Me!txtOrderQty & " " & _
"Where tbl_Current_Stock.Raw_Material= " & Me!cboPurchase.Column(1) & ""
End Sub
You might also need to wrap the Raw_Material column in quotes if it is not numeric.
check your sentence correctly. there is no technical error. there are some space missing in you query.
just add white space before "SET" and "where" words.
CurrentDb.Execute "UPDATE tbl_Current_Stock" & _
" SET Stock_Level= Stock_Level + " & Me!txtOrderQty & "" & _
" Where tbl_Current_Stock.Raw_Material= " & Me!cboPurchase.Column(1) & ""
Friend, follow some tips to generate your updade correctly:
Check the spaces after concatenating your query
Be careful not to generate queries with keywords stuck together
UPDATE tableTestSET nome = 'My Name' WHERE active IS NOT NULL (wrong)
UPDATE tableTest SET name = 'My Name' WHERE active IS NOT NULL
Do not forget to use quotation marks when using strings
UPDATE tableTest SET name = My Name WHERE active IS NOT NULL (wrong)
UPDATE tableTest SET name = 'My Name' WHERE active IS NOT NULL
I hope it helps...
Good Luck!
Related
I am very new to SQL and think I have a simple problem but was unable to figure it out from other posts. I have the following code:
INSERT INTO tblShortScores ( TradeNum, FilterNum, Rank, ScoreNum )
SELECT [Forms]![frmOpenTrades]![TradeNum] AS TradeNum, tblFilters.FilterNum, tblFilters.SBBExh AS Rank, tblFilters.SBBExh AS Score
FROM tblFilters
WHERE (((tblFilters.SBBExh) Is Not Null));
but instead of using the literal "SBBExh" in tblFilters.SBBExh, I want to do something like
tblFilters.("S" & [Forms]![frmOpenTrades]![Strategy])
where something like
[Forms]![frmOpenTrades]![Strategy] contains the value "BBExh".
It's in MS Access and I seem unable to find a syntax that works
any help is appreciated
Can't dynamically build field name in query object. Use VBA to construct and execute action SQL, like:
strField = "S" & Me.Strategy
CurrentDb.Execute "INSERT INTO tblShortScores (TradeNum, FilterNum, ScoreNum) " & _
"SELECT " & Me.TradeNum & " AS TradeNum, FilterNum, " & strField & " " & _
"FROM tblFilters WHERE " & strField & " Is Not Null;"
Assumes TradeNum is number type - if it is text, use apostrophe delimiters:
SELECT '" & Me.TradeNum & "' AS .
If SQL injection is a concern review, How do I use parameters in VBA in the different contexts in Microsoft Access?
I have a form that references a linked table (called "Customer Information") to auto-fill information (such as street address, phone number, etc.) about a customer once the user has entered the customer's name into the form (called "Quote Form"). Once the information has been auto-filled, the user still has the opportunity to change those fields. However, if they change those fields such that they no longer match the information on record for that particular customer, a message box asks them if they'd like to update the customer's information. If they click yes, I want a SQL statement to update the information in the "Customer Information" table to match the information on the "Quote Form".
Unfortunately, I am receiving the following error: "Run-time error '3075': Syntax error (missing operator) in query expression 'insert result of "Form.Controls(Box)" -- see code below'.
I am using a public function because I plan to repeat this procedure for several different fields. Here is my code:
Private Sub StreetAddressEntry_LostFocus()
'Check for match with existing customer information and update if necessary
Call UpdateCustomerInfo("StreetAddressEntry", "[Street Address (Billing)]", "[Customer Information]", "[Customer Name]", "CustomerEntry", "street address")
End Sub
Public Sub UpdateCustomerInfo(Box As String, Fld As String, Tbl As String, lookupFld As String, lookupBox As String, topic As String)
'Underlying function for updating customer information
Dim MsgBoxAnswer As Variant
If Form.Controls(Box).Value <> DLookup(Fld, Tbl, lookupFld & " = " & lookupBox & ".Value") Then
'Request Permission to update customer information based on conflicting entries
MsgBoxAnswer = MsgBox("The " & topic & " does not match what is currently listed for this customer. Would you like to update the customer's information to match?", vbYesNo, "Edit Customer Information?")
If MsgBoxAnswer = vbNo Then 'No permission granted to add a new customer to the list.
Exit Sub
Else 'Permission granted to add new customer to the list.
DoCmd.RunSQL "UPDATE " & Tbl & " SET " & Fld & " = " & Form.Controls(Box) & " WHERE " & lookupFld & " = " & Form.Controls(lookupBox)
End If
End If
End Sub
I would appreciate any suggestions or tips on how to improve this code and resolve the error message.
To debug: you can your SQL text into a variable before running and then print using MsgBox to see what is the SQL text generated. That usually helps catching the problem. If still not clear, you can run that SQL inside a Query in Access and see if it works there.
My guess is that you're missing single quotes if the columns you're trying to update are string columns. Try this
mySQL = "UPDATE " & Tbl & " SET " & Fld & " = '" & Form.Controls(Box) & "' WHERE " & lookupFld & " = '" & Form.Controls(lookupBox) & "'"
MsgBox (mySQL)
DoCmd.RunSQL
Also if table name or column names includes non-alphanumeric characters (like space etc.), you need to use brackets around the table/column names but I think this is not the case with yours as I see square bracketed columns in your code.
Again, the best is to print the SQL text generated and run it inside your database client (in this case Access Query tool)
I am a data consultant who migrates data I am sent into our system. I have written code that compares the contents of my table against what has been put into oracle, as an extra test. The tables are a little convoluted due to how they relate to each other. But essentially here is my question:
When I look to match two field values and the field doesnt exist I get a parameter pop up box. I want to only run the code if the field exists.
I have tried many things, wrapping an if statement around it but I always get the parameter box, can anyone help there must be an easier way to do this!
If Not DoCmd.OpenQuery("SELECT TOP 1" & MatchValues!FieldName & " FROM " &
MatchValues!ORACLE_TABLE_NAME) Then
MsgBox "moomins"
' strSQL = "INSERT INTO 002_TableValueErrors(ORACLE_TABLE_NAME,TRANSFORM_TABLE_NAME,FIELD_NAME,ORACLE_FIELD_VALUE,TRANSFORM_FIELD_VALUE) "
' strSQL = strSQL & " VALUES (" & MatchValues!ORACLE_TABLE_NAME & "," & MatchValues!TRANSFORM_TABLE_NAME & "," & MatchValues!FieldName & ",'ORACLE: NOT FOUND','ORACLE: NOT FOUND')"
End If
If you deal with Oracle: Have you tried to check if the field exists by querying ALL_TAB_COLUMNS in Oracle?
"Select count(*) from ALL_TAB_COLUMNS where table_name = " & MatchValues!ORACLE_TABLE_NAME & " and COLUMN_NAME = " & MatchValues!FieldName
(Untestet cause currently I have no Oracle Instance available)
I am trying to update a table in Access using SQL in a VBScript. I have 2 statements that reference 2 tables. The first statement works and the second doesn't. The only difference in the two is a field name, and I cannot figure out what is wrong. I've been working on it for 6 hours now. Could anyone assist?
This works
DB.Execute "UPDATE tblDEBIT20 SET CONTROL = '" & strControl & "' WHERE DEBIT_NUM = '" & strDebit & "'"
This doesn't
DB.Execute "UPDATE tblDEBIT20 SET CURRENTNAME = '" & strCurrent & "' WHERE DEBIT_NUM = '" & strDebit & "'"
The error I get is
No value given for one or more required parameters.
I think the point being made by both Ken and Robert is that you most likely have a name with an apostrophe in it. You can either replace all single quotes (') with something else or try another quote delimiter.
See if this works for you (escaping double quotes when used inside double quotes).
DB.Execute "UPDATE tblDEBIT20 SET CURRENTNAME = """ & strCurrent & """ WHERE DEBIT_NUM = """ & strDebit & """"
A tip for you in future debugging - add a Debug.Print strCurrent before the execute command - then when it fails you can easily see what the issue is - or at least tell us what value it failed on.
Below query is not returning any rows into the listbox.There is no error message:
lstDiff.RowSource = "select TestScenario,TestId from tblTesting where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'"
Could anyone help?
Your values in the field are numeric, so the extra single quotes aren't needed. Code should look like the following:
Me.lstDiff.RowSource = "select TestScenario,TestId from tblTesting where empid= " & Me.txtEmpId & " and testid= " & Me.txtAutoNumber & ";"
I've also dropped .Value from the field references, they're not harmful, but also aren't necessary.
And I've added a semi-colon to the end of your statement.
Depending on when/where you insert this code, you may need to add the following statement as well:
Me.lstDiff.Requery
You keep posting questions about the exact same WHERE clause with exactly the same apparent error in each one. SO users dutifully point out your error and then a few days later, you show up with a related question utilizing the same faulty WHERE clause.
DLookup Problem:
txtContSunStart1.Value = DLookup("Start1", "tblContract", _
"TestId = " & _
lstResults.Value & _
"" And "Day = 'Sunday'")
VBA Update Query:
DoCmd.RunSQL (" Update tbltesting set IsDiff ='Yes' where empid= " & Me.txtEmpId.Value & " and testid= " & Me.txtAutoNumber.Value & ";")
VBA SQL Problem
DoCmd.RunSQL ("insert into tbltesting (IsDiff)values ('Yes') where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'")
And then in the current question:
lstDiff.RowSource = "select TestScenario,TestId from tblTesting where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'"
You are having difficulties with exactly the same set of problems repeatedly.
Here are the rules for concatenating SQL strings with the correct delimiters in Access:
numeric values do not need delimiters:
"... AND testid= " & Me!txtAutoNumber
text values need quote delimiters. In Access, it's general practice to use double quotes, but much easier to use single quotes since it's a pain to type double quotes in a form that will work (typing """ or """" depending on context is counterintuitive and silly to me, so I always define a global constant that holds the double quote symbol and concatenate with that).
"... AND textfield=" & Chr(34) & Me!MyTextField & Chr(34)
date values use the # delimiter:
"... AND datefield=#" & Me!MyDateField & "#"
Boolean fields require no quotes and it works best to use True and False:
"... AND IsDiff=True"
These rules apply both to WHERE clause criteria and to SET statements in UPDATE queries. The rules apply in writing a SQL string that you pass to DoCmd.RunSQL or CurrentDB.Execute, as well as to writing SQL strings to be used as the recordsource of a form or report or as the rowsource of a combo box or listbox.
Personally, whenever I use SQL statements in code, I prefer to store the statement in a variable. While testing, on the line after you assign your statement to a variable, you can use Debug.Print to see what your SQL statement looks like after parsing your txtempid and txtautonumber. It would look something like this.
Dim sSQL as String
sSQL = "select TestScenario,TestId from tblTesting where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'"
Debug.Print sSQL
lstDiff.RowSource = sSQL
Then as long as your immediate window is visible (Ctrl-G), you can see what your SQL statement really is. If it looks right in the immediate window, copy and paste it into the query builder and run it there.
Try running the query in your SQL Management Studio. Do you get any row(s) back?
Edit: Just noticed the access-tag. Are you sure your table contains at least one post with supplied ids?
My Access is a bit rusty, but if all else fails try using a recordset to capture the data from the SQL and loop through it adding the values to the list box. Example Code