|
这是鄙人抄改了本论坛上关于用友_U8取期初余额的一个函数,后来又陆续改好了期末、现金流量、发生额函数,后来发现,由于数据库连接字符串在每个函数里都要写一遍,而且更换了电脑还要重新修改连接用友服务器的IP,非常麻烦,请诸位大侠给修订一下,是不是可以把连接SQL数据库的代码(下面红字部分)“公共化”,然后,如有可能,能否把这个数据库连接设置成在某SHEET表内取值呢?谢了。
'取科目的期初值
'参数cCode:科目代码,Timevalue :时间值,如1月,2月等, YearName可选 默认为系统的年份
'TimeType时间类别 分为 年和月默认为月,Account可选 帐套号 默认为 "901"
'
Function qc(cCode, Timevalue, Optional YearName As String, Optional TimeType As String = "月", Optional Account As String = "901")
Const connstr = "driver={SQL Server}; server=192.168.0.2;uid=sa;pwd="
Dim conn As ADODB.Connection
Dim rst, rs As ADODB.Recordset
Dim newbook As Workbook
Dim csqlstr As String
qc = 0
If Trim(cCode) = "" Then Exit Function
If Trim(Account) = "" Then Exit Function
If Trim(YearName) = "" Then YearName = Format(Now(), "yyyy")
If Trim(Timevalue) = "" Then Exit Function
If Trim(TimeType) = "年" Then Timevalue = 1
Set conn = New ADODB.Connection
With conn
.ConnectionString = connstr & ";database=UFDATA" & "_" & Trim(Account) & "_" & Trim(YearName)
.Open strConn
End With
csqlstr = "SELECT sum((CASE WHEN gl_accsum.cbegind_c<>'贷' THEN gl_accsum.mb ELSE -gl_accsum.mb End))" & _
" AS SumVal " & _
" FROM code INNER JOIN gl_accsum ON code.ccode = gl_accsum.ccode " & _
" WHERE gl_accsum.iperiod = " & Timevalue & " AND gl_accsum.ccode = " & SqlStr(cCode)
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = conn
.Open csqlstr
End With
qc = rst.Fields(0).Value
Set rst = Nothing
conn.Close
Set conn = Nothing
End Function
|
|