我有一个窗体设置为数据表视图,我试图从SQL Server数据的基础上,由用户选择指定的参数进行更新。
我通常使用存储过程实现这一点,有VBA调用它们通过ADO,但这个数据是在记录源需要能够加以修正/数据表中直接编辑不同。
我有我目前使用的显示数据的数据表,这个使用下面的代码:
Dim db As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sp As ADODB.Command
Set db = New ADODB.Connection
Set rs = New ADODB.Recordset
Set sp = New ADODB.Command
Set ps = frmName
db.Open dbString
db.CursorLocation = adUseClient
With sp
.ActiveConnection = db
.CommandText = "x04_ch_sl_ptsTable"
.CommandType = adCmdStoredProc
.Parameters.Append sp.CreateParameter("@tmName", adVarChar, adParamInput, 4, tmName)
End With
sp.Parameters.Append sp.CreateParameter("@wkEnd", adDBTimeStamp, adParamInput, , wkEnd)
Set rs = sp.Execute
Set ps.subFormDataViewer.Form.Recordset = rs
Exit Sub
End If
db.Close
可以这样修改,使数据集更新?
这是迄今为止我已经能够达到我想要做的唯一途径,但由于majorly明显的安全和注射的问题,我不希望走这条路线:
ps.subFormDataViewer.Form.RecordSource = " SELECT p.ps_dateClaim as [Date], p.ps_id as [ID], p.ps_empid as [Employee ID], e.hc_fullName as [Employee], p.ps_pts as [Claimed] " & _
" FROM (([ODBC;DRIVER=SQL Server;SERVER=<REDACTED>;Integrated_Security=SSPI;DATABASE=<REDACTED>].prod_pts as p " & _
" INNER JOIN [ODBC;DRIVER=SQL Server;SERVER=<REDACTED>;Integrated_Security=SSPI;DATABASE=<REDACTED>].hc_employee as e " & _
" ON p.ps_empid = e.hc_empid) " & _
" WHERE (p.ps_dateClaim = #" & wkEnd & "#" & _
" AND (e.hc_teamName = '" & tmName & "'); "
我曾尝试没有成功下列ADO方法:
With sp
Set .ActiveConnection = db
.CommandText = " SELECT t.Date, t.TeamName, t.Reference, t.Status, t.Reason, t.Checked " & _
" FROM testTbl as t" & _
" WHERE t.TeamName = @tmName "
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 4, tm)
Set rs = .Execute
End With
With sp
Set .ActiveConnection = db
.CommandText = " SELECT t.Date, t.TeamName, t.Reference, t.Status, t.Reason, t.Checked " & _
" FROM testTbl as t" & _
" WHERE t.TeamName = ? "
Set rs = .Execute(, Array(tm))
End With
(第2实施例确实产生数据,但是,当它识别每个字段为“表达”记录是不可更新)
我已经能够使用DAO实现这一点:
Dim db as DAO.Database
Dim rs as DAO.Recordset
Dim qd as DAO.QueryDef
Dim dbSQL as string
dbSQL = " SELECT t.Date, t.TeamName, t.Reference, t.Status, t.Reason, t.Checked " & _
" FROM testTbl as t" & _
" WHERE t.TeamName = tmName1 "
Set qd = db.CreateQueryDef("", dbSQL)
With qd
.Parameters!tmName1 = tm
End With
Set rs = qd.OpenRecordset
Set ps.subFormDataViewer.Form.Recordset = rs