从vb6将数据插入数据库?

时间:2014-05-22 14:08:12

标签: sql database ms-access vb6

我正在开发一个程序,它从listview对象中获取一些数据,并且应该将数据插入到数据库的特定字段中。我正在使用vb6和ms access 2003数据库。我不完全确定如何连接到vb6中的数据库,虽然我现在有这个代码

Option Explicit
Public dbCaseGoods As DAO.Database
Dim rs As DAO.Recordset
Dim Barcode() As String
Dim linenumber As Integer
Dim strinsert As String



Private Sub btnDone_Click()
linenumber = 1
strinsert = "INSERT INTO Work_Order (PO_Number,CNC_Code,Line_No) VALUES ('" & "PO NUMBER" & "', '" & "CNC CODE" & "', '" & "LINE NUMBER" & "')""
'strinsert = "INSERT INTO Work_Order (PO_Number,CNC_Code,Line_No) VALUES ('" & ListView1.ListItems(linenumber).Text & "' , '" &     ListView1.ListItems(linenumber).SubItems(1) & "','" &     ListView1.ListItems(linenumber).SubItems(2) & "')""
Set rs = dbCaseGoods.execute(strinsert, dbfailonerror)

txtbarcode.SetFocus
End Sub

Private Sub Command1_Click()
Form2.Show
End Sub

Private Sub Form_Load()
Set dbCaseGoods = OpenDatabase("I:\Casegoods\database\Contract_Casegoods.mdb")

ListView1.ColumnHeaders.Add Text:="P.O#", Width:=ListView1.Width / 3
ListView1.ColumnHeaders.Add Text:="Cnc Code", Width:=ListView1.Width / 3
ListView1.ColumnHeaders.Add Text:="Line #", Width:=ListView1.Width / 8
End Sub



Private Sub txtbarcode_KeyPress(KeyAscii As Integer)
Dim li As ListItem


Barcode = Split(txtbarcode.Text, "-")
On Error Resume Next
   If KeyAscii = vbKeyReturn Then
   Set li = ListView1.ListItems.Add(, , Barcode(0))
   li.SubItems(1) = Barcode(1)
   li.SubItems(2) = Barcode(2)
   txtbarcode.Text = ""
   Command2.Caption = ListView1.ListItems(1).Text
   Command3.Caption = ListView1.ListItems(1).SubItems(1)
   Command4.Caption = ListView1.ListItems(1).SubItems(2)
End If

End Sub

当我尝试按下按钮将一行数据插入数据库时​​,我现在得到的是Expected function or variable错误,.Execute突出显示Set rs = dbCaseGoods.execute(strinsert, dbfailonerror) < / p>

1 个答案:

答案 0 :(得分:2)

INSERT查询不会返回记录,所以

Set rs = dbCaseGoods.execute(strinsert, dbfailonerror)

不起作用。只需使用

dbCaseGoods.Execute strinsert, dbFailOnError

您还应该考虑使用parameter queries而不是“粘合”SQL语句。