Access 2010 update table field after form checkbox update - vba

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

Related

Syntax Error (Missing Operator) in Update SQL statement for VBA 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)

How to make a field in access show the current value in the table, and accept input, but not update the table

I have been tasked with designing an Access front end for a SQL database I built. Unfortunately my VBA and Access knowledge are not as strong as they should be and I have run into a roadblock.
Currently I'm working on a script that will identify and flag high dollar assets that are entered into the database through the form. I have the update code written, but when I enter in a high value in the field the act of updating the value in the form and the update script itself clash and there is a 'Write Conflict'.
I can unbind the field in the form and that resoles the write conflict, but then the user first browsing to the record won't see the current value in the table.
My question: is there a way to have a field in a form show the value in a table, accept input, but not write the input to the table and leave the writing to the update query. If it helps I have included the update action I have written.
Private Sub Cost_AfterUpdate()
Dim CostCheck As String
CostCheck = "Update dbo_Purchasing SET Capitolasset = -1, COST = " & Me.Cost & " " & _
"Where itemMaster = " & Me.ItemMaster & " and " & Me.Cost & " >= " & 5000 & ""
DoCmd.RunSQL CostCheck
Me.Refresh
End Sub
It seems that you are trying to set the Capitolasset flag of the current record to -1 if the Cost entered is >= 5000. If so, you don't need to run an extra query, just update the Capitolasset value according to the Cost entered:
Private Sub Cost_AfterUpdate()
If IsNumeric(Me.Cost) Then
If Me.Cost >= 5000 Then
Me.Capitolasset = -1
Else
Me.Capitolasset = 0
End If
End If
End Sub
This will update the field before the record is saved.

Button searches subform, then finds record in main form (Access VBA)

I have this ACCESS form that contains a subform:Form & Subform
I'm trying to create a search button (using VBA) to find a student by name (students' names are not in the table behind the main form).
I've done the first step, which is to search the subform for a student's name, but I'm having trouble with my desired second step. I'd like the code to then take the CWID number of the student and find the matching record in the main form. How would I do this? (My current code is below)
I've tried DoCmd FindRecord and GoToRecord, but it is completely stumping me. I'm Google-learning how to do this and think I'm fundamentally misunderstanding something and am thus unable to search for or understand the answer. Any help would be greatly appreciated.
Private Sub btnSearch_Click()
Dim SQL As String
SQL = "SELECT [AWN Banner].CWID, [AWN Banner].FirstName, [AWN Banner].LastName, [AWN Banner].Freshman, [AWN Banner].Instructor, [AWN Banner].Course " _
& "FROM [AWN Banner] " _
& "RIGHT JOIN [AWNEntry] ON [AWN Banner].CWID = [AWNEntry].CWID " _
& "WHERE [LastName] LIKE '" & Me.txtKeywords & "*' " _
& "ORDER BY [AWN Banner].LastName "
Me.subAwnObj.Form.RecordSource = SQL
Me.subAwnObj.Form.Requery
End Sub
When I create my search boxes I put in the underlying form source a WHERE clause WHERE [field] LIKE "*" & [MySearchBox] & "*" I put such a clause in the subform and the main form I create a JOIN to the subform record with the same WHERE clause defined. Then on click I request all recordsources.

Syntax error in SQL update

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!

MS Access SQL query within VBA code

I need a second set of eyes on this SQL query embedded in the VBA code. I'm making an MS Access app that returns a dataset based on the to & from date criteria set by the user in the specific date picker boxes. The sql query that you see actually has been tested statically within MS Access query design view. I tested it with actual dates where you see the Me.from_filter and Me.to_filter. It worked perfectly! If you chose something like from 1/1/2015 to 5/1/2015 it returns columns of all the months you need. Perfect. Now when I embed it in the VBA code and assign it to a control variable, I get the "Run-time error '2342': A RunSQL action requires an argument consisting of an SQL statement." Can someone please eyeball this and tell me what might be wrong?
Option Compare Database
Option Explicit
Dim strSQL As String
Private Sub Command0_Click()
If IsNull(Me.from_filter) Or IsNull(Me.to_filter) Then
MsgBox "You have not entered a start date or end date"
Else
strSQL = "TRANSFORM Sum(dbo_ASSET_HISTORY.MARKET_VALUE) AS SumOfMARKET_VALUE " _
& "SELECT [dbo_FIRM]![NAME] AS [FIRM NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME " _
& "FROM (dbo_ASSET_HISTORY INNER JOIN dbo_FIRM ON dbo_ASSET_HISTORY.FIRM_ID = dbo_FIRM.FIRM_ID) INNER JOIN dbo_FUND ON dbo_ASSET_HISTORY.FUND = dbo_FUND.FUND " _
& "WHERE (((dbo_FIRM.Name) Like 'Voya F*') And ((dbo_ASSET_HISTORY.PROCESS_DATE) >= #" & Me.from_filter & "# And (dbo_ASSET_HISTORY.PROCESS_DATE) <= #" & Me.to_filter & "#)) " _
& "GROUP BY [dbo_FIRM]![NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME " _
& "PIVOT [dbo_ASSET_HISTORY]![ASSET_YEAR] & '-' & [dbo_ASSET_HISTORY]![ASSET_MONTH];"
DoCmd.RunSQL (strSQL)
End If
End Sub
RunSQL is for running action queries like update, insert, select into, delete, etc as per Microsoft definition https://msdn.microsoft.com/en-us/library/office/ff194626.aspx , you should probably use OpenQuery or similar to run your query.