使用VBA,ADO的参数化查询问题,在线等!
在做一个登陆见面现在遇到一个编译出错的问题,大家帮忙看看。
Private Sub cmdlogon_Click()
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.Provider = "MSDASQL"
cnn.ConnectionString = "driver={SQL Server};" & _
"server=MBMFLASH\ITP;uid=HC;pwd=77887788;database=LAB"
cnn.Open
If cnn.State = adStateOpen Then
MsgBox "Welcome to LAB database!"
Else
MsgBox "Sorry. No LAB database."
End If
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
'指定cmd的当前连接为conn
cmd.ActiveConnection = cnn
'设置要执行的是存储过程average
cmd.CommandType = adCmdText
cmd.CommandText = "select * from dbo.logon where username=? and password=?"
'注意有的地方是用[dbo].logon来表示,这点需要待认证
Dim par As ADODB.Parameter
Set par = cmd.CreateParameter("username", adLongVarChar, adParamInput, 10, Val(nameselect.Text))
cmd.Parameters.Append par
Set par = cmd.CreateParameter("password", adInteger, adParamInput, 10, Val(code.Text))
cmd.Parameters.Append par
Dim rst As New ADODB.Recordset
Set rst = cmd.Execute
错误出现在“ Set rst = cmd.Execute”这一行,错误提示为
[解决办法]
参数类型设置得不对。