Recordset Error

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Hi folks,

I need a help on my following code:

Private Sub Form_Current()
Dim dbCurrent As Database
Dim rst As Recordset

Set dbCurrent = CurrentDb
Set rst = dbCurrent.OpenRecordset("select field1 from
table1")

Me.Text0.Value = rst.Fields("field1").Value

End Sub

The code is working fine if the recordset is not null. If
the recordset is null, it will give me an error message.
I want me.text0.value=null when recordset is null.

Could anyone show me how to do it?

Thanks in advance.

Tim.
 
Tim said:
Hi folks,

I need a help on my following code:

Private Sub Form_Current()
Dim dbCurrent As Database
Dim rst As Recordset

Set dbCurrent = CurrentDb
Set rst = dbCurrent.OpenRecordset("select field1 from
table1")

Me.Text0.Value = rst.Fields("field1").Value

End Sub

The code is working fine if the recordset is not null. If
the recordset is null, it will give me an error message.
I want me.text0.value=null when recordset is null.

Could anyone show me how to do it?

Thanks in advance.

Tim.

A recordset can't be Null, but I suspect you mean that it contains no
records. You can check for that easily enough like this:

Set rst = dbCurrent.OpenRecordset( _
"select field1 from table1")

With rst
If .EOF Then
Me.Text0.Value = Null
Else
Me.Text0.Value = rst.Fields("field1").Value
End If
.Close '*** you should close the recordset when you're done.
End With
 
Dirk,

It works great.

Thanks.

Tim.
-----Original Message-----


A recordset can't be Null, but I suspect you mean that it contains no
records. You can check for that easily enough like this:

Set rst = dbCurrent.OpenRecordset( _
"select field1 from table1")

With rst
If .EOF Then
Me.Text0.Value = Null
Else
Me.Text0.Value = rst.Fields("field1").Value
End If
.Close '*** you should close the recordset when you're done.
End With

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


.
 
Back
Top