你好,以下是关于vb 自定义函数怎么定义和调用的问题!

Function conn() As Boolean
Dim adoconn As adocb.Connection
Set adoconn = New ADODB.Connection
t1 = GetFromIni("database", "server", ".\\thjl.ini")
Debug.Print t1
t2 = GetFromIni("database", "user", ".\\thjl.ini")
t3 = GetFromIni("database", "password", ".\\thjl.ini")
t4 = GetFromIni("database", "data", ".\\thjl.ini")
constr = "Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=" & t4 & ";Data Source=" & t1 & ""
adoconn.ConnectionString = constr
adoconn.CursorLocation = adUseClient
adoconn.Open constr, t2, t3
End Function

我定义的函数,VB连接sql,我定义的是不是有问题,该怎么调用呢

UYOU
浏览 291回答 2
2回答

杨魅力

修改一下你的代码改成function conn() As adodb.connectionDim adoconn As adocb.ConnectionSet adoconn = New ADODB.Connectiont1 = GetFromIni("database", "server", ".\\thjl.ini")Debug.Print t1t2 = GetFromIni("database", "user", ".\\thjl.ini")t3 = GetFromIni("database", "password", ".\\thjl.ini")t4 = GetFromIni("database", "data", ".\\thjl.ini")constr = "Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=" & t4 & ";Data Source=" & t1 & ""adoconn.ConnectionString = constradoconn.CursorLocation = adUseClientadoconn.Open constr, t2, t3conn = adoconnEnd function这样你可以在使用时如下rs.open sql,conn当然,你的代码还得改进,比如错误处理之类的.还有你也可以做成 property ,这样用起来更形象
打开App,查看更多内容
随时随地看视频慕课网APP