根据组合框选择将字符串插入不同的表

时间:2019-07-04 14:25:29

标签: vb.net ms-access

首先,如果有人问这个问题,我深表歉意。我已经看过,但没有找到我所面临问题的确切答案。其次,我必须强调,我不是开发人员,我是工程师,并且只在需要时编写软件。

我有一个将数据传递到Access数据库的表单(此方法有效)。但是,我需要对其进行更新,以便根据组合框中的选择将信息传递到同一数据库中的不同表。例如,如果组合框选择= X,则插入tableX;如果组合框= Y,则插入tableY。任何帮助都将受到赞赏。

我尝试使用If语句来选择适当的表,但这不起作用。

Imports System.Data.OleDb

Public Class Form1
Public ds As New DataSet
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
Dim rs As New resizer
Dim cmd As OleDbCommand


Private con As Object

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


    provider = "Provider=Microsoft.Jet.OleDb.4.0;Data Source="
    dataFile = "R:\Quality\NCR-Access_Database\NCRdb1.mdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString
    myConnection.Open()



    Dim str As String
    str = ""

    If ComboBox2.SelectedText = "Assembly" Then
        str = "Insert into [ASSEMBLYtbl]([NCR-No],[Week-No],[Part-No],[Drawing-Rev],[Description],[W/O-Number],[Operator-No],[Operation-No],[Machine-No],[Section],[Batch-Qty],[Reject_Qty],[Disposition],[Mat-Cost],[Standard-Cost],[Defect-Descripition],[Fault-Code],[Dept],[Root-Cause],[NCR-Pinksheet],[Action]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    ElseIf ComboBox2.SelectedText = "Grinding" Then
        str = "Insert into [GRINDINGtbl]([NCR-No],[Week-No],[Part-No],[Drawing-Rev],[Description],[W/O-Number],[Operator-No],[Operation-No],[Machine-No],[Section],[Batch-Qty],[Reject_Qty],[Disposition],[Mat-Cost],[Standard-Cost],[Defect-Descripition],[Fault-Code],[Dept],[Root-Cause],[NCR-Pinksheet],[Action]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    ElseIf ComboBox2.SelectedText = "Milling" Then
        str = "Insert into [MILLINGtbl]([NCR-No],[Week-No],[Part-No],[Drawing-Rev],[Description],[W/O-Number],[Operator-No],[Operation-No],[Machine-No],[Section],[Batch-Qty],[Reject_Qty],[Disposition],[Mat-Cost],[Standard-Cost],[Defect-Descripition],[Fault-Code],[Dept],[Root-Cause],[NCR-Pinksheet],[Action]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    ElseIf ComboBox2.SelectedText = "Mill-Turn" Then
        str = "Insert into [MILL-TURNtbl]([NCR-No],[Week-No],[Part-No],[Drawing-Rev],[Description],[W/O-Number],[Operator-No],[Operation-No],[Machine-No],[Section],[Batch-Qty],[Reject_Qty],[Disposition],[Mat-Cost],[Standard-Cost],[Defect-Descripition],[Fault-Code],[Dept],[Root-Cause],[NCR-Pinksheet],[Action]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    ElseIf ComboBox2.SelectedText = "Turning" Then
        str = "Insert into [TURNINGtbl]([NCR-No],[Week-No],[Part-No],[Drawing-Rev],[Description],[W/O-Number],[Operator-No],[Operation-No],[Machine-No],[Section],[Batch-Qty],[Reject_Qty],[Disposition],[Mat-Cost],[Standard-Cost],[Defect-Descripition],[Fault-Code],[Dept],[Root-Cause],[NCR-Pinksheet],[Action]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    ElseIf ComboBox2.SelectedText = "Supplier" Then
        str = "Insert into [PURCHASINGtbl]([NCR-No],[Week-No],[Part-No],[Drawing-Rev],[Description],[W/O-Number],[Operator-No],[Operation-No],[Machine-No],[Section],[Batch-Qty],[Reject_Qty],[Disposition],[Mat-Cost],[Standard-Cost],[Defect-Descripition],[Fault-Code],[Dept],[Root-Cause],[NCR-Pinksheet],[Action]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    ElseIf ComboBox2.SelectedText = "Subcon" Then
        str = "Insert into [PURCHASINGtbl]([NCR-No],[Week-No],[Part-No],[Drawing-Rev],[Description],[W/O-Number],[Operator-No],[Operation-No],[Machine-No],[Section],[Batch-Qty],[Reject_Qty],[Disposition],[Mat-Cost],[Standard-Cost],[Defect-Descripition],[Fault-Code],[Dept],[Root-Cause],[NCR-Pinksheet],[Action]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    ElseIf ComboBox2.SelectedText = "Quality" Then
        str = "Insert into [QUALITYtbl]([NCR-No],[Week-No],[Part-No],[Drawing-Rev],[Description],[W/O-Number],[Operator-No],[Operation-No],[Machine-No],[Section],[Batch-Qty],[Reject_Qty],[Disposition],[Mat-Cost],[Standard-Cost],[Defect-Descripition],[Fault-Code],[Dept],[Root-Cause],[NCR-Pinksheet],[Action]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
    End If


    cmd = New OleDbCommand(str, myConnection)
    cmd.Parameters.Add(New OleDbParameter("NCR-No", TextBox1.Text))
    cmd.Parameters.Add(New OleDbParameter("Week-No", TextBox3.Text))
    cmd.Parameters.Add(New OleDbParameter("Part-No", TextBox4.Text))
    cmd.Parameters.Add(New OleDbParameter("Drawing_Rev", TextBox5.Text))
    cmd.Parameters.Add(New OleDbParameter("Description", TextBox6.Text))
    cmd.Parameters.Add(New OleDbParameter("W/O-No", TextBox7.Text))
    cmd.Parameters.Add(New OleDbParameter("Operator-No", TextBox8.Text))
    cmd.Parameters.Add(New OleDbParameter("Operation-No", TextBox9.Text))
    cmd.Parameters.Add(New OleDbParameter("Machine-No", TextBox10.Text))
    cmd.Parameters.Add(New OleDbParameter("Section", ComboBox2.Text))
    cmd.Parameters.Add(New OleDbParameter("Batch-Qty", TextBox12.Text))
    cmd.Parameters.Add(New OleDbParameter("Reject_Qty", TextBox13.Text))
    cmd.Parameters.Add(New OleDbParameter("Disposition", TextBox14.Text))
    cmd.Parameters.Add(New OleDbParameter("Mat-Cost", TextBox15.Text))
    cmd.Parameters.Add(New OleDbParameter("Standard-Cost", TextBox16.Text))
    cmd.Parameters.Add(New OleDbParameter("Defect-Description", RichTextBox1.Text))
    cmd.Parameters.Add(New OleDbParameter("Fault-Code", TextBox17.Text))
    cmd.Parameters.Add(New OleDbParameter("Dept", TextBox18.Text))
    cmd.Parameters.Add(New OleDbParameter("Root-Cause", RichTextBox2.Text))
    cmd.Parameters.Add(New OleDbParameter("NCR-Pinksheet", ComboBox1.Text))
    cmd.Parameters.Add(New OleDbParameter("Permanent-Action", RichTextBox3.Text))

    Try

        cmd.ExecuteNonQuery()
        cmd.Dispose()
        myConnection.Close()
        TextBox1.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox3.Clear()
        TextBox6.Clear()
        TextBox7.Clear()
        TextBox8.Clear()
        TextBox9.Clear()
        TextBox10.Clear()
        ComboBox2.ResetText()
        TextBox12.Clear()
        TextBox13.Clear()
        TextBox14.Clear()
        TextBox15.Clear()
        TextBox16.Clear()
        RichTextBox1.Clear()
        TextBox17.Clear()
        TextBox18.Clear()
        RichTextBox2.Clear()
        ComboBox1.ResetText()
        RichTextBox3.Clear()


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub


End Class

提交时,出现一个对话框,指出“未为命令对象设置命令文本”。如果我再次提交,则会在VS中收到未处理的异常事件---“ System.InvalidOperationException:'不允许更改'ConnectionString'属性。连接的当前状态为打开状态。'”

1 个答案:

答案 0 :(得分:0)

使用INSERT INTO语法

INSERT INTO table-name (column-names) 
VALUES (values)

使用SELECT这样的SQL INSERT INTO

INSERT INTO Customer (FirstName, LastName, City, Country, Phone)
SELECT LEFT(ContactName, CHARINDEX(' ',ContactName) - 1), 


SUBSTRING(ContactName, CHARINDEX(' ',ContactName) + 1, 100),
  City, Country, Phone
 FROM Supplier
 WHERE CompanyName = 'casterx.co'