0

I've been having issues with creating relationships programatically in VB.NET for a piece of coursework. I keep getting the error of "Relationship must be on the same number of fields with the same data types." despite the fact that it is an AUTOINCREMENT into a NUMBER. Here is the code which i am using, bearing in mind that it creates the table just fine. Any help will be much appreciated.

CODE:

    Dim con As OleDbConnection

    Dim command As OleDbCommand

    Dim cat As New Catalog
    If My.Computer.FileSystem.FileExists("C:\Users.......\TEST.accdb") = False Then
        Dim str(3) As String
        cat.Create("Provider=microsoft.jet.oledb.4.0;Data Source = C:\Users........\TEST.accdb;jet oledb:engine type=5")
        cat = Nothing
        con = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source =C:\Users.......TEST.accdb")
        con.Open()
        str(0) = "CREATE TABLE [tbl_Student] ([StudentID] AUTOINCREMENT NOT NULL PRIMARY KEY, [CourseID] NUMBER)"
        str(1) = "CREATE TABLE [tbl_Course] ([CourseID] AUTOINCREMENT NOT NULL PRIMARY KEY)"
        For i = 0 To 1
            command = New OleDbCommand(str(i), con)
            command.ExecuteNonQuery()
        Next
        con.Close()

        con = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source =C:\Users........TEST.accdb")
        con.Open()
        Try
            str(3) = "ALTER TABLE [tbl_Student] ADD FOREIGN KEY (CourseID) REFERENCES tbl_Course(CourseID)"
            command = New OleDbCommand(str(3), con)
            command.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ErrorToString)
        End Try
        con.Close()
    End If
0

1 Answer 1

1

NUMBER isn't a defined data type for CREATE TABLE in Access. I'm surprised it works at all.

Use LONG instead.

See Microsoft Access Data Types (column "Data type (CREATETABLE)")

Sign up to request clarification or add additional context in comments.

2 Comments

NUMBER Is a synonym for FLOAT, that is why it works in the create table
Thanks so much, to be honest I was also suprised that NUMBER worked. That really does explain a lot. Thanks so much! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.