|
楼主 |
发表于 2020-1-17 08:38
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Function Find_Data(Record_Time As String, Table_num As String, Channel As String)
- If Len(Record_Time) < 11 Or Channel = Empty Then
- Find_Data = "" '时间不完整直接停止执行,提高速度
- Exit Function
- End If
- Dim RS1 As Recordset
- Dim DB1 As Database
- Set DB1 = OpenDatabase("D:\Users\li.runbai\Desktop\Access项目\Temperature.mdb")
- Set RS1 = DB1.OpenRecordset(Name:="表" & Table_num, Type:=dbOpenDynaset)
- RS1.FindFirst "时间=" & "'" & Record_Time & "'"
- If RS1.NoMatch = True Then
- Find_Data = -1
- RS1.Close
- Exit Function
- Else
- Find_Data = RS1.Fields(Channel).Value
- End If
- RS1.Close
- Set RS1 = Nothing
- Set DB1 = Nothing
- End Function
- Function Find_Data_SQL(Record_Time As String, Table_num As String, Channel As String)
- If Len(Record_Time) < 11 Or Channel = Empty Then
- Find_Data_SQL = "" '时间不完整直接停止执行,提高速度
- Exit Function
- End If
- Dim Cnn As New ADODB.Connection '定义数据库类变量
- Dim rst As New ADODB.Recordset
- Dim Stpath, strSQL As String '定义长整形变量
- Dim icols, a As Integer '定义数字变量
- Stpath = "D:\Users\li.runbai\Desktop\Access项目\Temperature.mdb" '获取数据库路径,此处为与该excel表在同目录下
- Cnn.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" & Stpath '& ";Jet OLEDB:Database Password=" & "123"
- strSQL = "Select " & "*" & " from 表" & Table_num & " WHERE " & "时间 = '" & Record_Time & "' "
-
- Debug.Print strSQL
- rst.Open strSQL, Cnn
-
- If rst.EOF Or rst.BOF Then
- Find_Data_SQL = -1
- Else
- Find_Data_SQL = rst.Fields(Channel).Value
- End If
-
- rst.Close
- Set rst = Nothing
- Set Cnn = Nothing
- End Function
复制代码 |
|