选择在Microsoft SQL查询只特定的数据类型
问题描述:
在Access 2007年,我需要选择所有表中的短文本字段。选择在Microsoft SQL查询只特定的数据类型
VBA代码看起来应该是这样:
Dim strClient As String
Set dbs = CurrentDb()
Debug.Print Me.ID
strClient = "Select * from ANG_CLIENTS where DATA_TYPE='TEXT' AND ID=" & Me.ID
Set rs = dbs.OpenRecordset(strClient)
我得到的最后一个任务“运行时错误3061参数太少预计1”。
答
您需要通过记录的字段定义自定义函数中循环,并只提取文本字段的名称。然后
名称可以添加到您的SQL脚本。
Public Function TextDataFileds(rs As DAO.Recordset) As String
Dim fld As DAO.Field, item As String
For Each fld In rs.Fields
If fld.Type = 10 Then 'dbText
item = IIf(Len(item) = 0, fld.Name, item & ", " & fld.Name)
End If
Next fld
TextDataFileds = item
End Function
然后你可以这样调用:
Sub Test()
On Error GoTo ErrProc
Dim rs As DAO.Recordset
Set rs = CurrentDb().OpenRecordset("SELECT TOP 1 * FROM ANG_CLIENTS;")
Dim sql_ As String
sql_ = "SELECT " & TextDataFileds(rs) & " FROM ANG_CLIENTS WHERE ID=" & Me!ID
rs.Close
Set rs = Nothing
Set rs = CurrentDb().OpenRecordset(sql_)
'....
Leave:
rs.Close
Set rs = Nothing
On Error GoTo 0
Exit Sub
ErrProc:
MsgBox Err.Description, vbCritical
Resume Leave
End Sub
@Parfait谢谢,我忽略了这个。 –
@Parfait:“I如果不是VBA函数” ......哦,是。输入'VBA.I'和IntelliSense将它作为您的首选。 – Gustav