Hey guys im trying to display diffirent tables on 1 datagridview (ofcourse its one at a time only)

so far it works everytime i change the datatable using listview for selecting now the problem is how do i insert data on it but only using a single form....each table have the same field name but different table name in this case different dept

heres my code for inserting items

 If frmSGHI.ToolStripLabel9.Text = "Center Lane" Then
            Dim SqlStatement As String = "INSERT INTO tblcenterlane(DESCRIPTION, UNIT, BEG, OT_KITCHEN, OT_F&B, OT_F.O., OT_S&M, OT_H.K., ADMIN, DEL, PULLOUT, SPOILAGE, ENDING, UNIT_COST, AMOUNT, VARIANCE, REMARKS, DEPARTMENT, DATE, DATE_DURATION_FROM, DATE_DURATION_TO, SUPPLIER_ID, CATEGORY) VALUES('" & txtItemDesc.Text & "','" & txtUnit.Text & "','" & txtBeg.Text & "','" & txtDel.Text & "','" & txtSpoilDmg.Text & "','" & txtPout.Text & "','" & txtEnding.Text & "','" & txtUnitCost.Text & "','" & txtAmount.Text & "','" & txtVariance.Text & "','" & txtDate.Text & "','" & txtDurationF.Text & "','" & txtDurationT.Text & "','" & txtSupplier.Text & "','" & txtCategory.Text & "','" & rtxtRemarks.Text & "')"
            Save(SqlStatement)
            MsgBox("1")
        ElseIf frmSGHI.ToolStripLabel9.Text = "Foods" Then
            Dim SqlStatement As String = "INSERT INTO tblfoods(DESCRIPTION, UNIT, BEG, OT_KITCHEN, OT_F&B, OT_F.O., OT_S&M, OT_H.K., ADMIN, DEL, PULLOUT, SPOILAGE, ENDING, UNIT_COST, AMOUNT, VARIANCE, REMARKS, DEPARTMENT, DATE, DATE_DURATION_FROM, DATE_DURATION_TO, SUPPLIER_ID, CATEGORY) VALUES('" & txtItemDesc.Text & "','" & txtUnit.Text & "','" & txtBeg.Text & "','" & txtDel.Text & "','" & txtSpoilDmg.Text & "','" & txtPout.Text & "','" & txtEnding.Text & "','" & txtUnitCost.Text & "','" & txtAmount.Text & "','" & txtVariance.Text & "','" & txtDate.Text & "','" & txtDurationF.Text & "','" & txtDurationT.Text & "','" & txtSupplier.Text & "','" & txtCategory.Text & "','" & rtxtRemarks.Text & "')"
            Save(SqlStatement)
            MsgBox("2")
        ElseIf frmSGHI.ToolStripLabel9.Text = "Lane B" Then
            Dim SqlStatement As String = "INSERT INTO tbllaneb(DESCRIPTION, UNIT, BEG, OT_KITCHEN, OT_F&B, OT_F.O., OT_S&M, OT_H.K., ADMIN, DEL, PULLOUT, SPOILAGE, ENDING, UNIT_COST, AMOUNT, VARIANCE, REMARKS, DEPARTMENT, DATE, DATE_DURATION_FROM, DATE_DURATION_TO, SUPPLIER_ID, CATEGORY) VALUES('" & txtItemDesc.Text & "','" & txtUnit.Text & "','" & txtBeg.Text & "','" & txtDel.Text & "','" & txtSpoilDmg.Text & "','" & txtPout.Text & "','" & txtEnding.Text & "','" & txtUnitCost.Text & "','" & txtAmount.Text & "','" & txtVariance.Text & "','" & txtDate.Text & "','" & txtDurationF.Text & "','" & txtDurationT.Text & "','" & txtSupplier.Text & "','" & txtCategory.Text & "','" & rtxtRemarks.Text & "')"
            Save(SqlStatement)
            MsgBox("3")
        ElseIf frmSGHI.ToolStripLabel9.Text = "Lane C" Then
            Dim SqlStatement As String = "INSERT INTO tbllanec(DESCRIPTION, UNIT, BEG, OT_KITCHEN, OT_F&B, OT_F.O., OT_S&M, OT_H.K., ADMIN, DEL, PULLOUT, SPOILAGE, ENDING, UNIT_COST, AMOUNT, VARIANCE, REMARKS, DEPARTMENT, DATE, DATE_DURATION_FROM, DATE_DURATION_TO, SUPPLIER_ID, CATEGORY) VALUES('" & txtItemDesc.Text & "','" & txtUnit.Text & "','" & txtBeg.Text & "','" & txtDel.Text & "','" & txtSpoilDmg.Text & "','" & txtPout.Text & "','" & txtEnding.Text & "','" & txtUnitCost.Text & "','" & txtAmount.Text & "','" & txtVariance.Text & "','" & txtDate.Text & "','" & txtDurationF.Text & "','" & txtDurationT.Text & "','" & txtSupplier.Text & "','" & txtCategory.Text & "','" & rtxtRemarks.Text & "')"
            Save(SqlStatement)
            MsgBox("4")
        ElseIf frmSGHI.ToolStripLabel9.Text = "Lane D" Then
            Dim SqlStatement As String = "INSERT INTO tbllaned(DESCRIPTION, UNIT, BEG, OT_KITCHEN, OT_F&B, OT_F.O., OT_S&M, OT_H.K., ADMIN, DEL, PULLOUT, SPOILAGE, ENDING, UNIT_COST, AMOUNT, VARIANCE, REMARKS, DEPARTMENT, DATE, DATE_DURATION_FROM, DATE_DURATION_TO, SUPPLIER_ID, CATEGORY) VALUES('" & txtItemDesc.Text & "','" & txtUnit.Text & "','" & txtBeg.Text & "','" & txtDel.Text & "','" & txtSpoilDmg.Text & "','" & txtPout.Text & "','" & txtEnding.Text & "','" & txtUnitCost.Text & "','" & txtAmount.Text & "','" & txtVariance.Text & "','" & txtDate.Text & "','" & txtDurationF.Text & "','" & txtDurationT.Text & "','" & txtSupplier.Text & "','" & txtCategory.Text & "','" & rtxtRemarks.Text & "')"
            Save(SqlStatement)
            MsgBox("5")
        Else
            MsgBox("6")
        End If


Public Sub Save(ByRef SqlStatement As String)

        On Error GoTo wat

        Dim cmd As MySqlCommand = New MySqlCommand

        With cmd

            .CommandText = SqlStatement
            .CommandType = CommandType.Text
            .Connection = myconn
            .ExecuteNonQuery()

        End With

        MsgBox("Success!")

        If MsgBox("Do You Want to Add New Record?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then

            ClearTextboxes()
            txtItemDesc.Focus()

        Else

            myconn.Close()
            Me.Close()

        End If

wat:

    End Sub

im sorry if its really that long or messy

Recommended Answers

All 2 Replies

Try a parameterised query:

  "INSERT INTO @Table (col1,col2,col3) VALUES (@val1,@val2,@Val3)"

For details on how to use parameterized queries please refer to this code snippet. It also suggests how to adjust your code formatting to make queries easier to read.

commented: Nice link +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.