0

我这里有连接字符串:

sConn = "OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=VQPBOS;Data Source=" & "GHSI" & strcode

如果连接失败,如何编写“If 语句”以显示 Msgbox。因为用户需要输入“strcode”才能获得完整的服务器名称。错误信息是

Msgbox "Invalid Store Code. Check if the store code you have entered exists in any server"

如果它无法连接到该连接字符串。

我希望你能明白。谢谢 :)

4

1 回答 1

0

在这种情况下,您的ADODB.Connection对象会在ConnectExecute方法上引发错误。

您可以捕获该错误并使用消息框进行处理。

这是一个例子:

dim adoCN as new adodb.connection
dim sConn as string

on error goto err_Connection

sConn ="OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=VQPBOS;Data Source=" & "GHSI" & strcode

with adoCN
    .connectionstring=sconn
    .cursorlocation=aduseclient
    .open
end with
exit sub

err_Connection:
msgbox "The Connection failed to server: " &  "GHSI" & strcode & vbcrlf & vbcrlf & "Please check your connection settings", vbokonly+vbexclamation

您可以从Connectionstrings.com获取正确的连接字符串,也可以创建一个新的UDL 文件/Microsoft Data Link,在记事本中打开它,然后复制连接字符串

于 2013-06-17T09:22:12.777 回答