Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27512

VS 2012 Save edited data in same row in table in Access db

$
0
0
Hi yet another issue. :(

I've run into trouble once again. I can't figure this out.

I have a form in my software called Hændelseslog, here you can enter a insident with a lot of data. Everything is stored in a table in an Access database. When adding a new entry everything works fine but when you cycle through previously entered data and edit an entry and press "save edited and exit" it saves it in the first row in the table not the row the data originately came from. That is a big issue 'cause it just overwrites the data in the first line.
It has to change the line which it came from.
Here's a screendump so you have a clue to what i'm doing:
Name:  screenHaend.jpg
Views: 79
Size:  92.0 KB

Furthermore, here is my complete code for this form: Sorry it's not that long and it gives you a clue to what I'm doing. :blush:

The "ButtonGemRedigerede_Click" is the button to save the edited text. This one doesn't work as it should as explained above.
The "ButtonGemOgRetur1_Click" is the save new entry and that works fine.

Code:

Public Class Hændelseslog
    Dim MaxRows As Integer
    Dim inc As Integer

    Private Sub Hændelseslog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DatabaseHaendelseslog.haendelseslog' table. You can move, or remove it, as needed.
        Me.HaendelseslogTableAdapter1.Fill(Me.DatabaseHaendelseslog.haendelseslog)
        'TODO: This line of code loads data into the 'DatabaseTogtData1.togtdata' table. You can move, or remove it, as needed.
        Me.TogtdataTableAdapter1.Fill(Me.DatabaseTogtData1.togtdata)
        MaxRows = Me.DatabaseHaendelseslog.Tables("Haendelseslog").Rows.Count
        inc = MaxRows '- 1
        'MsgBox(Me.DatabaseHaendelseslog.haendelseslog.Rows.Count)
        Me.TogtdataTableAdapter1.Fill(Me.DatabaseTogtData1.togtdata)
        FMTextBox2.Text = ""
        TOTextBox1.Text = ""
        DatoMaskedTextbox.Text = DateTime.UtcNow
        TidMaskedTextBox.Text = ""
        Ordre_fraTextBox1.Text = ""
        Ordre_tilTextBox1.Text = ""
        PositionMaskedTextBox.Text = ""
        HændelseTextBox1.Text = ""
        Togtet_er_udfort_forTextBox.Text = DatabaseTogtData1.Tables("togtdata").Rows(0).Item(0)
        DTG_for_start_af_togtTextBox.Text = DatabaseTogtData1.Tables("togtdata").Rows(0).Item(1)
        DTG_for_slut_pa_togtTextBox.Text = DatabaseTogtData1.Tables("togtdata").Rows(0).Item(2)
    End Sub

    Private Sub NavigateRecords()
        FMTextBox2.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(1) & ""
        TOTextBox1.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(2) & ""
        DatoMaskedTextBox.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(3)
        TidMaskedTextBox.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(4) & ""
        Ordre_fraTextBox1.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(5) & ""
        Ordre_tilTextBox1.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(6) & ""
        PositionMaskedTextBox.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(7) & ""
        HændelseTextBox1.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(8) & ""
        Togtet_er_udfort_forTextBox.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(9) & ""
        DTG_for_start_af_togtTextBox.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(10) & ""
        DTG_for_slut_pa_togtTextBox.Text = DatabaseHaendelseslog.Tables("haendelseslog").Rows(inc).Item(11) & ""
       
    End Sub

    Private Sub ButtonGemOgRetur1_Click(sender As Object, e As EventArgs) Handles ButtonGemOgRetur1.Click
        Dim dsNewRow As DatabaseHaendelseslog.haendelseslogRow
        If FMTextBox2.Text = "" Then
            MsgBox("FM ikke udfyldt")
        Else
            If TOTextBox1.Text = "" Then
                MsgBox("TO ikke udfyldt")
            Else
                If TidMaskedTextBox.Text = "" Then
                    MsgBox("Tid ikke udfyldt")
                Else
                    If PositionMaskedTextBox.Text = "" Then
                        MsgBox("Position ikke udfyldt")
                    Else
                        If HændelseTextBox1.Text = "" Then
                            MsgBox("Hændelse ikke udfyldt")
                        Else
                            If Ordre_fraTextBox1.Text = "" Then
                                Ordre_fraTextBox1.Text = " "
                                GoTo Næste1

                            Else
Næste1:                        If Ordre_tilTextBox1.Text = "" Then
                                    Ordre_tilTextBox1.Text = " "
                                    GoTo Næste2
                                Else
Næste2:                            dsNewRow = DatabaseHaendelseslog.Tables("haendelseslog").NewRow
                                    dsNewRow.FM = FMTextBox2.Text
                                    dsNewRow._TO = TOTextBox1.Text
                                    dsNewRow.Dato = DatoMaskedTextBox.Text
                                    dsNewRow.Tid = TidMaskedTextBox.Text
                                    dsNewRow.Ordre_fra = Ordre_fraTextBox1.Text
                                    dsNewRow.Ordre_til = Ordre_tilTextBox1.Text
                                    dsNewRow.Position = PositionMaskedTextBox.Text
                                    dsNewRow.Hændelse = HændelseTextBox1.Text
                                    dsNewRow.Togtet_er_udfort_for = Togtet_er_udfort_forTextBox.Text
                                    dsNewRow.DTG_for_start_af_togt = DTG_for_start_af_togtTextBox.Text
                                    dsNewRow.DTG_for_slut_pa_togt = DTG_for_slut_pa_togtTextBox.Text

                                    DatabaseHaendelseslog.Tables("haendelseslog").Rows.Add(dsNewRow)
                                    Me.HaendelseslogTableAdapter1.Update(DatabaseHaendelseslog)
                                    Me.HaendelseslogTableAdapter1.Fill(Me.DatabaseHaendelseslog.haendelseslog)

                                    MsgBox("Hændelse gemt")
                                    Me.Close()
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub ButtonRetur1_Click(sender As Object, e As EventArgs) Handles ButtonRetur1.Click
        Me.Close()
    End Sub

    Private Sub ButtonForrige1_Click(sender As Object, e As EventArgs) Handles ButtonForrige1.Click

        If inc > 0 Then

            inc = inc - 1

            NavigateRecords()

        Else

            MsgBox("Ingen ældre poster")

        End If

    End Sub

    Private Sub ButtonNæste1_Click(sender As Object, e As EventArgs) Handles ButtonNæste1.Click

        If inc < MaxRows - 1 Then

            inc = inc + 1

            NavigateRecords()

        Else

            MsgBox("Ingen nyere poster")

        End If

    End Sub

    Private Sub ButtonTøm1_Click(sender As Object, e As EventArgs) Handles ButtonTøm1.Click
        inc = MaxRows
        FMTextBox2.Text = ""
        TOTextBox1.Text = ""
        DatoMaskedTextbox.Text = DateTime.UtcNow
        TidMaskedTextBox.Text = ""
        Ordre_fraTextBox1.Text = ""
        Ordre_tilTextBox1.Text = ""
        PositionMaskedTextBox.Text = ""
        HændelseTextBox1.Text = ""
        Togtet_er_udfort_forTextBox.Text = DatabaseTogtData1.Tables("togtdata").Rows(0).Item(0)
        DTG_for_start_af_togtTextBox.Text = DatabaseTogtData1.Tables("togtdata").Rows(0).Item(1)
        DTG_for_slut_pa_togtTextBox.Text = DatabaseTogtData1.Tables("togtdata").Rows(0).Item(2)
    End Sub

    Private Sub ButtonGemRedigerede_Click(sender As Object, e As EventArgs) Handles ButtonGemRedigerede.Click
        Me.Validate()
        Me.HaendelseslogBindingSource1.EndEdit()
        Me.TableAdapterManager1.UpdateAll(Me.DatabaseHaendelseslog)
        Me.Close()
    End Sub
End Class


Hope you can help me.

Thanks in advance.
Attached Images
 

Viewing all articles
Browse latest Browse all 27512

Trending Articles