Constraints against an external SQL server table (1 Viewer)

RogerCooper

Registered User.
Local time
Today, 06:10
Joined
Jul 30, 2014
Messages
286
I need to create a constraint within SQL Server that enforces the rule NET_WEIGHT <= GROSS_WEIGHT. My problem is that the GROSS_WEIGHT field is in a different SQL server database. How would I create such a constraint? Is there a way I could create a view or some similar object within my database which would have the data from the other database? Or is there a way I can define a constraint to lookup the data directly from the other database?
 

Ranman256

Well-known member
Local time
Today, 09:10
Joined
Apr 9, 2015
Messages
4,337
I have checks in the entry form to make sure the user has entered correct data.
Done on the form for easy changes, rather than have to edit the table properties.

If IsValidForm() then
SaveData
Docmd.Close
endif



Code:
Private Function IsValidForm() As Boolean
Dim vMsg
Select Case True
   Case IsNull(txtFile)
      vMsg = "File is missing"
   Case IsNull(txtTblData)
      vMsg = "Table name is missing"
End Select
If vMsg <> "" Then MsgBox vMsg, vbCritical, "Required Field"
IsValidForm = vMsg = ""
End Function
 

Josef P.

Well-known member
Local time
Today, 15:10
Joined
Feb 2, 2023
Messages
826
You could use an insert trigger in the SQL server to raise an error if necessary.
 

Minty

AWF VIP
Local time
Today, 14:10
Joined
Jul 26, 2013
Messages
10,371
Can you link the other table from the other database in Access?
Realistically, if that data is important in your database it should really be available as data in your database.
 

RogerCooper

Registered User.
Local time
Today, 06:10
Joined
Jul 30, 2014
Messages
286
I will check out using an insert trigger.

I link to the data using Access, but most users need to go into it using our ERP, so it is better to deal with it at a database level.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:10
Joined
Feb 19, 2002
Messages
43,275
"Constraint" is a very specific term. It is not possible to enforce constraints between separate databases. You can do whatever you want to in your Access FE to warn about this issue and attempt to prevent it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:10
Joined
Feb 19, 2002
Messages
43,275
Triggers can do a lot but I don't know if they can access other databases.
 

Josef P.

Well-known member
Local time
Today, 15:10
Joined
Feb 2, 2023
Messages
826
Triggers can do a lot but I don't know if they can access other databases.
If the access rights are there, it's not a problem.
In the same server instance it is very easy, you only have to declare completely (Database.Schema.TableName).
Otherwise, you can set up a linked server.

One could also consider synchronising the relevant table to be able to use a real constraint.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:10
Joined
Feb 19, 2002
Messages
43,275
Sounds like a Trigger will solve the problem. I would guess that you would need two of them though. One in each database so no matter which side changes the field, the change will be verified by the local trigger.
 

RogerCooper

Registered User.
Local time
Today, 06:10
Joined
Jul 30, 2014
Messages
286
If the access rights are there, it's not a problem.
In the same server instance it is very easy, you only have to declare completely (Database.Schema.TableName).
Otherwise, you can set up a linked server.

One could also consider synchronising the relevant table to be able to use a real constraint.
It is the same Server, so I will try this.
 

Isaac

Lifelong Learner
Local time
Today, 06:10
Joined
Mar 14, 2017
Messages
8,777
I need to create a constraint within SQL Server that enforces the rule NET_WEIGHT <= GROSS_WEIGHT. My problem is that the GROSS_WEIGHT field is in a different SQL server database. How would I create such a constraint? Is there a way I could create a view or some similar object within my database which would have the data from the other database? Or is there a way I can define a constraint to lookup the data directly from the other database?

I wonder if you could incorporate a CLR to check the other database, IF you don't have the link in sql server.
 

GregDataReno

New member
Local time
Today, 23:10
Joined
Jul 4, 2016
Messages
18
At any cost, I'd avoid relying on calling on data in one database to validate data in another - it will just lead to long-term pain, cost, frustration....

Instead, I'd consider a routine to retrieve into a table in your NET_WEIGHT database with the GROSS_WEIGHT values from the other database, and then rely on a constraint in your NET_WIGHT database that checks against the local GROSS_WEIGHT values.

The principle of 'coupling' in software design relates to your solution, specifically, avoiding interdependence between 'modules' (ie between databases in this case).

Goodluck with it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:10
Joined
Feb 19, 2002
Messages
43,275
Have you considered changing the design of the two schemas so that the related data is in the SAME database?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:10
Joined
Feb 19, 2002
Messages
43,275
Usually ERP's have unassigned columns in tables. Is it possible to find an unassigned column so you can add the data in that table. Of course, there will be no validation so that may not completely solve the problem. You certainly won't be allowed to add any triggers to the ERP so the best you can do is to check the value in the ERP when the value changes in your form. OR add a trigger on your own table to check it.

Depending on how important this relationship is, you might need to check the values EVERY time you use them because if the value in the ERP changes after your validation, you would never catch it unless you validate every time you use the data.
 

Minty

AWF VIP
Local time
Today, 14:10
Joined
Jul 26, 2013
Messages
10,371
How often does the data for that field change in the ERP ?
I'm wondering if you could copy the data into your database on a regular basis if it is fairly static?
 

RogerCooper

Registered User.
Local time
Today, 06:10
Joined
Jul 30, 2014
Messages
286
How often does the data for that field change in the ERP ?
I'm wondering if you could copy the data into your database on a regular basis if it is fairly static?
The GROSS_WEIGHT changes rarely, but when it changes is likely be when the NET_WEIGHT changes.
 

Users who are viewing this thread

Top Bottom