I am making a form in MS Access, my version is 2019 from Office 2019. I have problem with duplicate entry of the invoice for purchased merchandise. Each supplier has their own way of defining their "invoice numbers" and sometimes they are using similar numbers since some use "date" to define their invoice number for example , company A invoice no is 202107001 and Company B also has 202107001 since it is July of 2021 and it happens to be the "first bill" of their month... some supplier do not have "invoice no." because they are individual selling handmade product.
Therefore, I have to write vba code to prevent entering "duplicate" invoice number from our suppliers! Here is my unworking code...
not even it doesn't work but also shows "method or data not found" too! Would there be anyone kindly help please?
Here is the condition I intend to do with the below code
"If the entered supplier invoice number is a duplicate value according to their own identification scheme, the message box should appear as the latter part of the vba code..."
Private Sub txtIMHead_supinvoicenumber_AfterUpdate()
Dim supinvoicenumberstring As String
supinvoicenumberstring = Me.IMHead_supinvoicenumber.Value
If Me.txtHead_supinvoicenumber = DLookup("[IMHead_supinvoicenumber]", "tbl_IMHead", txt_IMHead_supid) Then
MsgBox "This supplier's invoice number" & supinvoicenumberstring & ",has been recorded!" _
& vbCr & " Please stop the process and record another invoice!" & vbCr & "Thank you very much!"
Me.Undo
End If
End Sub
WHERE CONDITION argument needs to specify field that criteria applies to:
DLookup("[IMHead_supinvoicenumber]", "tbl_IMHead", "some field name here=" & Me.txt_IMHead_supid)
However, that expression will return IMHead_supinvoicenumber from first record that matches the given supid. You need to know if invoice number already exists for selected supid so must use invoice number in criteria as well.
If Not IsNull(DLookup("[IMHead_supinvoicenumber]", "tbl_IMHead", _
"some field name here=" & Me.txt_IMHead_supid & _
" AND IMHead_supinvoicenumber='" & supinvoicenumberstring & "'")) Then
An option is DCount:
If DCount("*", "tbl_IMHead", "some field name here=" & Me.txt_IMHead_supid & _
" AND IMHead_supinvoicenumber='" & supinvoicenumberstring & "'") > 0 Then
Related
I have a unique index of fields in my table in microsoft access. The fields are Shift, Operator, Date_Field, and Machine. I have a data entry form with combobox selections for these fields, except for the date which autopopulates today's date. I want to be able to navigate the form te the record that matches the existing Shift/Operator/Date/Machine combo if it already exists. I have a DLookup function that checks to see if such a record exists already, but now I need to know how to change the form so it is entering the data on that record instead of a new one.
Here's what I have so far. It is being activated in the AfterUdate of one of the last combobox in the tab order.
Dim int_ID As Integer
With Me
'checks if duplicate record exists and stores it as int_ID variable
int_ID = Nz(DLookup("ID", "Tracking", "Shift= " & Val(.ShiftCbo) & " And Operator='" & .OpCbo.Column(1) & "' And Date_Field=#" & .DateBox & "# And Machine='" & .MachineCbo.Column(1) & "'"), 0)
End With
If int_ID <> 0 Then
'I need to know what to put here to take the form to the existing record.
End If
I've tried to use Cmd.GoToRecord but that doesn't work.
One of my favorite (and simplest) methods is using Me.Recordsource
Once you have your code working correctly which defines the filter criteria, try something like this:
Me.Recordsource= "select * from UnderlyingFormMainQuery where TextColumnName='" & varString & "' or NumericColumnName=" & intSomething
Me.Requery
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 trying to set a quantity value based on a sum of the current quantity and then subtracting an amount based on a quantity value in another table.
My current thought process is: If QuantityA is less than or equal to Quantity B then subtract QuantityA from Quantity B.
I am no expert on SQL and MS VBA, so I was wondering how I can make an if statement with the two different tables.
Private Sub Command23_Click()
If QuantityA <= QuantityB Then
QuantityB = QuantityB - QuantityA
Else
MsgBox "You can not hire that many" & ToolName & "as there is only" & [DEPARTMENT TOOLS (stocked)].Quantity & "available"
End If
MsgBox "Your hire request has been successful"
End Sub
Any help would be appreciated
You can achieve what you're asking by opening a recordset containing the results of a query and comparing the values returned by the query. In order to restrict the query to the proper quantities you need to pass in the ID of the tool you are checking.
#June7 is correct about saving calculated data. The only time you would want to save it is if you are keeping a log of events or if speed of join queries becomes an issue (unlikely given proper DB structure)
toolId might be stored in a text box control for example
Function Something_Click(toolId as string)
Dim rs as Recordset
' create a recordset containing data from the two tables
Set rs = CurrentDB.OpenRecordset("SELECT A.TOOL_QTY AS TOOL_QTY_A, " _
& " B.TOOL_QTY AS TOOL_QTY_B, A.TOOLNAME AS TOOLNAME" _
& " FROM TABLE1 AS A " _
& " INNER JOIN TABLE2 AS B ON A.TOOL_ID = B.TOOL_ID" _
& " WHERE A.TOOL_ID = '" & toolId & "'")
' compare the values in the table
If rs![TOOL_QTY_A] <= rs![TOOL_QTY_B] Then
' your true condition, for example
' MsgBox "Success"
Else
' your false condition, for example
' MsgBox "Oops, not enough of " & rs![TOOLNAME]
End If
' close the recordset
rs.close
End Function
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
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.