kerrance76
Registered User.
- Local time
- Today, 15:13
- Joined
- Mar 27, 2007
- Messages
- 16
Hello i am moving a database to a different system and have been told to write several functions for it. I have written the code below for setting up a new database but am having problems setting the delete and update cascades in strSQL5. I've tried everything i can think of but keep getting error in the constraint clause, can any body point out where i have gone wrong please.
I know this is not the easiest way or quickest of building a table but this is how i have been told to do it
Thanks for looking
I know this is not the easiest way or quickest of building a table but this is how i have been told to do it
Thanks for looking
Code:
Public Function createTables()
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQL3 As String
Dim strSQL4 As String
Dim strSQL5 As String
strSQL1 = "CREATE TABLE tblCustomers " & _
"(CustomerNo INTEGER, LastName TEXT (15), FirstName TEXT (10), Address TEXT (25), " & _
"City TEXT (15), Telephone TEXT (13), DiscountRate Single, " & _
"CONSTRAINT pk1 PRIMARY KEY (CustomerNo))"
strSQL2 = "CREATE TABLE tblSalesmen " & _
"(SalesmanNo INTEGER, SalesmanName TEXT (50), MonthlyBasicSalary SINGLE, " & _
"CommissionRate SINGLE, " & _
"CONSTRAINT pk2 PRIMARY KEY (SalesmanNo))"
strSQL3 = "CREATE TABLE tblParts " & _
"(PartNo LONG, Description TEXT (30), SellingPrice CURRENCY, CostPrice CURRENCY, " & _
"CONSTRAINT pk3 PRIMARY KEY (PartNo))"
strSQL4 = "CREATE TABLE tblInvoices " & _
"(InvoiceNo AUTOINCREMENT, [Date] DATE, CustomerNo INTEGER, SalesmanNo INTEGER, " & _
"CONSTRAINT pk4 PRIMARY KEY (InvoiceNo), " & _
"CONSTRAINT fk1 FOREIGN KEY (CustomerNo) " & _
"REFERENCES tblCustomers (CustomerNo), " & _
"CONSTRAINT fk2 FOREIGN KEY (SalesmanNo) " & _
"REFERENCES tblSalesmen (SalesmanNo))"
strSQL5 = "CREATE TABLE tblInvoiceLines " & _
"(InvoiceNo LONG, PartNo LONG, Quantity INTEGER, " & _
"CONSTRAINT pk5 PRIMARY KEY (invoiceNo, PartNo), " & _
"CONSTRAINT ck1 FOREIGN KEY (PartNo) " & _
"REFERENCES tblParts(PartNo), " & _
"CONSTRAINT ck2 FOREIGN KEY (InvoiceNo) " & _
"REFERENCES tblInvoices(InvoiceNo) " & _
"ON DELETE CASCADE ON UPGRADE CASCADE)"
Dim db As Database
Set db = DBEngine.OpenDatabase(Application.CodeProject.Path & "\copy.mdb")
Dim qdf As QueryDef
Set qdf = db.CreateQueryDef("", strSQL1)
qdf.Execute
Set qdf = db.CreateQueryDef("", strSQL2)
qdf.Execute
Set qdf = db.CreateQueryDef("", strSQL3)
qdf.Execute
Set qdf = db.CreateQueryDef("", strSQL4)
qdf.Execute
Set qdf = db.CreateQueryDef("", strSQL5)
qdf.Execute
End Function