"Where" in the validation rule - Access VBA - vba

Here is my problem: I have two tables. The first one is Stock_out, it contains a field named Quantity_out. The second one is stock, it contains a field named Available_Quantity. The two tables share a field named "RRN".
In the validation rule of Quantity_out, I want to make something like
[Quantity_out]>=[Stock].[Available_Quantity] where [RRN]=[Stock].[RRN]
The problem is where doesn't recognize "where".
My access version is 2016.
How can I make that?
Best regards,
SG

Here is the solution from another website. It's working.
Hi,
it's not possible to reference a different table in a validation rule.
There are two methods if you want to apply such a rule at the table level:
with macro code in a Before Change data macro (in Access versions >=2010),
example 4 in this article shows how to refer to a different table
in all versions >=Access 2000 you can use a CHECK constraint, which however has disadvantages:
It can only be done by VBA+ADO and is not visible in the UI, and you (almost) can't configure the error message. Code for your requirements:
CurrentProject.Connection.Execute _
"ALTER TABLE Stock_out" & _
" ADD CONSTRAINT Quantity_Must_Be_Higher_Or_Equal" & _
" CHECK (Stock_out.Quantity_out >= (SELECT Stock.Available_Quantity FROM Stock" & _
" WHERE Stock.RRN = Stock_out.RRN))"
The example uses the constraint name to give the user a hint what's wrong, as it is the only way to write sth into the system error message.
cu
Karl
Access FAQ (de/it): donkarl.com
Access Lobby: AccessDevelopers.org

Related

Access Form Macro Where Condition

This is my first post here. Kind of a newbie at Access and I've searched forums to answer my question and tried using similar answers to get my macro working, but I have a syntax error.
I want to click the details button on one form (Employee Profile), and open another form (Employee Training Records) where it will pull up records pertaining to the current employee profile.
In my where condition currently I have:
="[st_no]=" & [st_no] & " AND [emp_id]='" & [emp_id] & "'"
I get a syntax error saying:
(missing operator) in query expression '[st_no]=IEC 62841-2-5 AND [emp_id]='3"
What am I missing?
Since the field st_no looks to be a string, you'll need to enclose the corresponding value with single or double quotes; conversely, since emp_id looks to be an integer, you don't need the surrounding quotes.
As such, I would suggest:
="[st_no]='" & [st_no] & "' AND [emp_id]=" & [emp_id]

VBA to validate use of correct (updated) version of a file

Vendors are sent an excel workbook that they add data to and send back. Recently, we made some changes to this file (some data validation fields) that inhibit users from adding incorrect data. BUT...i've found that vendors are just using the old file because it doesn't contain these annoying validation features.
I'm attempting to add a function to an uploading macro that basically checks that the file is the current version (the one with validation).
I thought about adding a hidden worksheet to the new file and doing a simple count through to check the presence of this worksheet. Like below
For i = 1 To Worksheets.Count
If Worksheets(i).Name = "Validation" Then
exists = True
End If
Next i
If Not exists Then
exit sub
End If
Another option might be to add some type of signature to the code and check for the presence of that signature before uploading. I'm open to new ideas on how to tackle this just looking for creative ideas.
thanks for your help!
I should better write it down as an answer.
If you have data validation, you should have a relational table in Access consisting of these data, connected with a foreign key to your main table. While inserting the data in Excel to Access, use a sub-query where you can validate the data against the validation table in access. If an error occurs, handle it within your code so that you will know if the vendors are using the old workbook.
An example:
"INSERT INTO main_table (validation_column, column2, ...)
VALUES ((SELECT validation_id FROM validation_table WHERE validation_name = " & Cells(i,j).Value & "), value2, ...);"

MS Access Issue

