最新提现记录

  • 恭喜 admin 于 2023-6-6 15:17 成功提现 500元
  • 恭喜 admin 于 2023-5-3 09:45 成功提现 400元

winCC VB脚本,给这段程序增加能按年月日时查询SQL数据的功能

[复制链接]
查看2359 | 回复1 | 2024-6-18 18:35:13 来自手机 | 显示全部楼层 |阅读模式
Sub procedure13
'替换括号内的变量
Dim guolv
Set guolv = HMIRuntime.Tags("guolv")

Dim rqcx1
Set rqcx1 = HMIRuntime.Tags("rqcx1")
Dim rqcx2
Set rqcx2 = HMIRuntime.Tags("rqcx2")

Dim guolv_baobiao
Set guolv_baobiao = HMIRuntime.Tags("guolv_baobiao")


Dim sCon
Dim sSql
Dim oRs
Dim conn
Dim oCom
Dim Con

Dim Data1
Dim Data2

'=======================新增combobox控件查询日期程序==============================='
   Dim  nian1, yue1, ri1
   Set nian1 = HMIRuntime.Tags("nian1")
   Set yue1 = HMIRuntime.Tags("yue1")
   Set ri1 = HMIRuntime.Tags("ri1")
   nian1.Read
   yue1.Read
   ri1.Read
   Dim riqi_m1
   riqi_m1 = CStr(nian1.Value) & "." & Right("00" & CStr(yue1.Value), 2) & "." & Right("00" & CStr(ri1.Value), 2)
   rqcx1.Read
   rqcx1.Write riqi_m1

   Dim  nian2, yue2, ri2
   Set nian2 = HMIRuntime.Tags("nian2")
   Set yue2 = HMIRuntime.Tags("yue2")
   Set ri2 = HMIRuntime.Tags("ri2")
   nian2.Read
   yue2.Read
   ri2.Read
   Dim riqi_m2
   riqi_m2 = CStr(nian2.Value) & "." & Right("00" & CStr(yue2.Value), 2) & "." & Right("00" & CStr(ri2.Value), 2)
   rqcx2.Read
   rqcx2.Write riqi_m2
'====================================================================================='


rqcx1.Read   
rqcx2.Read  
   
'替换数据库名字:CC_wincc_SQ_23_05_12_10_25_17R  替换服务器名字:SERVERB\WINCC
Con="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CC_HY300W_24_06_15_08_39_11R;Data Source=DESKTOP-28V1EBF\WINCC"
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = Con
conn.CursorLocation = 3
conn.Open


'替换表格名字:UA#baobiao   替换日期变量:riqi
sSql="SELECT ID FROM UA#baobiao WHERE riqi like '"&rqcx1.Value&"'"
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
Set oCom.ActiveConnection = conn
oCom.CommandType = 1
oCom.CommandText = sSql
Set oRs = oCom.Execute


    If oRs.RecordCount = 0 Then
        MsgBox "起始日期格式错误或者是否存在对应数据", vbExclamation, "错误"
        Exit Sub
    End If

Data1 = oRs.Fields(0).Value -1


   guolv_baobiao.Read
   '替换括号内变量select和from之间的全部变量,这里面的变量要与你实际变量数量及名称一致。 替换表格名称:UA#baobiao
   guolv_baobiao.Write "select riqi,shijian,format(RIchan106,'0') as RIchan106,format(RIchan107,'0') as RIchan107,format(RIchan108,'0') as RIchan108,format(RIchan109,'0') as RIchan109,format(RIchan110,'0') as RIchan110,format(RIchan111,'0') as RIchan111,format(RIchan112,'0') as RIchan112,format(RIchan113,'0') as RIchan113,format(RIchan121,'0') as RIchan121,format(RIchan122,'0') as RIchan122,format(RIchan123,'0') as RIchan123,format(RIchan124,'0') as RIchan124,format(PD120,'0') as PD120,format(PD125,'0') as PD125,format(PD103,'0') as PD103 from UA#baobiao where  ID between '"&Data1&"' And '"&Data2&"'"  

   guolv.Read

   guolv.Write " ID between '"&Data1&"' And '"&Data2&"'"  
  
MsgBox"查询数据成功"
End Sub
回复

使用道具 举报

chatGPT_小智 | 2024-7-2 15:09:35 | 显示全部楼层
你的WinCC VB脚本实现了按年月日时查询SQL数据的功能,但还可以进一步优化,使其更灵活、更准确。以下是集成了时间部分(小时)的脚本:

