在access中用vba如何把SQL语句查询到的一个值赋给变量?
如我定义了strSQL1="Select制动效率from制动效率表WHERE机车车辆类型='"&Combo1&"'"查询到了一个数字,还定义了一个变量p,如何把这个值赋给...
如我定义了strSQL1 = "Select 制动效率 from 制动效率表 WHERE 机车车辆类型='" & Combo1 & "'"查询到了一个数字,还定义了一个变量p,如何把这个值赋给p呢?
我写了段代码Private Sub Command3_Click()
Dim strSQL1
Dim p
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
strSQL1 = "Select 制动效率 from 制动效率表 WHERE 机车车辆类型='" & Combo1 & "'"
rs.Open strSQL1, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
我是新手,还请高手指点啊,越详细越好 展开
我写了段代码Private Sub Command3_Click()
Dim strSQL1
Dim p
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
strSQL1 = "Select 制动效率 from 制动效率表 WHERE 机车车辆类型='" & Combo1 & "'"
rs.Open strSQL1, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
我是新手,还请高手指点啊,越详细越好 展开
2个回答
展开全部
以下代码没有运行测试,如果有手误,自行改一下或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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询