简体   繁体   English

从用户窗体VBA Excel输入SQL Server的用户名和密码

[英]Input username and password for SQL Server from userform VBA Excel

I want to input username and password SQL Server from userform VBA Excel, but I don't understand how to do that. 我想从用户窗体VBA Excel输入用户名和密码SQL Server,但我不知道该怎么做。 So I create code like this: 所以我创建这样的代码:

Sub OPenCOnn()    
   Set cnn = New ADODB.Connection
   cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=172.20.20.20;Initial Catalog=bank;User ID=" & txtUser.Text & ";Password=" & txtPass.Text & ";"
End Sub

But its didn't work. 但是它没有用。 I receive the below error: 我收到以下错误:

run time error, object required 运行时错误,需要对象

您需要像这样的字符串周围加上单引号:

cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=172.20.20.20;Initial Catalog=bank;User ID='" & txtUser.Text & "';Password='" & txtPass.Text & "';"

run time error, object required 运行时错误,需要对象

You are getting that error because your code cannot find txtUser and txtPass 您收到该错误,因为您的代码找不到txtUsertxtPass

Ensure the textboxes are there. 确保文本框在那里。

在此处输入图片说明

Use Option Explicit on the top of your code and then you will notice that it will highlight txtUser and say that Variable not defined . 在代码顶部使用Option Explicit ,然后您会注意到它将突出显示txtUser并说Variable not defined Of Course you will have to also define cnn as Dim cnn As ADODB.Connection 当然,您还必须将cnn定义为Dim cnn As ADODB.Connection

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM