在access中用vba如何把SQL语句查询到的一个值赋给变量?

2025-01-06 04:44:19
推荐回答(2个)
回答1:

以下代码没有运行测试,如果有手误,自行改一下或HI我.
Dim cn As New ADODB.Connection
cn.Open "...." '连接句自己写吧.
Dim cmd As New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandText = "select * from tb where dt >@d1 and dt<@d2 "
Dim p1 As ADODB.Parameter, p2 As ADODB.Parameter
Set p1 = cmd.CreateParameter("@d1", adDate, adParamInput)
Set p2 = cmd.CreateParameter("@d2", adDate, adParamInput)
p1.Value = Format(Now(), "yyyy-MM-1")
p2.Value = Now()

cmd.Parameters.Append p1
cmd.Parameters.Append p2
Dim rs As ADODB.Recordset

Set rs = cmd.Execute

'这个rs是你要的返回值

rs.Close
Set rs = Nothing
Set cmd = Nothing
'cn.Close
'Set cn = Nothing

回答2:

p=rs("制动效率“)