判断字段是否存在
if Rs.Fields( "字段 ") then
.....
end if
我这个字段有时候有,有时候没有的,现在就是判断它有没有,然后处理,但是怎么判断呢?
[解决办法]
haveField=false
for each i in rs.fields
if i.name= "字段 " then
haveField=true
exit for
end if
next
if haveField then msgbox "字段存在! "
[解决办法]
顶楼上的.
另外
如果sql数据库可以查数据库表
select * from syscolumns a,sysobjects b where a.id=b.id and a.name= '字段 ' and b.name= '表 '
[解决办法]
Dim cn As ADODB.Connection, rs As ADODB.Recordset
Dim i As Integer
Dim TheFieldExisting As Boolean
Dim strField As String
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB;Persist Security Info=False "
cn.Open
Set rs = New ADODB.Recordset
rs.Open ( "SELECT * FROM titles "), cn
'方法一
For i = 0 To rs.Fields.Count - 1
If UCase(rs.Fields(i).Name) = "TITLE " Then
TheFieldExisting = True
Exit For
End If
Next i
MsgBox "Field [Title] " & IIf(TheFieldExisting, " ", "Not ") & "in Place "
'方法二
On Error Resume Next
strField = rs!UserName
If Err.Number = 3265 Then TheFieldExisting = False
MsgBox "Field [UserName] " & IIf(TheFieldExisting, " ", "Not ") & "in Place "