2015-12-22 120 views
0

请问你能帮助我吗?我在Access中有一个数据库和一个用Delphi编写的程序(我使用了Ehlib库),我试图在VBA中重写该程序。如何检查VBA中的记录集中是否有记录?

with CustomDriver do 
    try 
    with SelectCommand do 
    begin 
     CommandText.Text := 'select count(*) from OBJ_APP a' + ^M + 
          'where ID_App = :AID_App and exists (select 1 from obj_app b where b.ID_Obj = a.ID_Obj and b.ID_App = :AID_App_Replace)'; 
     Parameters.ParamByName('AID_App').Value := AID_App; 
     Parameters.ParamByName('AID_App_Replace').Value := AID_App_Replace; 
    end; 
    vFree := True; 
    ExecuteCommand(SelectCommand, DsReplace, vFree); 
if (DsReplace <> Nil) 
and(DsReplace.RecordCount > 0) 
and(DsReplace.Fields[0].AsInteger > 0) then 

如何在VBA中编写最后三行代码?有没有比& Chr (34) &更容易编写双引号的方法?

回答

1

打开记录集后只需检查.EOF

Set rs = CurrentDb.OpenRecordset("SELECT * FROM foo") 
If Not rs.EOF Then 
    If rs(0) > 0 Then 
    ' or if you want to be explicit 
    If rs.Fields(0).Value > 0 Then 

双引号被他们加倍掩盖:

Debug.Print "Hello ""World""" 

但通常可以使你的代码更好的可读性通过使用单引号:

strSQL = "SELECT * FROM foo WHERE bar = 'something'" 
0

您还可以检查RecordCount

Set rs = CurrentDb.OpenRecordset("Select * From YourTable") 
If rs.RecordCount > 0 Then 
    ' At least one record exists. 
    ' Do stuff. 
End If 
相关问题