我正在编写关于访问添加和更新表中数据的代码,但它说“标准表达式中的数据不匹配”。你能告诉我我哪里错了吗? [英] I am writting code on access to add and update data from table but it says "data mismatch in criteria expression" can you please tell me where I am wrong?
本文介绍了我正在编写关于访问添加和更新表中数据的代码,但它说“标准表达式中的数据不匹配”。你能告诉我我哪里错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
选项比较数据库
Option Compare Database
Private Sub cmdAdd_Click()
If Me.txtPartNumb.Tag & "" = "" Then
CurrentDb.Execute "INSERT INTO PartList(PartNumb, PartName, PartCellIDFK)" & _
" VALUES (" & Me.txtPartNumb & ",'" & Me.txtPartName & "','" & Me.txtCellID & "')"
Else
CurrentDb.Execute "UPDATE PartList" & _
" SET PartNumb=" & Me.txtPartNumb & _
", PartName='" & Me.txtPartName & "'" & _
", PartCellIDFK='" & Me.txtCellID & "'" & _
" WHERE PartNumb=" & Me.txtPartNumb.Tag
End If
cmdClear_Click
partsearchsub.Form.Requery
End Sub
Private Sub cmdClear_Click()
Me.txtPartNumb = ""
Me.txtPartName = ""
Me.txtCellID = ""
Me.txtPartNumb.SetFocus
Me.cmdAdd.Caption = "Add"
Me.cmdEdit.Enabled = True
Me.txtPartNumb.Tag = ""
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Private Sub cmdDelete_Click()
If Not (Me.partsearchsub.Form.Recordset.EOF And Me.partsearchsub.Form.Recordset.BOF) Then
If MsgBox("Are you sure you want to delete this?", vbYesNo) = vbYes Then
CurrentDb.Execute "DELETE FROM PartList" & _
" WHERE PartNumb =" & Me.partsearchsub.Form.Recordset.Fields("PartNumb")
Me.partsearchsub.Form.Requery
End If
End If
End Sub
Private Sub cmdEdit_Click()
If Not (Me.partsearchsub.Form.Recordset.EOF And Me.partsearchsub.Form.Recordset.BOF) Then
With Me.partsearchsub.Form.Recordset
Me.txtPartNumb = .Fields("PartNumb")
Me.txtPartName = .Fields("PartName")
Me.txtCellID = .Fields("PartCellIDFK")
Me.txtPartNumb.Tag = .Fields("PartNumb")
Me.cmdEdit.Enabled = False
Me.cmdAdd.Caption = "Update"
End With
End If
End Sub
我尝试过:
我无法尝试很多东西因为我是初学者。但我观看了一段视频,我的表情与视频中描述的人完全一样。所以我迷路了,我不知道出了什么问题
What I have tried:
I could not be tried many things because I am a beginner in access. But I watched a video and my expressions are as exactly as the person describing in the video. So I am lost and I do not know what is wrong
推荐答案
对于初学者来说,停止这样做:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
其次,给定txtPartNumb,txtPartName和txtCellID是文本框,将它们连接成SQL字符串无论如何都不会起作用。
尝试:
For starters, stop doing it like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Second, given thet txtPartNumb, txtPartName, and txtCellID are text boxes, concatenating them into an SQL string won't work anyway.
Try:
Dim s as String = "---" & Me.txtPartNumb & "---"
Console.WriteLine(s)
这将给你带来的是:
What this will give you is:
---System.Windows.Forms.TextBox, Text: ABCD---
你需要在获取文本框的值时,请指定文本框的Text属性!
You need to specify the Text property of the text box when fetching it's value!
这篇关于我正在编写关于访问添加和更新表中数据的代码,但它说“标准表达式中的数据不匹配”。你能告诉我我哪里错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文