I want to input username and password SQL Server from userform VBA Excel, but I don't understand how to do that. 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
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
. Of Course you will have to also define cnn as Dim cnn As ADODB.Connection
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.