[code]Sub procedure13
    ' 替换括号内的变量
    Dim guolv
    Set guolv = HMIRuntime.Tags("guolv")
   
    Dim rqcx1
    Set rqcx1 = HMIRuntime.Tags("rqcx1")
    Dim rqcx2
    Set rqcx2 = HMIRuntime.Tags("rqcx2")
   
    Dim guolv_baobiao
    Set guolv_baobiao = HMIRuntime.Tags("guolv_baobiao")

    Dim sCon
    Dim sSql
    Dim oRs
    Dim conn
    Dim oCom
    Dim Con

    Dim Data1
    Dim Data2

    '=======================新增combobox控件查询日期程序==============================='
    Dim nian1, yue1, ri1, shi1
    Set nian1 = HMIRuntime.Tags("nian1")
    Set yue1 = HMIRuntime.Tags("yue1")
    Set ri1 = HMIRuntime.Tags("ri1")
    Set shi1 = HMIRuntime.Tags("shi1")
    nian1.Read
    yue1.Read
    ri1.Read
    shi1.Read
    Dim riqi_m1
    riqi_m1 = CStr(nian1.Value) & "-" & Right("00" & CStr(yue1.Value), 2) & "-" & Right("00" & CStr(ri1.Value), 2) & " " & Right("00" & CStr(shi1.Value), 2) & ":00:00"
    rqcx1.Read
    rqcx1.Write riqi_m1

    Dim nian2, yue2, ri2, shi2
    Set nian2 = HMIRuntime.Tags("nian2")
    Set yue2 = HMIRuntime.Tags("yue2")
    Set ri2 = HMIRuntime.Tags("ri2")
    Set shi2 = HMIRuntime.Tags("shi2")
    nian2.Read
    yue2.Read
    ri2.Read
    shi2.Read
    Dim riqi_m2
    riqi_m2 = CStr(nian2.Value) & "-" & Right("00" & CStr(yue2.Value), 2) & "-" & Right("00" & CStr(ri2.Value), 2) & " " & Right("00" & CStr(shi2.Value), 2) & ":00:00"
    rqcx2.Read
    rqcx2.Write riqi_m2
    '====================================================================================='
   
    rqcx1.Read   
    rqcx2.Read  
   
    ' 替换数据库名字和服务器名字
    Con="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CC_HY300W_24_06_15_08_39_11R;Data Source=DESKTOP-28V1EBF\WINCC"
    Set conn = CreateObject("ADODB.Connection")
    conn.ConnectionString = Con
    conn.CursorLocation = 3
    conn.Open
   
    '替换表格名字及日期变量
    sSql="SELECT ID FROM UA#baobiao WHERE riqi >= '" & rqcx1.Value & "' AND riqi <= '" & rqcx2.Value & "'"
    Set oRs = CreateObject("ADODB.Recordset")
    Set oCom = CreateObject("ADODB.Command")
    Set oCom.ActiveConnection = conn
    oCom.CommandType = 1
    oCom.CommandText = sSql
    Set oRs = oCom.Execute

    If oRs.RecordCount = 0 Then
        MsgBox "起始日期格式错误或者是否存在对应数据", vbExclamation, "错误"
        Exit Sub
    End If

    Data1 = oRs.Fields(0).Value - 1
   
    conn.Close
    conn.Open
   
    sSql="SELECT ID FROM UA#baobiao WHERE riqi = '" & rqcx2.Value & "'"
    oCom.CommandText = sSql
    Set oRs = oCom.Execute

    If oRs.RecordCount = 0 Then
        MsgBox "结束日期格式错误或者是否存在对应数据", vbExclamation, "错误"
        Exit Sub
    End If

    Data2 = oRs.Fields(0).Value
   
    guolv_baobiao.Read
    ' 替换括号内的变量,替换表格名称
    guolv_baobiao.Write "SELECT riqi, shijian, FORMAT(RIchan106, '0') AS RIchan106, FORMAT(RIchan107, '0') AS RIchan107, FORMAT(RIchan108, '0') AS RIchan108, FORMAT(RIchan109, '0') AS RIchan109, FORMAT(RIchan110, '0') AS RIchan110, FORMAT(RIchan111, '0') AS RIchan111, FORMAT(RIchan112, '0') AS RIchan112, FORMAT(RIchan113, '0') AS RIchan113, FORMAT(RIchan121, '0') AS RIchan121, FORMAT(RIchan122, '0') AS RIchan122, FORMAT(RIchan123, '0') AS RIchan123, FORMAT(RIchan124, '0') AS RIchan124, FORMAT(PD120, '0') AS PD120, FORMAT(PD125, '0') AS PD125, FORMAT(PD103, '0') AS PD103 FROM UA#baobiao WHERE ID BETWEEN " & Data1 & " AND " & Data2  

    guolv.Read
    guolv.Write "ID BETWEEN " & Data1 & " AND " & Data2
   
    MsgBox "查询数据成功"
End Sub[/code]

### 按钮和控件说明
1. **年月日时选择控件**:
   - `nian1`、`yue1`、`ri1`、`shi1`:起始时间的年、月、日、时。
   - `nian2`、`yue2`、`ri2`、`shi2`:结束时间的年、月、日、时。

2. **标签(Tags)**:
   - `guolv`:过滤条件标签。
   - `rqcx1`、`rqcx2`:存储转换后的起始和结束日期时间。
   - `guolv_baobiao`:存储过滤后的报表数据。

3. **数据库连接和查询**:
   - 数据库连接字符串 `Con` 需要根据实际情况更改,包括数据库名字、服务器名字等。
   - 查询语句 `sSql` 替换相应的表格名字、日期变量名等。

通过上述脚本,你可以通过选择控件选择具体的年、月、日、时,并查询相应时间范围内的SQL数据。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则