ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3390|回复: 0

[求助] 求助,用友取数不能正确取非末级科目数量

[复制链接]

TA的精华主题

TA的得分主题

发表于 2006-5-11 15:02 | 显示全部楼层 |阅读模式
在论坛里找到取用友数据的函数,想修改一下取数量,取发生额数量不管末级非末级都能正常取值,但期初、期末函数只能正确取末级科目,取的非末级科目数量只要与末级科目年初数量相加,结果才是正确的。      
那位高手能帮帮小弟。

'连接数据库的参数,driver 连sql server的常数不用改 其中 server为数据库所在服务器的机器名或IP地址
'uid为sql server的帐号名称, pwd为相应帐号的密码
Const connstr = "driver={SQL Server}; server=Cw;uid=sa;pwd="
Dim conn As ADODB.Connection
Dim rst, rs As ADODB.Recordset
Dim NewBook As Workbook
'

'sql语句中字符变量加引号的函数
'
Function SqlStr(data)
  
   SqlStr = "'" & Replace(data, "'", "''") & "'"
  
End Function


'取科目的期末值数量
'参数cCode:科目代码, TimeValue :时间值,如1月,2月等, YearName可选 默认为系统的年份, Account可选 帐套号 默认为 "001"
'
Function Slqm(cCode, TimeValue, Optional YearName As String, Optional Account As String = "001")
    Dim csqlstr As String
    Slqm = 0
    If Trim(cCode) = "" Then Exit Function
    If Trim(TimeValue) = "" Then Exit Function
    If Trim(Account) = "" Then Exit Function
    If Trim(YearName) = "" Then YearName = Format(Now(), "yyyy")
    Set conn = New ADODB.Connection
    With conn
         .ConnectionString = connstr & ";database=UFDATA" & "_" & Trim(Account) & "_" & Trim(YearName)
         .Open strConn
    End With
   
    csqlstr = "SELECT SUM((CASE WHEN a.cendd_c <> '贷' THEN a.ne_s ELSE - a.ne_s END))" & _
                        " AS SumVal " & _
               " FROM code b INNER JOIN " & _
               " gl_accsum a ON b.ccode = a.ccode " & _
               " WHERE a.iperiod = " & TimeValue & " AND a.ccode = " & SqlStr(cCode)
   
    Set rst = New ADODB.Recordset
    With rst
         .ActiveConnection = conn
         .Open csqlstr
    End With
    Slqm = rst.Fields(0).Value
    Set rst = Nothing
    conn.Close
    Set conn = Nothing
End Function

'取科目的期初值数量
'参数cCode:科目代码,TimeValue :时间值,如1月,2月等, YearName可选 默认为系统的年份
'TimeType时间类别 分为 年和月默认为月,Account可选 帐套号 默认为 "001"
'
Function Slqc(cCode, TimeValue, Optional YearName As String, Optional TimeType As String = "月", Optional Account As String = "001")
    Dim csqlstr As String
    Slqc = 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.nb_s ELSE -gl_accsum.nb_s 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
    Slqc = rst.Fields(0).Value
    Set rst = Nothing
    conn.Close
    Set conn = Nothing
End Function


'取科目的发生数数量
'参数cCode:科目代码,TimeValue :时间值,如1月,2月等, Direction 为借 代 方向,YearName可选 默认为系统的年份,TimeType时间类别 分为 年和月默认为月
'ifcheck 是否记帐 默认为0没记帐,Account可选 帐套号 默认为 "001"
'
Function Slfs(cCode, TimeValue, Direction, Optional TimeType As String = "月", Optional YearName As String, Optional ifcheck As Integer = 0, Optional Account As String = "001")
   Dim csqlstr As String
   Slfs = 0
   If Trim(cCode) = "" Then Exit Function
   If Trim(TimeType) = "" Then Exit Function
   If Trim(TimeValue) = "" Then Exit Function
   If Trim(Direction) = "" Then Exit Function
   If Trim(Account) = "" Then Exit Function
   If Trim(YearName) = "" Then YearName = Format(Now(), "yyyy")
   If Trim(ifcheck) = "" Then Exit Function
   Set conn = New ADODB.Connection
    With conn
         .ConnectionString = connstr & ";database=UFDATA" & "_" & Trim(Account) & "_" & Trim(YearName)
         .Open strConn
    End With
   
   csqlstr = " SELECT sum((CASE when "
   If Direction = "借" Then
          csqlstr = csqlstr & " 1=1 "
   Else
          csqlstr = csqlstr & " 1=0 "
   End If
   csqlstr = csqlstr & " THEN a.nd_s ELSE a.nc_s End)) as SumVal FROM code b INNER JOIN gl_accvouch a ON b.ccode = a.ccode "
   If TimeType = "年" Then
         csqlstr = csqlstr & " where a.iperiod>=1 and a.iperiod<=" & TimeValue
      Else
         csqlstr = csqlstr & " where a.iperiod=" & TimeValue
   End If
   csqlstr = csqlstr & " AND a.iflag is null AND a.ccode "
   If ifbend(conn, cCode) = 1 Then
      csqlstr = csqlstr & "=" & SqlStr(cCode)
   Else
      csqlstr = csqlstr & "like " & SqlStr(cCode & "%")
   End If
   If ifcheck = 1 Then
     csqlstr = csqlstr & " AND a.ibook=" & ifcheck
   End If
   
   Set rst = New ADODB.Recordset
   With rst
         .ActiveConnection = conn
         .Open csqlstr
   End With
   Slfs = rst.Fields(0).Value
   Set rst = Nothing
   conn.Close
   Set conn = Nothing
End Function
'
这个是判断科目是否为末级的函数
'
Function ifbend(conn, cCode)
   Dim strsql As String
   strsql = " select bend from code where ccode=" & SqlStr(cCode)
   Set rs = New ADODB.Recordset
   With rs
       .ActiveConnection = conn
       .Open strsql
   End With
   ifbend = rs.Fields(0).Value
   Set rs = Nothing
End Function

cJqQhr5O.rar (10.85 KB, 下载次数: 16)
[此贴子已经被作者于2006-5-11 15:10:35编辑过]
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-11 11:21 , Processed in 0.020068 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表