2011-04-26 112 views
1

下面的代码:VB SQL UPDATE命令返回错误“运行时错误424:需要对象。”

Dim tr As Transactions 
Set tr = New Transactions 

Dim ID As Integer 
Dim name As String, username As String, password As String, activate As String 

name = cmbName.Value 
ID = tr.GetUserID(name) 

If (AccountActivated = True) Then 
    username = txtUsername.Value 
    password = txtPassword.Value 
    MsgBox name & " " & username & " " & password 
    activate = "Yes" 
Else 
    username = "" 
    password = "" 
    activate = "No" 
End If 

tr.UpdateAccount name, username, password, activate 'ERROR HERE: Object required 

这里; S功能我打电话:

Public Function UpdateAccount(ByVal name As String, ByVal username As String, ByVal password As String, ByVal activation As String) 
     Call connectDB 
     sSQL = "update User set Username = '" & username & "', Password = '" & password & "', AccountActivated = '" & activation & "' where Name = '" & name & "'" 
     MsgBox sSQL 
     db.Execute sSQL 
End Function 
+1

接收到确切的错误信息是什么?而且,你一定要使用'Parameters'来绑定'username','password','activation'和'user'的值。 – 2011-04-26 03:39:03

+0

运行时错误424:所需的对象 – Ali 2011-04-26 04:51:38

+0

这是什么语法,所以我可以尝试?谢谢! – Ali 2011-04-26 04:52:06

回答

1

为什么在这里使用 “DB”,并在其他问题 “CurrentDb”?始终使用明确的选项编码。

为什么功能,改变为分,我不记得这是否会导致问题。

但最重要的是,不要使用从文本框输入的文本来构建一个sql字符串,这使得它很容易成为SQL注入的受害者,使用参数。

+0

如果您从“功能”更改为“子”,则该线将变为红色。另外,如何为SQL执行参数呢?谢谢! – Ali 2011-04-26 08:17:25

+0

对不起..猜测dao不支持参数,你将不得不使用记录集。其他方式是清理字符串条带可能的sql注入代码像撇号 – ariel 2011-04-26 08:29:25

+0

但是你的“对象需要”可能是一个错误的变量名称。打开Option Explicit,你应该看到它。 – ariel 2011-04-26 08:30:46

相关问题