将VB6中的文本框和组合框值插入到MS Access 2010中 [英] Insert textboxes and combo box values in VB6 to MS access 2010
问题描述
这是我的代码;
选项明确
公共骗局新ADODB.Connection
公开rs作为新的ADODB.Recordset
------------------------------------ -----------------------------------------
私人Sub cmdadd_Click()
Dim res As String
Dim SQLSTR As String
Dim ConString As String
Set rs =新ADODB.Recordset
ConString =provider = Microsoft.jet.oledb.4.0;数据源=D:\ My Project VB6 \Tubewell.accdb
设置con =新ADODB.Connection
con.ConnectionString = ConString
res = MsgBox(你要添加这条记录吗?,vbQuestion + vbYesNo,添加员工)
如果res = vbYes那么
con.Open
SQLSTR =插入Employee_Details(EmpNo,Employee_Name,Status)值( '& txtempno&','& empname&','& Combo1&')
con.Execute SQLSTR
con。关闭
设置con = Nothi ng $>
结束如果
结束子
当我运行项目时我得到了;
预期:声明结束错误信息。
请帮助我。
我尝试过:
这是我的代码;
选项明确
Public con As New ADODB.Connection
Public rs As New ADODB.Recordset
---------- -------------------------------------------------- -----------------
Private Sub cmdadd_Click()
Dim res As String
Dim SQLSTR As String
Dim ConString As String
设置rs =新ADODB.Recordset
ConString =provider = Microsoft.jet.oledb。 4.0;数据源=D:\ My Project VB6 \Tubewell.accdb
设置con =新ADODB.Connection
con.ConnectionString = ConString
res = MsgBox(你要添加这条记录吗?,vbQuestion + vbYesNo,添加员工)
如果res = vb是的然后
con.Open
SQLSTR =插入Employee_Details(EmpNo,Employee_Name,Status)值('& txtempno&','& empname& ;','& Combo1&')
con.Execute SQLSTR
con.Close
设置con = Nothing
结束如果
结束子
当我运行项目时我得到了;
预期:声明结束错误信息。
请帮助我。
Here is my code;
Option Explicit
Public con As New ADODB.Connection
Public rs As New ADODB.Recordset
-----------------------------------------------------------------------------
Private Sub cmdadd_Click()
Dim res As String
Dim SQLSTR As String
Dim ConString As String
Set rs = New ADODB.Recordset
ConString= "provider=Microsoft.jet.oledb.4.0;Data Source="D:\My Project VB6\Tubewell.accdb"
Set con = New ADODB.Connection
con.ConnectionString = ConString
res = MsgBox("Do you want to add this record?", vbQuestion + vbYesNo, "Add Employee")
If res = vbYes Then
con.Open
SQLSTR = "Insert into Employee_Details(EmpNo,Employee_Name,Status)values('"&txtempno&"','"&empname&"','"&Combo1&"')"
con.Execute SQLSTR
con.Close
Set con = Nothing
End If
End Sub
When I am running the project I am getting;
"Expected: End of Statement" Error message.
Please help me.
What I have tried:
Here is my code;
Option Explicit
Public con As New ADODB.Connection
Public rs As New ADODB.Recordset
-----------------------------------------------------------------------------
Private Sub cmdadd_Click()
Dim res As String
Dim SQLSTR As String
Dim ConString As String
Set rs = New ADODB.Recordset
ConString= "provider=Microsoft.jet.oledb.4.0;Data Source="D:\My Project VB6\Tubewell.accdb"
Set con = New ADODB.Connection
con.ConnectionString = ConString
res = MsgBox("Do you want to add this record?", vbQuestion + vbYesNo, "Add Employee")
If res = vbYes Then
con.Open
SQLSTR = "Insert into Employee_Details(EmpNo,Employee_Name,Status)values('"&txtempno&"','"&empname&"','"&Combo1&"')"
con.Execute SQLSTR
con.Close
Set con = Nothing
End If
End Sub
When I am running the project I am getting;
"Expected: End of Statement" Error message.
Please help me.
推荐答案
尝试用分号终止语句,并用空格括住VALUE关键字:
Try terminating the statement with a semicolon and enclose the VALUE keyword by spaces:
SQLSTR = "INSERT INTO Employee_Details (EmpNo,Employee_Name,Status) VALUES ('" & txtempno & "','" & empname & "','" & Combo1 & "');"
我还插入了额外的空格并使用全部大写关键字。这不是必要的,但可以提高可读性。
I have also inserted additional spaces and used all uppercase for keywords. This is not necessary but improves readability.
首先修复 SQL注入 [ ^ ]漏洞。
参数化查询并非完全 VBA就像在.NET中一样,但它并不太糟糕:
Append和CreateParameter方法示例(VB)| Microsoft Docs [ ^ ]
这样的事情应该有效:
Start by fixing the SQL Injection[^] vulnerability in your code.
Parameterizing queries isn't quite as simple in VBA as it is in .NET, but it's not too bad:
Append and CreateParameter Methods Example (VB) | Microsoft Docs[^]
Something like this should work:
Private Sub cmdadd_Click()
If MsgBox("Do you want to add this record?", vbQuestion + vbYesNo, "Add Employee") <> vbYes Then
Exit Sub
End If
Dim ConString As String
ConString= "provider=Microsoft.jet.oledb.4.0;Data Source=""D:\My Project VB6\Tubewell.accdb"""
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.ConnectionString = ConString
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandText = "INSERT INTO Employee_Details (EmpNo, Employee_Name, Status) VALUES (?, ?, ?)"
Dim p As ADODB.Parameter
Set p = cmd.CreateParameter(, adInteger, adParamInput, , txtempno.Text)
cmd.Parameters.Append p
Set p = cmd.CreateParameter(, adVarChar, adParamInput, 50, empname.Text)
cmd.Parameters.Append p
Set p = cmd.CreateParameter(, adVarChar, adParamInput, 10, Combo1.Value)
cmd.Parameters.Append p
con.Open
cmd.Execute
con.Close
End Sub
注意:您需要更改名称的参数大小和与数据库匹配的状态参数。
[ ^ ]
如何在没有技术术语的情况下解释SQL注入? |信息安全堆栈交换 [ ^ ]
查询参数化备忘单| OWASP [ ^ ]
NB: You'll need to change the parameter sizes for the name and status parameters to match the database.
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
不是你问题的解决方案,而是你遇到的另一个问题。
永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。
名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。
SQL注入 - 维基百科 [ ^ ]
SQL注入 [ ^ ]
按示例进行SQL注入攻击 [ ^ ]
PHP:SQL注入 - 手册 [ ^ ]
SQL注入预防备忘单 - OWASP [ ^ ]
-----
养成缩进代码的习惯,这有助于阅读:
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
-----
Get the habit to indent your code, it helps reading:
Option Explicit
Public con As New ADODB.Connection
Public rs As New ADODB.Recordset
-----------------------------------------------------------------------------
Private Sub cmdadd_Click()
Dim res As String
Dim SQLSTR As String
Dim ConString As String
Set rs = New ADODB.Recordset
ConString= "provider=Microsoft.jet.oledb.4.0;Data Source="D:\My Project VB6\Tubewell.accdb"
Set con = New ADODB.Connection
con.ConnectionString = ConString
res = MsgBox("Do you want to add this record?", vbQuestion + vbYesNo, "Add Employee")
If res = vbYes Then
con.Open
SQLSTR = "Insert into Employee_Details(EmpNo,Employee_Name,Status)values('"&txtempno&"','"&empname&"','"&Combo1&"')"
con.Execute SQLSTR
con.Close
Set con = Nothing
End If
End Sub
Office Automation Ltd. - Smart Indenter [ ^ ]
安装Sma rt Indenter in Office 2013-2016 [ ^ ]
这篇关于将VB6中的文本框和组合框值插入到MS Access 2010中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!