Creating a constraint in a table in the backend (1 Viewer)

pacctono

Member
Local time
Today, 15:43
Joined
Jun 13, 2022
Messages
64
Hello!

I have a backend with the tables and the frontand with queries, forms, reports and link table. Now I would like to add a constraint in a table in the backend using vba:

DoCmd.RunSQL "ALTER TABLE diarios_det_2022 ADD CONSTRAINT RelacionCuenta FOREIGN KEY (cuenta) REFERENCES cuentas_2022 (cuenta);"

Please, any help, fellows
 

June7

AWF VIP
Local time
Today, 11:43
Joined
Mar 9, 2014
Messages
5,473
Is backend Access? Why don't you just open backend and modify Relationships?

Is the foreign key field in diarios_det_2022 named cuenta? Why do you have table with a year in name - do you create a table for each year? If so, why?

Are you running this in frontend? Would have to identify filepath for backend db.

In my test, constraint was created but did not show up in Relationships window. I had to manually add tables to Relationships and link automatically displayed.
 
Last edited:

pacctono

Member
Local time
Today, 15:43
Joined
Jun 13, 2022
Messages
64
Is backend Access? Why don't you just open backend and modify Relationships?

Is the foreign key field in diarios_det_2022 named cuenta? Why do you have table with a year in name - do you create a table for each year? If so, why?

Are you running this in frontend? Would have to identify filepath for backend db.
Is backend Access? yes Why don't you just open backend and modify Relationships? Can I do that in vba from the frontend?

Is the foreign key field in diarios_det_2022 named cuenta? yes Why do you have table with a year in name - do you create a table for each year? bad practice If so, why? I found it like that I,ll try to change it in a new version

Are you running this in frontend? yes Would have to identify filepath for backend db. : Split(CurrentDb.TableDefs("cuentas_2020").Connect, "=")(1)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:43
Joined
May 7, 2009
Messages
19,245
open the Backend Exclusively and do it there.
 

ebs17

Well-known member
Local time
Today, 21:43
Joined
Feb 7, 2020
Messages
1,946
Code:
Application.DoCmd.RunSQL sSQL
The DoCmd object is an object of the Access application object. If nothing else is specified, it will be executed where the code is running, i.e. in the frontend. So it can't work that way.

Using VBA, constraints can only be created and removed via ADODB. So you would have to create an ADODB connection to the backend and use it to execute the SQL statement.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:43
Joined
Jan 20, 2009
Messages
12,852
The year being in the table name makes me think the whole need to add the constraint stems from some kind of archiving that you are expecting users to do at runtime. Wrong thinking. It is a dangerous process that should not be done routinely.

Why are you creating archive tables? If you have more data than Access backend can handle (2 GB) then you should be using an RDMS backend like SQL Server which will hold 10 GB in the free version or practically unlimited in the paid versions.
 

June7

AWF VIP
Local time
Today, 11:43
Joined
Mar 9, 2014
Messages
5,473
ebs17, as already commented, I tested code and it works, no connection need be established. Just had to add filepath to SQL statement. Drawback noted was although constraint established, relationship not displayed in Relationships.
 

561414

Active member
Local time
Today, 14:43
Joined
May 28, 2021
Messages
280
Here's some VBA code to add constraints from VBA

EDIT
However, it's for creating tables with constraints already.
I tested this
Code:
Sub x()
DoCmd.RunSQL "ALTER TABLE diarios_det_2022 ADD CONSTRAINT RelacionCuenta FOREIGN KEY (cuenta) REFERENCES cuentas_2022 (cuenta);"
End Sub

it runs fine, it just assumes diarios_det_2022 table already has a cuenta field and cuentas_2022 table has a cuenta field as primary key
 
Last edited:

ebs17

Well-known member
Local time
Today, 21:43
Joined
Feb 7, 2020
Messages
1,946
add filepath to SQL statement
When using DDL, this is a SPECIAL design.

I don't understand the sticking to the DoCmd stuff (RunSQL, OpenQuery). DAO and ADODB are not foreign languages (for me).
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:43
Joined
Feb 19, 2002
Messages
43,280
This db is for one of the applications I sell to the public. Occasionally, I make upgrades. When the client is using the SQL Server BE, I send them a script for their DBA to run to "convert" the BE to a new version. For ACE BE's, the clients run this attached DB. It converts a ver36 BE to ver37.

The code uses both the currentdb() object as well as a named be. All of the DDL queries are in the FE. The FE copies each querydef into an sql string one at a time and then runs them against the named BE. The tables in the named BE are not linked.

To create the DDL, I used a product called SQL Examiner. It compares two SQL server databases and generates a script to convert one database to the other format. I use the script as is for my SQL server clients. For my ACE clients, I convert the script to individual DDL queries.

@pacctono I'm pretty sure that you have other problems that need to be corrected and so you probably don't actually need this type of code but I'm including it because we see this request occasionally and there are valid reasons to run code in dbA to update the schema in dbB. This is the simplest non-code solution. I could have used DAO to do this but why when I had a tool that would generate perfectly good DDL?

Just FYI, in case you don't want to open the attachment, here's a couple of examples of what DDL looks like:

DROP INDEX [tblStatestblAuditParms] on [tblAuditParms]
---
CREATE UNIQUE INDEX [UniqueIDX] ON [tblListValues]([AuditParmsID], [ListName], [ItemName])
---
ALTER TABLE [tblListValues] DROP CONSTRAINT [DF__tblListVa__ITOnl__33D4B598]
---
CREATE TABLE [tmp_tblListValues]
(
[ListValueID] COUNTER Primary Key,
[AuditParmsID] Long NOT NULL,
[ListName] text(50) NOT NULL,
[ItemName] text(50) NOT NULL,
[Seqnum] Integer NULL,
[ITOnly] Byte NOT NULL,
[UpdatedBy] text(20) NULL,
[UpdatedDT] DateTime NOT NULL,
[upsize_ts] timestamp NULL
)
 

Attachments

  • DEA_Ver36toVer37.zip
    221.1 KB · Views: 66
Last edited:

Users who are viewing this thread

Top Bottom