20170112xlVBA查询SQL

Sub NextSeven_CodeFrame()
‘应用程序设置
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual

    ‘错误处理
    On Error GoTo ErrHandler

    ‘计时器
    Dim StartTime, UsedTime As Variant
    StartTime = VBA.Timer

    ‘变量声明
    Dim wb As Workbook
    Dim sht As Worksheet
    Dim Rng As Range
    Dim Arr As Variant
    Dim EndRow As Long

    Dim oSht As Worksheet
    Dim DataPath As String
    Dim SQL As String
    Dim EndDate As Date
    Dim StartDate As Date
    Dim Client As String

    ‘实例化对象
    Set wb = Application.ThisWorkbook
    Set sht = wb.Worksheets("凭证录入")
    Set oSht = wb.Worksheets("客户明细")
    DataPath = wb.FullName

    usertxt = Application.InputBox("请输入开始日期", "开始日期", , , , , , 2)
    If usertxt = False Then Exit Sub
    StartDate = Format(CDate(usertxt), "yyyy-mm-dd")

    usertxt = Application.InputBox("请输入结束日期", "结束日期", , , , , , 2)
    If usertxt = False Then Exit Sub
    EndDate = Format(CDate(usertxt), "yyyy-mm-dd")

    usertxt = Application.InputBox("请输入客户姓名", "客户姓名", , , , , , 2)
    If usertxt = False Then Exit Sub
    Client = CStr(usertxt)

    oSht.UsedRange.Offset(1).Clear
    Set Rng = oSht.Range("A2")

    SQL = "SELECT * FROM [" & sht.Name & "$A3:V] WHERE  出货客户=‘" & Client & "‘ AND ( 出货日期 Between  #" & StartDate & "# AND #" & EndDate & "#  )"
    SQL = SQL & " ORDER BY 型号 ASC"
    If RecordExistsRunSQL(DataPath, SQL) = True Then
        GetRecordSetIntoRange DataPath, SQL, Rng
    End If

    ‘运行耗时
    UsedTime = VBA.Timer - StartTime
    MsgBox "本次运行耗时:" & Format(UsedTime, "0.0000000秒")
ErrorExit:        ‘错误处理结束,开始环境清理
    Set wb = Nothing
    Set sht = Nothing
    Set Rng = Nothing

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
    Exit Sub
ErrHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Description & "!", vbCritical, "错误提示!"
        ‘Debug.Print Err.Description
        Err.Clear
        Resume ErrorExit
    End If
End Sub

Public Sub GetRecordSetIntoRange(ByVal DataPath As String, ByVal SQL As String, ByVal Rng As Range)
‘对传入数据源地址进行判断
    If Len(DataPath) = 0 Or Len(Dir(DataPath)) = 0 Then _
 MsgBox "数据源地址为空或者数据源文件不存在!", vbInformation, "NS Excel Studio": Exit Sub
    ‘对传入SQL语句进行判断
    If Len(SQL) = 0 Then _
 MsgBox "SQL语句不能为空!", vbInformation, "NS Excel Studio": Exit Sub
    ‘对象变量声明
    Dim cnn As Object
    Dim rs As Object
    ‘数据库引擎——Excel作为数据源
    Const DATA_ENGINE As String = "Provider=Microsoft.jet.OLEDB.4.0;" & _
          "Extended Properties=‘Excel 8.0;HDR=YES;IMEX=2‘; Data Source= "
    ‘创建ADO Connection 连接器 实例
    Set cnn = CreateObject("ADODB.Connection")
    ‘On Error Resume Next
    ‘创建 ADO RecordSet  记录集 实例
    Set rs = CreateObject("ADODB.RecordSet")
    ‘连接数据源
    cnn.Open DATA_ENGINE & DataPath
    ‘执行查询 返回记录集
    rs.Open SQL, cnn, 1, 1
    ‘Set RS = CNN.Execute(SQL)
    ‘复制记录集到指定Range
    Rng.CopyFromRecordset rs
    ‘关闭记录集
    rs.Close
    ‘关闭连接器
    cnn.Close
    ‘释放对象
    Set rs = Nothing
    Set cnn = Nothing