Hello Stackoverflow community!
I have run into an issue and I'd love some advice.
I'm working with MS Access and I am trying to append two particular fields from one table to another; however this implementation is in a form and it gets a little complicated... So, I'll explain everything the best that I can
BACKGROUND INFORMATION:
First and fore most, I have two tables; one of which is a linked excel spread sheet from another directory (who is not willing to change any formatting what so ever, so I CANNOT make ANY changes to this file and it is being updated on a daily basis). This excel spreadsheet is very large and contains somewhere around 50 columns
The other table is not anywhere near as large but has around 20 columns and is meant to extract two columns from the excel spreadsheet (the first column and the third column). I'm trying to make a form for this database to be as user-friendly as possible and not many people in my office are familiar with the technicalities of Access queries and programming in VBA.
THE SITUATION:
On my form, the user will enter data into TextBoxA, from there they will click a button; this button will trigger a search through the linked excel spreadsheet for the data that was typed into TextBoxA. It will then copy the data from Field1 (which was the typed data) and Field3 and append these selected fields into the first two fields of the table in my Access Database. All of this is being done through a segment of VBA code
Private Sub CmdCloseForm_Click()
If IsNull(Me.TextBoxA) Or Me.TextBoxA = "" Then
MsgBox ("Field is empty, please try again!")
Else
Dim VendorNum As String
SearchingValue = Me.TextBoxA
Dim SQL As String
SQL = "INSERT INTO tbleRecord (Field1,Field2)" & _
"SELECT * " & _
"FROM tbleLinkedExcel " & _
"WHERE Field1 = '" & SearchingValue & "';"
DoCmd.RunSQL SQL
End If
End Sub
So the biggest issue here is that in Field1, and every time I try to run the code,
I receive an error; which I am assuming it is because of the space (unfortunately I cannot give the ACTUAL variable names out as it is confidential)
ERROR MESSAGE
The INSERT INTO statement contains the following unknown field name: 'FIELD 1'. Make sure you have typed the name correctly, and try the operation again.
The thing is, is that this 'FIELD 1' variable/name is not in my code, but in the linked excel spreadsheet (again, I am not able to change ANYTHING on this spreadsheet).
Thanks guys!

Insert value from one table to another by pressing a button

I have a select query called "tbl_Update" and a table called "tbl_A" in Access DB. I would like to press on on a button, which makes the following:
looks in fields "KW" and when they match (in this example below, the value "2016.45" are matching), then insert the value of "tbl_update.CA041073p" into "tbl_A.CA041073p". Seems to be very easy, but getting always Errors to do it. Any simple solutions how to do it? Thanks!
My code is the following and it is giving me the Error 3037:
Public Function Update()
DoCmd.RunSQL "Update tbl_A INNER JOIN tbl_Update " & _
"ON tbl_A.KW = tbl_Update.KW " & _
"SET tbl_A.CA041073p = [tbl_Update].[CA041073p] "
End Function
One quite likely reason is that the user running the program doesn't have read-write access to the database file, especially if it is located in program files folder.
So check the directory and file permissions and modify them if needed. You can also consider changing the location of the database file to another, more easily accessible folder.

Access. Alternative for left function

i am trying to write a query using left function in access to take only the first 3 characters of a field.
Is there any alternative method for performing the same process without using left
Usage of left function shows a compile. error all of a sudden without any reason. If i copy the table and query to a new database it works fine for a while before the error comes again. This happens only on the usage of Left function.
you can use mid function:
Mid (Field, 1, 3)
The compile error is showing up because you have a missing reference. Open any module and check the References.
You can always try Mid
Mid([field1],1,3)
It sounds very like you have a problem with your references. Look for any references marked "MISSING". Also try to delete Visual Basic for Applications, it won't allow this, but it sometimes corrects the problem. Finally, check the details of Visual Basic for Applications and make sure that is available in the stated location. Any alternative to Left will be affected by this problem.
This problem is frequently associated with a missing reference that you would not think had anything to do with Left.
Run the following code and report back as to the results. Also tell us what version of Access you are running.
Sub ViewReferenceDetails()
Dim ref As Reference
For Each ref In Access.References
Debug.Print ref.Name & " - " & ref.Major & "." & ref.Minor & " - " & ref.FullPath
Next ref
End Sub