0

Hello first of all thanks for your time

I am programming an app that makes use of an Acces database

Table Players

id Players Password Turn Moves

1 new 'empty' true 3

Table Map

id Regio Eigenaar Troepen

1 Alaska "empty" 0

2 NoordCanada "Empty" 0

...

40 IndoChina "Empty" 0

In my Application I first add 3 (to add 3 players) and then I put the data set In a DataGridView so you can adjust the names of the players the regions they own etc

this all happens in the following code

Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Public da As New OleDb.OleDbDataAdapter
Public ds As New DataSet
Dim sql As String
Public cb As New OleDb.OleDbCommandBuilder(da)

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    dbProvider = "PROVIDER=microsoft.ACE.OLEDB.12.0;"
    dbSource = "Data Source = "

    con.ConnectionString = dbProvider & dbSource & Form1.dbPath & "\Risk.accdb"

    con.Open()

    sql = "SELECT * FROM Map"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "Map")

    Sql = "SELECT * FROM Players"
    da = New OleDb.OleDbDataAdapter(Sql, con)
    da.Fill(ds, "Players")

    con.Close()

    For i = 2 To Form3.MaxPlayersBox.Maximum

        ds.Tables("Players").Rows.Add(i)
        'Dim cb As New OleDb.OleDbCommandBuilder(da)

        'da.Update(ds, "Players")

    Next i

    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "Players"

End Sub

Private Sub PLayersB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PLayersB.Click

    DataGridView1.DataMember = Nothing

    DataGridView1.DataMember = "Players"

End Sub

Private Sub MapB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MapB.Click

    DataGridView1.DataMember = Nothing

    DataGridView1.DataMember = "Map"

    RandomizeB.Visible = True

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RandomizeB.Click

    If MsgBox("heb je alle spelers toegevoegd?", vbYesNo) = vbNo Then

        Exit Sub

    End If

    Dim P As Integer = 0
    Dim RandInt As New Random
    Dim Rand As Integer

    For i = 0 To 39

        Rand = RandInt.Next(0, 39)

line34:     If ds.Tables("Map").Rows(Rand).Item("Eigenaar").ToString = "" Then

            ds.Tables("Map").Rows(Rand).Item("Eigenaar") = ds.Tables("Players").Rows(P).Item("Players")

        ElseIf Rand < 39 Then

            Rand += 1
            GoTo Line34

        Else

            Rand = 0
            GoTo line34

        End If

        P += 1

        If P > ds.Tables("Players").Rows.Count - 1 Then

            P = 0

        End If

    Next i

    RandomizeB.Visible = False


End Sub

Private Sub SaveB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveB.Click

    con.Open()
    sql = "SELECT * FROM Map"
    da = New OleDb.OleDbDataAdapter(sql, con)
    Dim cb As New OleDb.OleDbCommandBuilder(da)

    da.Update(ds, "Map")

    sql = "SELECT * FROM Players"
    da = New OleDb.OleDbDataAdapter(sql, con)
    Dim cb2 As New OleDb.OleDbCommandBuilder(da)

    da.Update(ds, "Players")

    'The problem is right here 


    Me.Close()

End Sub

So theDataSet I update has become

Table Players

id Players Password Turn Moves

1 Player1 'empty' true 3 2 Player2 'empty' False 0 3 Player3 'empty' False 0 4 Player4 'empty' False 0

Table Map

id Regio Eigenaar Troepen

1 Alaska "Player2" 0

2 NoordCanada "Player3" 0

...

40 IndoChina "Player1" 0

When I try to update the database (private sub SaveB_Click) I get an error:

da.Update(ds,"Players"): Instruction INSERT INTO contains a syntax error

I have been researching on the internet for a week now but I just don't know what to do

The database is saved in a shared Dropbox folder BTW. (that's what gives my game an online multiplayer feature)

Thank U and if you need more information I'll be available the rest of the day.

6
  • 1
    --> "INSERT INTO contains a syntax error" - start there. Commented Dec 9, 2013 at 13:15
  • I'm 17 and not an advaned programmer (I have no educational experience only self-learned skills) could you explain what I should do about this? Commented Dec 9, 2013 at 13:16
  • to INSERT into a DB you use a SQL INSERT statement. your code is using a SELECT statement. google SQL INSERT INTO Commented Dec 9, 2013 at 13:33
  • I suggest you do a debug and examine the offending insert command which commandbuilder has created. Commented Dec 9, 2013 at 13:38
  • @EmmandKareem I need to admit that I can't really debug . I can see where the code crashes and I try to fix it with trial and error I've tried debugging with the Intellitrance but I simply could not see figure out what it was saying. Commented Dec 9, 2013 at 13:44

1 Answer 1

2

These 2 blocks are wrong:

con.Open()
sql = "SELECT * FROM Map"
da = New OleDb.OleDbDataAdapter(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)

da.Update(ds, "Map")

The SQL to add new rows requires a SQL INSERT INTO statement, but it looks like you pasted your SELECT statement. THe INSERT INTO will require more info like what columns and what values to put there. I cant tell you what that SQL should be, because I have no idea what the columns are or what you want to put there.

FWIW, there is also an UPDATE statement where you are changing the values of an existing row, which is also different.

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

2 Comments

This points out my problem but doesn't solve it. thank you I'm off to the land of Internet research again (If anyone could solve the problem that would be appreciated) @Plutonix thank you
as I said, we cant tell you what that SQL should be because there is not enough info as to what is in the DB and what you want the values to be. the INSERT INTO syntax is not difficult though.

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.