找回密码
 注册账号

QQ登录

只需一步,快速开始

手机号码,快捷登录

手机号码,快捷登录

初学者课程:T3自学|T6自学|U8自学软件下载课件下载工具下载资料:通资料|U8资料|NC|培训|年结积分规则 | 使用常见问题Q&A
知识库:U8 | | NC | U9 | OA | 政务U8|U9|NCC|NC65|NC65客开|NCC客开新手必读 | 任务 | 快速增金币用友QQ群[微信群]
查看: 6362|回复: 8

[转帖] 用vba取用友后台的数据。[转帖]

  [复制链接]
发表于 2009-12-5 16:08:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册账号

×
我不是会计,因工作需要,做了三个函数, 基本上可以搞定 资产负债表 和利润分配表。因为现金流量表我还不会做,所以还没做相关的函数,谁教会我怎么做,我把相关的函数也补齐。 '连接数据库的参数,driver 连sql server的常数不用改 其中 server为数据库所在服务器的机器名或IP地址 'uid为sql server的帐号名称, pwd为相应帐号的密码 Const connstr = "driver={SQL Server}; server=202.0.0.2;uid=sa;pwd=123pwd321" Dim conn As ADODB.Connection Dim rst, rs As ADODB.Recordset Dim NewBook As Workbook ' '这是一段测试代码,可以取科目表 ' Sub getdatatest() Dim i As Integer Set conn = New ADODB.Connection With conn .ConnectionString = connstr & ";database=UFDATA_003_2003" .Open strConn End With Set rst = New ADODB.Recordset With rst .ActiveConnection = conn .Open "SELECT A.ccode, B.ccode_name AS code_name, A.iperiod, A.mb, A.md, A.mc, A.me " & _ " FROM GL_accsum A INNER JOIN " & _ " code B ON A.ccode = B.ccode " End With Set NewBook = Workbooks.Add For i = 0 To rst.Fields.Count - 1 NewBook.Sheets(1).Range("a1").Offset(0, i).Value = rst.Fields(i).Name Next i NewBook.Sheets(1).Range("a2").CopyFromRecordset rst Set rst = Nothing conn.Close Set conn = Nothing End Sub ' 'sql语句中字符变量加引号的函数 ' Function SqlStr(data) SqlStr = "'" & Replace(data, "'", "''") & "'" End Function ' '取科目的期末值 '参数cCode:科目代码, TimeValue :时间值,如1月,2月等, YearName可选 默认为系统的年份, Account可选 帐套号 默认为 "003" ' Function qm(cCode, TimeValue, Optional YearName As String, Optional Account As String = "003") Dim csqlstr As String qm = 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.me ELSE - a.me 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 qm = rst.Fields(0).Value Set rst = Nothing conn.Close Set conn = Nothing End Function ' '取科目的期初值 '参数cCode:科目代码,TimeValue :时间值,如1月,2月等, YearName可选 默认为系统的年份 'TimeType时间类别 分为 年和月默认为月,Account可选 帐套号 默认为 "003" ' Function qc(cCode, TimeValue, Optional YearName As String, Optional TimeType As String = "月", Optional Account As String = "003") 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 ' '取科目的发生数 '参数cCode:科目代码,TimeValue :时间值,如1月,2月等, Direction 为借 代 方向,YearName可选 默认为系统的年份 'ifcheck 是否记帐 默认为0没记帐,Account可选 帐套号 默认为 "003" ' Function fs(cCode, TimeValue, Direction, Optional TimeType As String = "月", Optional YearName As String, Optional ifcheck As Integer = 0, Optional Account As String = "003") Dim csqlstr As String fs = 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.md ELSE a.mc 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 fs = 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
发表于 2009-12-6 15:46:24 | 显示全部楼层
呵呵,看看,这几天研究这个呢。
发表于 2009-12-6 16:36:01 | 显示全部楼层
呵呵,一般实施人员或是售前人员都用这方法
发表于 2009-12-6 17:45:27 | 显示全部楼层
不错,学习一下经验!
发表于 2009-12-6 23:33:51 | 显示全部楼层
挺好的,学习一下~~
发表于 2010-2-11 22:30:27 | 显示全部楼层
好东西,先收藏,以后慢慢学习。谢谢楼主
发表于 2010-2-21 01:33:58 | 显示全部楼层
留个联系方式吧.一起讨论下
发表于 2010-12-27 16:24:36 | 显示全部楼层
好东西,先收藏,以后慢慢学习。谢谢楼主
发表于 2011-7-16 23:39:13 | 显示全部楼层
不错,学习一下经验!
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

QQ|站长微信|Archiver|手机版|小黑屋|用友之家 ( 蜀ICP备07505338号|51072502110008 )

GMT+8, 2024-11-25 00:28 , Processed in 0.070104 second(s), 11 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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