我这里有连接字符串:
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"
如果它无法连接到该连接字符串。
我希望你明白。谢谢:))
答案 0 :(得分:0)
在这种情况下, ADODB.Connection 对象会在连接或执行方法上引发错误。
您可以捕获该错误并使用消息框处理它。
以下是一个例子:
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数据链接,在记事本中打开它,然后复制连接字符串