End Sub
Public Function RecordExistsRunSQL(ByVal DataPath As String, ByVal SQL As String) As Boolean
‘对传入数据源地址进行判断
    If Len(DataPath) = 0 Or Len(Dir(DataPath)) = 0 Then
        RecordExistsRunSQL = False
        MsgBox "数据源地址为空或者数据源文件不存在!", vbInformation, "NS Excel Studio"
        Exit Function
    End If
    ‘对传入SQL语句进行判断
    If Len(SQL) = 0 Then
        RecordExistsRunSQL = False
        MsgBox "SQL语句不能为空!", vbInformation, "NS Excel Studio"
        Exit Function
    End If
    ‘对象变量声明
    Dim cnn As Object
    Dim rs As Object
    ‘数据库引擎——Excel作为数据源
    ‘Const DATA_ENGINE As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
         ‘ "Extended Properties=‘Excel 12.0;HDR=YES;IMEX=2‘; Data Source= "

             ‘数据库引擎——Excel作为数据源
    Const DATA_ENGINE As String = "Provider=Microsoft.jet.OLEDB.4.0;" & _
          "Extended Properties=‘Excel 8.0;HDR=YES;IMEX=2‘; Data Source= "

    ‘创建ADO Connection 连接器 实例
    Set cnn = CreateObject("ADODB.Connection")
    On Error Resume Next
    ‘创建 ADO RecordSet  记录集 实例
    Set rs = CreateObject("ADODB.RecordSet")
    ‘连接数据源
    cnn.Open DATA_ENGINE & DataPath
    ‘执行查询 返回记录集
    rs.Open SQL, cnn, 1, 1
    ‘返回函数结果
    If rs.RecordCount > 0 Then
        RecordExistsRunSQL = True
    Else
        RecordExistsRunSQL = False
    End If
    ‘关闭记录集
    rs.Close
    ‘关闭连接器
    cnn.Close
    ‘释放对象
    Set rs = Nothing
    Set cnn = Nothing
End Function

  

时间: 2024-10-06 06:00:23

20170112xlVBA查询SQL的相关文章

Oracle EBS-SQL (SYS-1): sysadmin_用户职责查询.sql

select fu.user_name 用户名, fu.description 用户说明, frv.RESPONSIBILITY_NAME 职责名称, REQUEST_GROUP_NAME 报表组, (select  ppf.FULL_NAME from apps.per_people_f  ppf where fu.employee_id = ppf.PERSON_ID and trunc(sysdate)  between  trunc(ppf.EFFECTIVE_START_DATE) a

Oracle EBS-SQL (SYS-2): sys_在线用户查询.sql

SELECT fs.USER_NAME,       fu.description,       fs.RESPONSIBILITY_NAME,       fs.USER_FORM_NAME,       fs.TIME,       fs.RESP_APPL_ID,       fs.RESPONSIBILITY_ID,       fs.FORM_ID,       fs.FORM_APPL_ID  FROM FND_SIGNON_AUDIT_VIEW fs,       fnd_user

Oracle EBS-SQL (SYS-3):sys_人员用户名对应关系查询.sql

select fu.user_name 用户名,       fu.description 描述,       (select ppf.FULL_NAME          from per_people_f ppf         where ppf.PERSON_ID = fu.employee_id           and trunc(sysdate) between trunc(ppf.EFFECTIVE_START_DATE) and               trunc(ppf

Oracle EBS-SQL (SYS-10):锁定表查询.sql

/*死锁查询-1*/ SELECT o.object_name, l.session_id,l.process, l.locked_mode FROM v$locked_object l , dba_objects o WHERE o.object_id=l.object_id ------------------------------------------------ /*死锁查询-2*/ select GL.SESSION_ID,         do.object_name,     

Oracle EBS-SQL (SYS-7):表单个性化查询.sql

animation-timing-function animation-timing-function 规定动画的速度曲线.速度曲线定义动画从一套 CSS 样式变为另一套所用的时间.在平常的取值中,主要有以下几个: 值 描述 linear 动画从头到尾的速度是相同的. ease 默认.动画以低速开始,然后加快,在结束前变慢. ease-in 动画以低速开始. ease-out 动画以低速结束. ease-in-out 动画以低速开始和结束. cubic-bezier(n,n,n,n) 在 cub

Oracle EBS-SQL (SYS-5):sys_配置文件查询.sql

select    distinct l.profile_option_name,             v.profile_option_value,             fu.user_nameform     applsys.fnd_profile_option_values v,            apps.fnd_profile_options_vl          l,            apps.fnd_user                           

分页用到的子查询sql语句

说明(2017-8-31 23:30:22): 1. 分页用到的子查询sql语句 2. 记住的意思就是背过^_^ 3. 还有一个top语句,查一查

循环查询sql带逗号(,)分隔的数据

? 1 2 3 4 5 6 select * from sys_role_list where id in (select c from (with test as (select roleid c from sys_role_info where id=1) select substr(t.ca,instr(t.ca, ',', 1, c.lv) + 1,instr(t.ca, ',', 1, c.lv + 1) - (instr(t.ca, ',', 1, c.lv) + 1)) AS c

查询sql时 改变列原来的类型

查询sql时 改变列原来的类型SELECT   create_time   FROM    ciimp_fin_invoice               本来要查询的 是 创建时间 时间类型 结果为 2017-03-29 03:29:40 SELECT create_time+'' FROM ciimp_fin_invoice 在sql列中 +'' 将其列改变为字符串类型,输出的结果为:20170329032940    + 可扩展 字符串.数字.时间 类型都可以